欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-01-15 10:33:23
马上又要到情人节了,再不解风情的人也得向女友表示表示。作为一个程序员,示爱的时候自然也要用我们自己的方式。 这里给大家上传一段我在今年情人节的时候写给女朋友的一段简单的j...

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

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

代码效果图:

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

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