本文為大家分享了swing實(shí)現(xiàn)窗體拖拽和拉伸的具體代碼,供大家參考,具體內(nèi)容如下
當(dāng)用setundecorated(true) 后 jframe去掉標(biāo)題欄后就得自己寫拖拽和拉伸功能了。
下面是效果圖,我的截圖軟件不能夠截取除系統(tǒng)默認(rèn)光標(biāo)外的光標(biāo),所以各個(gè)方向光標(biāo)變化在圖中沒(méi)有體現(xiàn)
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import javax.swing.*; import java.awt.*; /** * 窗體拖拽和拉伸 */ public class winresizedemo { private jframe jf; public winresizedemo(){ jf= new jframe(); jf.setundecorated( true ); //去標(biāo)邊界和標(biāo)題欄 jf.setlocationrelativeto( null ); //窗口置中 jf.setsize( 400 , 400 ); jf.setdefaultcloseoperation(jframe.exit_on_close); resizeevent dg = new resizeevent(jf); /**添加兩個(gè)監(jiān)聽器**/ jf.addmouselistener(dg); jf.addmousemotionlistener(dg); jf.setvisible( true ); } public static void main(string [] args){ new winresizedemo(); } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
|
import javax.swing.*; import java.awt.*; import java.awt.event.mouseadapter; import java.awt.event.mouseevent; /** * 實(shí)現(xiàn)窗口各個(gè)方向拉伸以及拖動(dòng)。 */ public class resizeevent extends mouseadapter{ public jframe jf; private point prepos,curpos,jfpos; private static final double breadth = 15.0 ; //邊界拉伸范圍 private int dragtype; private static final int drag_move = 1 ; private static final int drag_up = 2 ; private static final int drag_upleft = 3 ; private static final int drag_upright = 4 ; private static final int drag_left = 5 ; private static final int drag_right = 6 ; private static final int drag_bottom = 7 ; private static final int drag_bottomleft = 8 ; private static final int drag_bottomright = 9 ; public resizeevent(jframe jf){ this .jf = jf; } @override public void mousepressed(mouseevent e){ prepos = e.getlocationonscreen(); } @override public void mousemoved(mouseevent e){ areacheck(e.getpoint()); } @override public void mousedragged(mouseevent e){ curpos = e.getlocationonscreen(); jfpos = jf.getlocation(); dragaction(); prepos = curpos; } private void dragaction(){ switch (dragtype){ case drag_move: jf.setlocation(jfpos.x+curpos.x-prepos.x, jfpos.y+curpos.y-prepos.y); break ; case drag_up: //x位置不變,y位置變化,并且height變化 jf.setlocation(jfpos.x, jfpos.y+curpos.y-prepos.y); jf.setsize(jf.getwidth(), jf.getheight()-(curpos.y-prepos.y)); break ; case drag_left: //y位置不變,x位置變化,width變化 jf.setlocation(jfpos.x+curpos.x-prepos.x, jfpos.y); jf.setsize(jf.getwidth()-(curpos.x-prepos.x), jf.getheight()); break ; case drag_right: //x,y位置不變,width變化 jf.setlocation(jfpos.x, jfpos.y); jf.setsize(jf.getwidth()+(curpos.x-prepos.x), jf.getheight()); break ; case drag_bottom: //x,y位置不變,height變化 jf.setlocation(jfpos.x, jfpos.y); jf.setsize(jf.getwidth(), jf.getheight()+(curpos.y-prepos.y)); break ; case drag_upleft: //x,y位置均變化,h,w均變化 jf.setlocation(jfpos.x+curpos.x-prepos.x, jfpos.y+curpos.y-prepos.y); jf.setsize(jf.getwidth()-(curpos.x-prepos.x), jf.getheight()-(curpos.y-prepos.y)); break ; case drag_bottomright: //x,y位置均不變,h,w變化 jf.setlocation(jfpos.x, jfpos.y); jf.setsize(jf.getwidth()+(curpos.x-prepos.x), jf.getheight()+(curpos.y-prepos.y)); break ; case drag_upright: //x位置不變,y,w,h變化 jf.setlocation(jfpos.x, jfpos.y+curpos.y-prepos.y); jf.setsize(jf.getwidth()+(curpos.x-prepos.x), jf.getheight()-(curpos.y-prepos.y)); break ; case drag_bottomleft: //y不變,xwh變化 jf.setlocation(jfpos.x+curpos.x-prepos.x, jfpos.y); jf.setsize(jf.getwidth()-(curpos.x-prepos.x), jf.getheight()+(curpos.y-prepos.y)); break ; default : break ; } } private boolean areacheck(point p){ if (p.getx()<=breadth && p.gety()<=breadth){ dragtype = drag_upleft; jf.setcursor( new cursor(cursor.nw_resize_cursor)); } else if (p.getx()>breadth && p.getx()<(jf.getwidth()-breadth) && p.gety()<=breadth){ dragtype = drag_up; jf.setcursor( new cursor(cursor.n_resize_cursor)); } else if (p.getx()>=(jf.getwidth()-breadth) && p.gety()<=breadth){ dragtype = drag_upright; jf.setcursor( new cursor(cursor.ne_resize_cursor)); } else if (p.getx()<=breadth && p.gety()<(jf.getheight()-breadth) && p.gety()>breadth){ dragtype = drag_left; jf.setcursor( new cursor(cursor.w_resize_cursor)); } else if (p.getx()>=(jf.getwidth()-breadth) && p.gety()<(jf.getheight()-breadth) && p.gety()>breadth){ dragtype = drag_right; jf.setcursor( new cursor(cursor.e_resize_cursor)); } else if (p.getx()<=breadth && p.gety()>=(jf.getheight()-breadth)){ dragtype = drag_bottomleft; jf.setcursor( new cursor(cursor.sw_resize_cursor)); } else if (p.getx()>breadth && p.getx()<(jf.getwidth()-breadth) && p.gety()>=(jf.getheight()-breadth)){ dragtype = drag_bottom; jf.setcursor( new cursor(cursor.s_resize_cursor)); } else if (p.getx()>=(jf.getwidth()-breadth) && p.gety()>=(jf.getheight()-breadth)){ dragtype = drag_bottomright; jf.setcursor( new cursor(cursor.se_resize_cursor)); } else { dragtype = drag_move; jf.setcursor( new cursor(cursor.move_cursor)); return false ; } return true ; } } |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://blog.csdn.net/a694543965/article/details/78027743