马上又要到情人节了,再不解风情的人也得向女友表示表示。作为一个程序员,示爱的时候自然也要用我们自己的方式。
这里给大家上传一段我在今年情人节的时候写给女朋友的一段简单的java swing代码,主要定义了一个对话框,让女友选择是不是喜欢自己。如果她选了“是”,皆大欢喜,如果她想选“不”,哼哼。。。看一下截图吧。
代码效果图:
接下来不废话,直接上代码了。新版本已上传,也欢迎大家到我的github上下载和改进代码(点此转到github)。
另外就是因为这个代码当时是在情人节的时候写的,对话框标题栏的信息也是与情人节相关,要想在其他的节日使用,只需要修改几个字符串就可以了,我在需要修改的地方都打了中文注释,大家可以很容易地找到。不过正如我在注释里写的那样,这个程序顶多是你俩之间一个温馨的小玩笑,你要是想今晚嘿嘿嘿的话,真正的礼物还是得备好哦: )
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
|
package gift_package; import java.awt.container; import java.awt.font; import java.awt.toolkit; import java.awt.event.mouseevent; import java.awt.event.mouselistener; import java.awt.event.windowevent; import java.awt.event.windowlistener; import javax.swing.jbutton; import javax.swing.jdialog; import javax.swing.jframe; import javax.swing.jlabel; import javax.swing.swingconstants; import javax.swing.windowconstants; /** * a funny code for your lover, which creates a frame that let her/him choose * whether she/he loves you. if she/he choose 'yes', everythingis normal, but * if she/he tries to choose 'no', something interestingwould happen. first, * the 'no' button would change its position, it lookes like it attemps to escape * from being clicked. after a couple of rounds, if she/he still want to click * 'no' button, the 'no' button and 'yes' button will exchange their position. * besides, the window will cannot be closed untill the 'yes' button is clicked. * * to use this code, please make sure her/his computer has installed the jre. * * note that this code is just a little joke, do not use it as a real valentin's * day gift, if you want to get laid at valentin's day, use rose, wine and fancy * restaurant, if you want to keep your mate's love, use your heart. * * @author rainman_zjd * @version initialt version, 2016.3.20 */ public class happyvalentinsday extends jframe { private static final long serialversionuid = 1l; private jlabel label; private jbutton button1; private jbutton button2; private jdialog dialog1; private int entercount = 0 ; private boolean chooseflag = false ; public static final int screenwidth = ( int )toolkit.getdefaulttoolkit().getscreensize().getwidth(); public static final int screenheight = ( int )toolkit.getdefaulttoolkit().getscreensize().getheight(); public happyvalentinsday() { label = new jlabel( "hi, my name is rainman_zjd, i love you, do you love me?" , swingconstants.center); // 自行修改 button1 = new jbutton( "no, i don't!" ); // 按钮1 button2 = new jbutton( "yes, i do!" ); // 按钮2 dialog1 = new jdialog( this ); // 创建一个新的对话框,并设置父窗口为当前窗体 windowinitial(); setwindowlistener(); } // constructor public happyvalentinsday(string labeltxt, string bt1txt, string bt2txt) { label = new jlabel(labeltxt, swingconstants.center); button1 = new jbutton(bt1txt); button2 = new jbutton(bt2txt); dialog1 = new jdialog( this ); windowinitial(); chooseflag = true ; setdefaultcloseoperation(windowconstants.dispose_on_close); setvisible( true ); } // constructor_string /** * 窗体初始化,使用的是绝对布局 */ private void windowinitial() { setdialog(dialog1, "awesome!" , "meeting you is the luckest thing in my life!" ); // 自行修改 label.setfont( new font( "" , font.bold, 17 )); label.setbounds( 0 , 30 , 480 , 20 ); /** * 以匿名内部类的方式为按钮1添加鼠标事件监听器,当鼠标进入按钮1后将突然改变自己的位置 */ button1.addmouselistener( new mouselistener() { @override public void mousereleased(mouseevent e) { return ;} @override public void mousepressed(mouseevent e) { return ;} @override public void mouseexited(mouseevent e) { return ;} @override public void mouseentered(mouseevent e) { switch (entercount) { case 0 : button1.setbounds( 75 , 60 , 110 , 30 ); happyvalentinsday. this .repaint(); ++entercount; break ; case 1 : button1.setbounds( 75 , 110 , 110 , 30 ); happyvalentinsday. this .repaint(); ++entercount; break ; case 2 : button1.setbounds( 155 , 60 , 110 , 30 ); happyvalentinsday. this .repaint(); ++entercount; break ; case 3 : button1.setbounds( 75 , 110 , 110 , 30 ); happyvalentinsday. this .repaint(); ++entercount; break ; case 4 : button1.setbounds( 310 , 110 , 110 , 30 ); button2.setbounds( 75 , 110 , 110 , 30 ); happyvalentinsday. this .repaint(); ++entercount; break ; case 5 : button1.setbounds( 75 , 110 , 110 , 30 ); button2.setbounds( 310 , 110 , 110 , 30 ); happyvalentinsday. this .repaint(); entercount = 0 ; break ; } // seitch_entercount } // mouseentered @override public void mouseclicked(mouseevent e) { dialog1.setvisible( true ); setdefaultcloseoperation(dispose_on_close); } // mouseclicked }); // mouselistener button1.setbounds( 70 , 110 , 110 , 30 ); button1.setfont( new font( "" , font.bold, 13 )); /** * 以匿名内部类的方式为按钮2添加鼠标事件监听器,按下时显示对话框 */ button2.addmouselistener( new mouselistener() { @override public void mousereleased(mouseevent e) { return ;} @override public void mousepressed(mouseevent e) { return ;} @override public void mouseexited(mouseevent e) { return ;} @override public void mouseentered(mouseevent e) { return ;} @override public void mouseclicked(mouseevent e) { dialog1.setvisible( true ); chooseflag = true ; setdefaultcloseoperation(dispose_on_close); } // mouseclicked }); // mouselistener button2.setbounds( 310 , 110 , 110 , 30 ); button2.setfont( new font( "" , font.bold, 13 )); container c = getcontentpane(); c.setlayout( null ); c.add(label); c.add(button1); c.add(button2); settitle( "happy valentin's day!" ); // 自行修改 setbounds(screenwidth/ 2 - 250 , screenheight/ 2 - 100 , 500 , 200 ); setresizable( false ); setdefaultcloseoperation(windowconstants.do_nothing_on_close); } // windowinitial /** * 设置对话框属性 * @param diag * @param tittle * @param txt */ private void setdialog(jdialog diag, string tittle, string txt) { jlabel diaglabel = new jlabel(txt, swingconstants.center); diaglabel.setfont( new font( "" , font.bold, 17 )); diaglabel.setbounds( 0 , 40 , 430 , 20 ); jbutton diagbut = new jbutton( "confirm" ); diagbut.setfont( new font( "" , font.bold, 14 )); diagbut.setbounds( 155 , 100 , 100 , 30 ); diagbut.addmouselistener( new mouselistener() { @override public void mousereleased(mouseevent e) { return ;} @override public void mousepressed(mouseevent e) { return ;} @override public void mouseexited(mouseevent e) { return ;} @override public void mouseentered(mouseevent e) { return ;} @override public void mouseclicked(mouseevent e) { diag.dispose(); if (chooseflag) system.exit( 0 ); } // mouseclicked }); diag.settitle(tittle); diag.setbounds(screenwidth/ 2 - 225 , screenheight/ 2 - 100 , 450 , 200 ); diag.setlayout( null ); diag.add(diagbut); diag.add(diaglabel); } // setdialog /** * 设置单击窗口关闭按钮时的动作 */ private void setwindowlistener() { this .addwindowlistener( new windowlistener() { @override public void windowopened(windowevent e) { return ;} @override public void windowiconified(windowevent e) { return ;} @override public void windowdeiconified(windowevent e) { return ;} @override public void windowdeactivated(windowevent e) { return ;} @override public void windowclosed(windowevent e) { return ;} @override public void windowactivated(windowevent e) { return ;} @override public void windowclosing(windowevent e) { if (!chooseflag) { string labeltxt = "is your default choose \"yes, i do!\"?" ; // 自行修改 new happyvalentinsday(labeltxt, "no" , "yes" ); } // if } // windowclosing }); // windowlistener } // setwindowlistener public static void main(string[] args) { happyvalentinsday myapp = new happyvalentinsday(); myapp.setvisible( true ); } // main } /*happyvalentinsday*/ |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家,祝大家情人节快乐
原文链接:http://blog.csdn.net/u013916933/article/details/51458967