Valentine's Day is coming soon, and no matter how unfamiliar people are, they have to express their feelings to their girlfriends. As a programmer, we must naturally use our own way when we show our love.
Here I will upload a simple Java Swing code I wrote to my girlfriend on Valentine's Day this year. It mainly defines a dialog box to let my girlfriend choose whether she likes herself. If she chooses "yes", everyone will be happy. If she wants to choose "no", hum. . . Take a look at the screenshot.
Code rendering:
Next, I won’t talk nonsense, just upload the code. The new version has been uploaded, and everyone is also welcome to download and improve the code on my github (click here to go to github).
In addition, because this code was written on Valentine's Day at that time, and the information in the title bar of the dialog box is also related to Valentine's Day. If you want to use it on other festivals, you only need to modify a few strings. I have written Chinese annotations in the places where I need to modify, so you can easily find it. But as I wrote in the comments, this program is at most a warm little joke between you two. If you want tonight, the real gift must be prepared: )
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', everything normal, but * if she/he tries to choose 'NO', something interesting would happen. First, * the 'NO' button would change its position, it looks like it attempts 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 not be closed until 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 choiceFlag = 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); // Modify button1 = new JButton("No, I don't!"); // Button 1 button2 = new JButton("Yes, I do!"); // Button 2 dialog1 = new JDialog(this); // Create a new dialog box and set the parent window to the current form 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 /** * Form initialization, using absolute layout*/ private void windowInitial() { setDialog(dialog1, "Awesome!", "Meeting you is the lucky thing in my life!"); // Modify label.setFont(new Font("", Font.BOLD, 17)); label.setBounds(0, 30, 480, 20); /** * Add a mouse event listener to button 1 in anonymous internal class. When the mouse enters button 1, it will suddenly change its position*/ 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, 30); HappyValentinsDay.this.repaint(); ++enterCount; break; case 5: button1.setBounds(75, 110, 110, 30); button2.setBounds(310, 110, 30); 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)); /** * Add a mouse event listener to button 2 in anonymous inner class, and the dialog box is displayed when pressed*/ 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!"); // Modify setBounds(screenWidth/2-250, screenHeight/2-100, 500, 200); setResizable(false); setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); }// windowInitial /** * Set dialog properties* @param diag * @param title * @param txt */ private void setDialog(JDialog diag, String title, 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(title); diag.setBounds(screenWidth/2-225, screenHeight/2-100, 450, 200); diag.setLayout(null); diag.add(diagBut); diag.add(diagLabel); }// setDialog /** * Set the action when clicking the window close button*/ private void setWindowListener() { this.addWindowListener(new WindowListener() { @Override public void windowOpened(WindowEvent e) {return;} @Override public void windowIconified(WindowEvent e) {return;} @Override public void windowDeiconized(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 windowActivated(WindowEvent e) {return;} @Override public void windowClosing(WindowEvent e) { if(!chooseFlag) { String labelTxt = "Is your default choice /"Yes, I do!/"?"; // Modify new HappyValentinsDay(labelTxt, "NO", "YES"); }// if }// windowClosing });// WindowListener }// setWindowListener public static void main(String[] args) { HappyValentinsDay myApp = new HappyValentinsDay(); myApp.setVisible(true); }// main }/*HappyValentinsDay*/ The above is all the content of this article. I hope it will be helpful to everyone's study. I also hope that everyone will support Wulin.com and wish you a happy Valentine's Day.