当前位置:   article > 正文

情人节写给女朋友的小程序_发给女朋友的信息的小程序,可选择是否继续

发给女朋友的信息的小程序,可选择是否继续

      今天是情人节哈,再不解风情的人也得向女友表示表示。作为一个程序员,示爱的时候自然也要用我们自己的方式。这里给大家上传一段我在今年情人节的时候写给女朋友的一段简单的Java Swing代码,主要定义了一个对话框,让女友选择是不是喜欢自己。如果她选了“是”,皆大欢喜,如果她想选“不”,哼哼。。。看一下截图吧。


代码效果图:


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

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

  1. package gift_package;
  2. import java.awt.Container;
  3. import java.awt.Font;
  4. import java.awt.Toolkit;
  5. import java.awt.event.MouseEvent;
  6. import java.awt.event.MouseListener;
  7. import java.awt.event.WindowEvent;
  8. import java.awt.event.WindowListener;
  9. import javax.swing.JButton;
  10. import javax.swing.JDialog;
  11. import javax.swing.JFrame;
  12. import javax.swing.JLabel;
  13. import javax.swing.SwingConstants;
  14. import javax.swing.WindowConstants;
  15. /**
  16.  * A funny code for your lover, which creates a frame that let her/him choose 
  17.  * whether she/he loves you. If she/he choose 'YES', everythingis normal, but
  18.  * if she/he tries to choose 'NO', something interestingwould happen. First,
  19.  * the 'NO' button would change its position, it lookes like it attemps to escape
  20.  * from being clicked. After a couple of rounds, if she/he still want to click 
  21.  * 'NO' button, the 'NO' button and 'YES' button will exchange their position. 
  22.  * Besides, the window will cannot be closed untill the 'YES' button is clicked.
  23.  * 
  24.  * To use this code, please make sure her/his computer has installed the JRE. 
  25.  * 
  26.  * Note that this code is just a little joke, DO NOT USE IT AS A REAL VALENTIN'S 
  27.  * DAY GIFT, if you want to get laid at Valentin's Day, use ROSE, WINE and FANCY
  28.  * RESTAURANT, if you want to keep your mate's love, use YOUR HEART.
  29.  *
  30.  * @author rainman_zjd
  31.  * @version initialt version, 2016.3.20
  32.  */
  33. public class HappyValentinsDay extends JFrame {
  34.     private static final long serialVersionUID = 1L;
  35.     private JLabel     label;
  36.     private JButton    button1;
  37.     private JButton    button2;
  38.     private JDialog    dialog1;
  39.     private int enterCount = 0;
  40.     private boolean chooseFlag = false;
  41.     public static final int screenWidth = 
  42.             (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth();
  43.     public static final int screenHeight = 
  44.             (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight();
  45.     public HappyValentinsDay() {
  46.         label   = new JLabel("Hi, my name is rainman_zjd, I love you, do you love me?", SwingConstants.CENTER); // 自行修改
  47.         button1 = new JButton("No, I don't!"); // 按钮1
  48.         button2 = new JButton("Yes, I do!");   // 按钮2
  49.         dialog1 = new JDialog(this);           // 创建一个新的对话框,并设置父窗口为当前窗体
  50.         windowInitial();
  51.         setWindowListener();
  52.     }// constructor
  53.     public HappyValentinsDay(String labelTxt, String bt1Txt, String bt2Txt) {
  54.         label   = new JLabel(labelTxt, SwingConstants.CENTER);
  55.         button1 = new JButton(bt1Txt);
  56.         button2 = new JButton(bt2Txt);
  57.         dialog1 = new JDialog(this);
  58.         windowInitial();
  59.         chooseFlag = true;
  60.         setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
  61.         setVisible(true);
  62.     }// constructor_String
  63.     /**
  64.      * 窗体初始化,使用的是绝对布局
  65.      */
  66.     private void windowInitial() {
  67.         setDialog(dialog1, "Awesome!", "Meeting you is the luckest thing in my life!"); // 自行修改
  68.         label.setFont(new Font("", Font.BOLD, 17));
  69.         label.setBounds(0, 30, 480, 20);
  70.         
  71.         /**
  72.          * 以匿名内部类的方式为按钮1添加鼠标事件监听器,当鼠标进入按钮1后将突然改变自己的位置
  73.          */
  74.         button1.addMouseListener(new MouseListener() { 
  75.             @Override
  76.             public void mouseReleased(MouseEvent e) {return;}           
  77.             @Override
  78.             public void mousePressed(MouseEvent e) {return;}            
  79.             @Override
  80.             public void mouseExited(MouseEvent e) {return;}            
  81.             @Override
  82.             public void mouseEntered(MouseEvent e) {
  83.                 switch(enterCount) {
  84.                 case 0:
  85.                     button1.setBounds(75, 60, 110, 30);
  86.                     HappyValentinsDay.this.repaint();
  87.                     ++enterCount;
  88.                     break;
  89.                 case 1:
  90.                     button1.setBounds(75, 110, 110, 30);
  91.                     HappyValentinsDay.this.repaint();
  92.                     ++enterCount;
  93.                     break;
  94.                 case 2:
  95.                     button1.setBounds(155, 60, 110, 30);
  96.                     HappyValentinsDay.this.repaint();
  97.                     ++enterCount;
  98.                     break;
  99.                 case 3:
  100.                     button1.setBounds(75, 110, 110, 30);
  101.                     HappyValentinsDay.this.repaint();
  102.                     ++enterCount;
  103.                     break;
  104.                 case 4:
  105.                     button1.setBounds(310, 110, 110, 30);
  106.                     button2.setBounds(75, 110, 110, 30);
  107.                     HappyValentinsDay.this.repaint();
  108.                     ++enterCount;
  109.                     break;
  110.                 case 5:
  111.                     button1.setBounds(75, 110, 110, 30);
  112.                     button2.setBounds(310, 110, 110, 30);
  113.                     HappyValentinsDay.this.repaint();
  114.                     enterCount = 0;
  115.                     break;
  116.                 }// seitch_entercount
  117.             }// mouseEntered           
  118.             @Override
  119.             public void mouseClicked(MouseEvent e) {
  120.                 dialog1.setVisible(true);
  121.                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  122.             }// mouseClicked
  123.         });// MouseListener
  124.         
  125.         button1.setBounds(70, 110, 110, 30);
  126.         button1.setFont(new Font("", Font.BOLD, 13));
  127.         
  128.         /**
  129.          * 以匿名内部类的方式为按钮2添加鼠标事件监听器,按下时显示对话框
  130.          */
  131.         button2.addMouseListener(new MouseListener() {      
  132.             @Override
  133.             public void mouseReleased(MouseEvent e) {return;}           
  134.             @Override
  135.             public void mousePressed(MouseEvent e) {return;}           
  136.             @Override
  137.             public void mouseExited(MouseEvent e) {return;}            
  138.             @Override
  139.             public void mouseEntered(MouseEvent e) {return;}           
  140.             @Override
  141.             public void mouseClicked(MouseEvent e) {
  142.                 dialog1.setVisible(true);
  143.                 chooseFlag = true;
  144.                 setDefaultCloseOperation(DISPOSE_ON_CLOSE);
  145.             }// mouseClicked
  146.         });// MouseListener
  147.         button2.setBounds(310, 110, 110, 30);
  148.         button2.setFont(new Font("", Font.BOLD, 13));
  149.         Container c = getContentPane();
  150.         c.setLayout(null);
  151.         c.add(label);
  152.         c.add(button1);
  153.         c.add(button2);
  154.         setTitle("Happy Valentin's Day!"); // 自行修改
  155.         setBounds(screenWidth/2-250, screenHeight/2-100, 500, 200);
  156.         setResizable(false);
  157.         setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
  158.     }// windowInitial
  159.     /**
  160.      * 设置对话框属性
  161.      * @param diag
  162.      * @param tittle
  163.      * @param txt
  164.      */
  165.     private void setDialog(JDialog diag, String tittle, String txt) {
  166.         JLabel diagLabel = new JLabel(txt, SwingConstants.CENTER);
  167.         diagLabel.setFont(new Font("", Font.BOLD, 17));
  168.         diagLabel.setBounds(0, 40, 430, 20);
  169.         JButton diagBut = new JButton("Confirm");
  170.         diagBut.setFont(new Font("", Font.BOLD, 14));
  171.         diagBut.setBounds(155, 100, 100, 30);
  172.         diagBut.addMouseListener(new MouseListener() {            
  173.             @Override
  174.             public void mouseReleased(MouseEvent e) {return;}                        
  175.             @Override
  176.             public void mousePressed(MouseEvent e) {return;}                        
  177.             @Override
  178.             public void mouseExited(MouseEvent e) {return;}                     
  179.             @Override
  180.             public void mouseEntered(MouseEvent e) {return;}          
  181.             @Override
  182.             public void mouseClicked(MouseEvent e) {
  183.                 diag.dispose();
  184.                 if (chooseFlag)
  185.                     System.exit(0);
  186.             }// mouseClicked
  187.         });
  188.         diag.setTitle(tittle);
  189.         diag.setBounds(screenWidth/2-225, screenHeight/2-100, 450, 200);
  190.         diag.setLayout(null);
  191.         diag.add(diagBut);
  192.         diag.add(diagLabel);
  193.     }// setDialog
  194.     /**
  195.      * 设置单击窗口关闭按钮时的动作
  196.      */
  197.     private void setWindowListener() {
  198.         this.addWindowListener(new WindowListener() {           
  199.             @Override
  200.             public void windowOpened(WindowEvent e) {return;}         
  201.             @Override
  202.             public void windowIconified(WindowEvent e) {return;}           
  203.             @Override
  204.             public void windowDeiconified(WindowEvent e) {return;}
  205.             @Override
  206.             public void windowDeactivated(WindowEvent e) {return;}
  207.             @Override
  208.             public void windowClosed(WindowEvent e) {return;}          
  209.             @Override
  210.             public void windowActivated(WindowEvent e) {return;}
  211.             @Override
  212.             public void windowClosing(WindowEvent e) {
  213.                 if(!chooseFlag) {
  214.                     String labelTxt = "Is your default choose \"Yes, I do!\"?"; // 自行修改
  215.                     new HappyValentinsDay(labelTxt, "NO", "YES");
  216.                 }// if
  217.             }// windowClosing
  218.         });// WindowListener
  219.     }// setWindowListener
  220.     public static void main(String[] args) {
  221.         HappyValentinsDay myApp = new HappyValentinsDay();
  222.         myApp.setVisible(true);
  223.     }// main
  224. }/*HappyValentinsDay*/


声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号