本文实例为大家分享了swing登录注册界面展示的具体代码,供大家参考,具体内容如下
开发环境:eclipse neon.3 release (4.6.3)
我用的为javaee版本的,为了便于设计界面,需要安装windowbuilder插件,可以直接在marketplace里搜索到。
说明:参考凯哥视频同步学习的
界面效果
登录界面
注册界面
关键技术
该项目用到了java swing外观插件
用到了图片(点击src右键文件夹取名res并复制图片login.jpg)
源码
com.lsh.view.logindialog
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
package com.lsh.view; import java.awt.borderlayout; import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jpanel; import javax.swing.border.emptyborder; import javax.swing.jtextfield; import javax.swing.jlabel; import java.awt.event.actionlistener; import java.awt.event.actionevent; import javax.swing.imageicon; import javax.swing.border.titledborder; import com.lsh.util.imagescale; @suppresswarnings ( "serial" ) public class logindialog extends jdialog { private final jpanel contentpanel = new jpanel(); private jtextfield textfield; private jtextfield textfield_1; private jtextfield textfield_2; private jtextfield textfield_3; private jtextfield textfield_4; private jtextfield textfield_5; private static final int dialog_width= 414 ; private static final int dialog_height= 340 ; private static final int dialog_height_extend= 573 ; /** * launch the application. */ public static void main(string[] args) { try { org.jb2011.lnf.beautyeye.beautyeyelnfhelper.launchbeautyeyelnf(); } catch (exception e) { //todo exception system.out.println( "加载炫彩皮肤失败!" ); } try { logindialog dialog = new logindialog(); dialog.setdefaultcloseoperation(jdialog.dispose_on_close); dialog.setvisible( true ); } catch (exception e) { e.printstacktrace(); } } /** * create the dialog. */ public logindialog() { setalwaysontop( true ); setresizable( false ); setbounds( 400 , 100 , dialog_width,dialog_height); getcontentpane().setlayout( new borderlayout()); contentpanel.setborder( new emptyborder( 5 , 5 , 5 , 5 )); getcontentpane().add(contentpanel, borderlayout.center); contentpanel.setlayout( null ); //设置居中 //setlocation(windowxy.getxy(logindialog.this.getsize())); jbutton btnnewbutton = new jbutton( "注 册" ); btnnewbutton.addactionlistener( new actionlistener() { public void actionperformed(actionevent arg0) { if (logindialog. this .getheight()==dialog_height_extend){ logindialog. this .setsize(dialog_width,dialog_height); } else { logindialog. this .setsize(dialog_width,dialog_height_extend); } } }); btnnewbutton.setbounds( 53 , 224 , 93 , 23 ); contentpanel.add(btnnewbutton); jbutton btnnewbutton_1 = new jbutton( "登 录" ); btnnewbutton_1.setbounds( 190 , 224 , 93 , 23 ); contentpanel.add(btnnewbutton_1); textfield = new jtextfield(); textfield.setbounds( 133 , 147 , 150 , 25 ); contentpanel.add(textfield); textfield.setcolumns( 10 ); textfield_1 = new jtextfield(); textfield_1.setbounds( 133 , 182 , 150 , 25 ); contentpanel.add(textfield_1); textfield_1.setcolumns( 10 ); jlabel lblnewlabel = new jlabel( "邮 箱" ); lblnewlabel.setbounds( 53 , 151 , 54 , 15 ); contentpanel.add(lblnewlabel); jlabel lblnewlabel_1 = new jlabel( "密 码" ); lblnewlabel_1.setbounds( 53 , 194 , 54 , 15 ); contentpanel.add(lblnewlabel_1); jlabel lblnewlabel_2 = new jlabel( "new label" ); lblnewlabel_2.setbounds( 0 , 0 , 360 , 136 ); imageicon icon= new imageicon(logindialog. class .getresource( "/res/login.jpg" )); icon=imagescale.getimage(icon, lblnewlabel_2.getwidth(), lblnewlabel_2.getheight()); lblnewlabel_2.seticon((icon)); contentpanel.add(lblnewlabel_2); jpanel panel = new jpanel(); panel.setborder( new titledborder( null , "\u6ce8\u518c\u7528\u6237" , titledborder.leading, titledborder.top, null , null )); panel.setbounds( 12 , 259 , 336 , 221 ); contentpanel.add(panel); panel.setlayout( null ); jlabel lblnewlabel_3 = new jlabel( "邮 箱" ); lblnewlabel_3.setbounds( 41 , 29 , 55 , 18 ); panel.add(lblnewlabel_3); jlabel lblnewlabel_4 = new jlabel( "验证码" ); lblnewlabel_4.setbounds( 41 , 85 , 55 , 18 ); panel.add(lblnewlabel_4); jlabel lblnewlabel_5 = new jlabel( "密 码" ); lblnewlabel_5.setbounds( 41 , 115 , 55 , 18 ); panel.add(lblnewlabel_5); jlabel label = new jlabel( "确认密码" ); label.setbounds( 41 , 145 , 55 , 18 ); panel.add(label); textfield_2 = new jtextfield(); textfield_2.setbounds( 123 , 22 , 150 , 25 ); panel.add(textfield_2); textfield_2.setcolumns( 10 ); textfield_3 = new jtextfield(); textfield_3.setbounds( 123 , 80 , 150 , 25 ); panel.add(textfield_3); textfield_3.setcolumns( 10 ); textfield_4 = new jtextfield(); textfield_4.setbounds( 123 , 113 , 150 , 25 ); panel.add(textfield_4); textfield_4.setcolumns( 10 ); textfield_5 = new jtextfield(); textfield_5.setbounds( 123 , 145 , 150 , 25 ); panel.add(textfield_5); textfield_5.setcolumns( 10 ); jbutton btnnewbutton_2 = new jbutton( "发送验证码" ); btnnewbutton_2.setbounds( 123 , 52 , 83 , 23 ); panel.add(btnnewbutton_2); jbutton btnnewbutton_3 = new jbutton( "取 消" ); btnnewbutton_3.setbounds( 51 , 182 , 83 , 27 ); panel.add(btnnewbutton_3); jbutton btnnewbutton_4 = new jbutton( "确 认" ); btnnewbutton_4.setbounds( 190 , 182 , 83 , 27 ); panel.add(btnnewbutton_4); } } |
工具类 com.lsh.util.imagescale
1
2
3
4
5
6
7
8
9
10
11
12
13
|
package com.lsh.util; import java.awt.image; import javax.swing.imageicon; public class imagescale { public static imageicon getimage(imageicon icon, int width, int height){ image image=icon.getimage().getscaledinstance(width, height,image.scale_replicate); icon.setimage(image); return icon; } } |
工具类 com.lsh.util.windowxy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
package com.lsh.util; import java.awt.dimension; import java.awt.point; import java.awt.toolkit; public class windowxy { public static point getxy( int w, int h){ toolkit toolkit=toolkit.getdefaulttoolkit(); int width=toolkit.getscreensize().width; int height=toolkit.getscreensize().height; return new point((width-w)/ 2 ,(height-h)/ 2 ); } public static point getxy(dimension dimension) { return getxy(dimension.width, dimension.height); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/nima1994/article/details/68486131