服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|JavaScript|易语言|

服务器之家 - 编程语言 - JAVA教程 - 情人节写给女朋友Java Swing代码程序

情人节写给女朋友Java Swing代码程序

2021-04-05 13:26阿笨爹 JAVA教程

这篇文章主要为大家分享了情人节写给女朋友的java小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,祝大家每天都是情人节

马上又要到情人节了,再不解风情的人也得向女友表示表示。作为一个程序员,示爱的时候自然也要用我们自己的方式。

这里给大家上传一段我在今年情人节的时候写给女朋友的一段简单的java swing代码,主要定义了一个对话框,让女友选择是不是喜欢自己。如果她选了“是”,皆大欢喜,如果她想选“不”,哼哼。。。看一下截图吧。

代码效果图:

情人节写给女朋友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

延伸 · 阅读

精彩推荐
  • JAVA教程基于java中集合的概念(详解)

    基于java中集合的概念(详解)

    下面小编就为大家带来一篇基于java中集合的概念(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    翊风8902021-01-01
  • JAVA教程JAVA中常见异常类

    JAVA中常见异常类

    本文主要介绍了JAVA中的常见异常类。具有很好的参考价值,下面跟着小编一起来看下吧 ...

    toyzhou3102020-07-31
  • JAVA教程Java接口和抽象类实例分析

    Java接口和抽象类实例分析

    这篇文章主要介绍了Java接口和抽象类,实例分析了java接口与抽象类的概念与相关使用技巧,需要的朋友可以参考下 ...

    海 子1622019-12-19
  • JAVA教程Spring MVC项目中log4J和AOP使用详解

    Spring MVC项目中log4J和AOP使用详解

    项目日志记录是项目开发、运营必不可少的内容,有了它可以对系统有整体的把控,出现任何问题都有踪迹可寻。下面这篇文章主要给大家介绍了关于Spr...

    日生三金9932021-03-04
  • JAVA教程Java concurrency之AtomicReference原子类_动力节点Java学院整理

    Java concurrency之AtomicReference原子类_动力节点Java学院整理

    AtomicReference是作用是对"对象"进行原子操作。这篇文章主要介绍了Java concurrency之AtomicReference原子类,需要的朋友可以参考下...

    动力节点3502020-11-09
  • JAVA教程Spring Boot(二)之web综合开发

    Spring Boot(二)之web综合开发

    本篇文章为大家介绍spring boot的其它特性(有些未必是spring boot体系桟的功能,但是是spring特别推荐的一些开源技术本文也会介绍),对了这里只是一个大概...

    纯洁的微笑3802020-09-27
  • JAVA教程java线程并发semaphore类示例

    java线程并发semaphore类示例

    Java 5.0里新加了4个协调线程间进程的同步装置,它们分别是Semaphore, CountDownLatch, CyclicBarrier和Exchanger,本例主要介绍Semaphore,Semaphore是用来管理一个资源池的工...

    java教程网5212019-10-31
  • JAVA教程Java Mybatis中的 ${ } 和 #{ }的区别使用详解

    Java Mybatis中的 ${ } 和 #{ }的区别使用详解

    这篇文章主要介绍了Mybatis中的 ${ } 和 #{ }的区别使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋...

    大头就是我4362020-07-21