馬上又要到情人節(jié)了,再不解風(fēng)情的人也得向女友表示表示。作為一個(gè)程序員,示愛(ài)的時(shí)候自然也要用我們自己的方式。
這里給大家上傳一段我在今年情人節(jié)的時(shí)候?qū)懡o女朋友的一段簡(jiǎn)單的java swing代碼,主要定義了一個(gè)對(duì)話框,讓女友選擇是不是喜歡自己。如果她選了“是”,皆大歡喜,如果她想選“不”,哼哼。。??匆幌陆貓D吧。
代碼效果圖:
接下來(lái)不廢話,直接上代碼了。新版本已上傳,也歡迎大家到我的github上下載和改進(jìn)代碼(點(diǎn)此轉(zhuǎn)到github)。
另外就是因?yàn)檫@個(gè)代碼當(dāng)時(shí)是在情人節(jié)的時(shí)候?qū)懙?,?duì)話框標(biāo)題欄的信息也是與情人節(jié)相關(guān),要想在其他的節(jié)日使用,只需要修改幾個(gè)字符串就可以了,我在需要修改的地方都打了中文注釋?zhuān)蠹铱梢院苋菀椎卣业健2贿^(guò)正如我在注釋里寫(xiě)的那樣,這個(gè)程序頂多是你倆之間一個(gè)溫馨的小玩笑,你要是想今晚嘿嘿嘿的話,真正的禮物還是得備好哦: )
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 ); // 創(chuàng)建一個(gè)新的對(duì)話框,并設(shè)置父窗口為當(dāng)前窗體 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 /** * 窗體初始化,使用的是絕對(duì)布局 */ 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 ); /** * 以匿名內(nèi)部類(lèi)的方式為按鈕1添加鼠標(biāo)事件監(jiān)聽(tīng)器,當(dāng)鼠標(biāo)進(jìn)入按鈕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 )); /** * 以匿名內(nèi)部類(lèi)的方式為按鈕2添加鼠標(biāo)事件監(jiān)聽(tīng)器,按下時(shí)顯示對(duì)話框 */ 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 /** * 設(shè)置對(duì)話框?qū)傩? * @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 /** * 設(shè)置單擊窗口關(guān)閉按鈕時(shí)的動(dòng)作 */ 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*/ |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家,祝大家情人節(jié)快樂(lè)
原文鏈接:http://blog.csdn.net/u013916933/article/details/51458967