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

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

代码效果图:

接下来不废话,直接上代码了。新版本已上传,也欢迎大家到我的github上下载和改进代码(点此转到github)。

另外就是因为这个代码当时是在情人节的时候写的,对话框标题栏的信息也是与情人节相关,要想在其他的节日使用,只需要修改几个字符串就可以了,我在需要修改的地方都打了中文注释,大家可以很容易地找到。不过正如我在注释里写的那样,这个程序顶多是你俩之间一个温馨的小玩笑,你要是想今晚嘿嘿嘿的话,真正的礼物还是得备好哦: )

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*/

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持,祝大家情人节快乐

给女朋友道歉的java代码_情人节写给女朋友Java Swing代码程序相关推荐

  1. java情人节_情人节写给女朋友Java Swing代码程序

    马上又要到情人节了,再不解风情的人也得向女友表示表示.作为一个程序员,示爱的时候自然也要用我们自己的方式. 这里给大家上传一段我在今年情人节的时候写给女朋友的一段简单的Java Swing代码,主要定 ...

  2. 送给女朋友的java程序_情人节写给女朋友Java Swing代码程序

    马上又要到情人节了,再不解风情的人也得向女友表示表示.作为一个程序员,示爱的时候自然也要用我们自己的方式. 这里给大家上传一段我在今年情人节的时候写给女朋友的一段简单的Java Swing代码,主要定 ...

  3. 如何编写无法维护的代码_如何写出让同事无法维护的代码?

    程序命名 容易输入的变量名.比如:Fred,asdf 单字母的变量名.比如:a,b,c,x,y,z(如果不够用,可以考虑 a1,a2,a3,a4,-.) 有创意地拼写错误.比如:SetPintleOp ...

  4. python如何写代码_如何写出优雅的Python代码?

    有时候你会看到很Cool的Python代码,你惊讶于它的简洁,它的优雅,你不由自主地赞叹:竟然还能这样写.其实,这些优雅的代码都要归功于Python的特性,只要你能掌握这些Pythonic的技巧,你一 ...

  5. python注销一段代码_请写出一段Python代码实现删除一个list里面的重复元素?

    方法1:使用set函数 s=set(list),然后再list(s) 方法2:append 1 defdelList(L):2 L1 =[]3 for i inL:4 if i not inL1:5 ...

  6. 灰色模型 java代码_灰色模型的简单Java实现

    前几天在以前的遗留代码中发现一个问题,就是我生成的一个数据的走势曲线的预测值(用于灰色时间序列预测)总是和老代码里的不一致,具体来说就是:遗留代码里面的预测值的斜率总是为零,相比之下我生成的就比较合理 ...

  7. 双表查询java代码_什么是JDBC?Java数据库连接性简介

    JDBC(Java数据库连接性)是Java API,用于管理与数据库的连接,发出查询和命令以及处理从数据库获得的结果集.JDBC在1997年作为JDK 1.1的一部分发布,是为Java持久层开发的首批 ...

  8. 优秀的java代码_像这样写,Java菜鸟也能写出牛逼的代码

    场景一 有时候我们会遇到一个方法就是占满了整个屏幕,其中各种if else 判断 ,for 循环嵌套,时不时来穿插着各种a b c参数,让人看得实在是眼花缭乱.让后面维护的人望而却步,也实在的代码块后 ...

  9. java象棋人机代码_中国象棋人机对弈Java版源码

    [实例简介] 中国象棋人机对弈Java版源码,包含人工智能实现(含多个难度级别,采用α-β迭代搜索算法) [实例截图] [核心代码] 中国象棋人机对弈Java版源码 ├── boards │   ├─ ...

最新文章

  1. 遇到网络问题你是怎么解决的?安琪拉有二招
  2. 港中文提出全新点云上采样方法,破解自动驾驶感知难题
  3. python soup提取叶子标签_python3用BeautifulSoup抓取div标签
  4. 常考数据结构与算法:输出二叉树的右视图
  5. Python之并行--基于joblib
  6. HBASE启动失败,Failed construction of Master: class org.apache.hadoop.hbase.master.HMaster
  7. 离散结构和离散数学中文书_在离散数学中对场景执行的操作
  8. Android硬件抽象层(HAL)概要介绍和学习计划 1转
  9. [ZZ]MVC设计模式
  10. 20145209预备作业01
  11. 详解Nacos的高可用特性(转载)
  12. mysql limit索引变_Mysql limit 优化,百万至千万级快速分页 复合索引的引用并应用于轻量级框架...
  13. 如何使用 Apple Watch 拨打电话?
  14. linux运维视频教程
  15. 手机新趋势:智能大屏
  16. 一款HP的本本大家帮忙看一下
  17. Java扫雷游戏的设计与实现毕业设计论文
  18. python做语音信号处理
  19. Java算法_优先队列和PriorityQueue——HDU 1873:看病要排队
  20. 每个程序员都应该读的非编程书

热门文章

  1. 300英雄服务器修改数据库,建议300这么改一下~
  2. 广东二级造价工程师《造价管理》真题解析
  3. 你带她满星通关,她为你笑靥如花|Cocos精品《保卫萝卜3》
  4. 【Python游戏】Python各大游戏合集:超级玛丽、天天酷跑、我的世界、魔塔、雷霆战机 附带源码
  5. web前端入门到实战:css伪元素::after和::before,及图标字体的使用
  6. 视频压缩的基本原理,一些常见压缩算法的概念
  7. linux删除文件夹下所有文件
  8. Windows桌面程序自动化控制之uiautomation模块全面讲解
  9. java开发和PHP开发有什么区别?
  10. 【运维】Linux如何解决root用户Operation not permitted