微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

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

代码效果图:

接下来不废话,直接上代码了。新版本已上传,也欢迎大家到我的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*/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐