本文为大家分享了swing实现窗体拖拽和拉伸的具体代码,供大家参考,具体内容如下
当用setundecorated(true) 后 jframe去掉标题栏后就得自己写拖拽和拉伸功能了。
下面是效果图,我的截图软件不能够截取除系统默认光标外的光标,所以各个方向光标变化在图中没有体现
代码如下:
?
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 ); //去标边界和标题栏
jf.setlocationrelativeto( null ); //窗口置中
jf.setsize( 400 , 400 );
jf.setdefaultcloseoperation(jframe.exit_on_close);
resizeevent dg = new resizeevent(jf);
/**添加两个监听器**/
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;
/**
* 实现窗口各个方向拉伸以及拖动。
*/
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 ;
}
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。
原文链接:http://blog.csdn.net/a694543965/article/details/78027743
相关文章
猜你喜欢
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
- 个人网站服务器域名解析设置指南:从购买到绑定全流程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-27 83
-
2025-06-04 58
-
2025-05-25 73
-
2025-05-25 27
-
2025-05-29 39
热门评论