Flask接口签名sign原理与实例代码浅析
846
2023-02-17
情人节写给女朋友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*/
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~