注意:因为该小程序及其简单,并且我当时也是边学边写的,所以代码写的杂乱无章,朴实无华。我写这篇文章也只是为了纪念一下那一天(2022/11/29),那天我边学边写这个表白代码,写了一天才写完(我是菜鸡,我还在努力哈哈)。然后晚上我就把这个代码打包成了一个.exe程序,并把它发给了我喜欢的女孩,向她表白,很遗憾的是,她拒绝了我……(可能我的真正的那个她,还没来吧)之所以我要纪念那一天,因为那是我第一次对自己喜欢的女孩表白(是我的一次小勇敢哈哈),至于我为什么不线下表白,是因为疫情,大家都被封在寝室出不去。经历了这件事之后,我觉得还是得把心思放学习上!可不能荒废了我的大学生活啊!加油!
废话少说,直接上代码,没有注释哦!嘿嘿!
下面是目录结构,再下面是源码
//这是Confession.java的源码
public class Confession {
public static void main(String[] args) {
MyJFrame myJFrame=new MyJFrame();
}
}
//这是MyJFrame.java的源码
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Random;
public class MyJFrame extends JFrame{
private int n=0;
public MyJFrame() {
Image icon = Toolkit.getDefaultToolkit().getImage("images\\icon.png");
this.setIconImage(icon);//修改窗口logo
this.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
this.setTitle("给XXX的表白");
this.setSize(500, 250);
this.setLocationRelativeTo(null);
this.setLayout(null);
JButton btn1 = new JButton("是");
btn1.setBounds(100, 150, 65, 30);
this.add(btn1);
JButton btn2=new JButton("给老子爬");
btn2.setBounds(300,150,100,30);
this.add(btn2);
JButton btn4=new JButton("关闭");
btn4.setBounds(200,150,65,30);
this.add(btn4);
JLabel jLabel1=new JLabel("你是不是喜欢我!");
jLabel1.setBounds(110,60,200,50);
jLabel1.setFont(new Font("宋体",Font.BOLD,20));
this.add(jLabel1);
JLabel imgJLabel=new JLabel(new ImageIcon("images\\xixi.jpg"));
imgJLabel.setBounds(300,60,80,80);
this.add(imgJLabel);
this.setResizable(false);//修改为窗口不可修改
this.setVisible(true);
final JDialog dialog = new JDialog(this, "消息");
dialog.setSize(400, 200);
dialog.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
dialog.setLayout(null);
final JButton btn3 = new JButton("好呀");
btn3.setBounds(80,100,65,30);
dialog.add(btn3);
final JDialog dialog1=new JDialog(this,"消息");
dialog1.setSize(400,200);
dialog1.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
dialog1.setLayout(null);
JButton btn5=new JButton("不好");
btn5.setBounds(220,100,65,30);
btn5.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
btn5.setLocation(new Random().nextInt(300),new Random().nextInt(120));
}
});
btn5.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Component MyJFrame = null;
JOptionPane.showMessageDialog(MyJFrame, "不好意思,这个按钮不好使哦!");
}
});
dialog.add(btn5);
JButton btn6=new JButton("我也爱你!");
btn6.setBounds(135,100,100,30);
dialog1.add(btn6);
btn1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setLocationRelativeTo(null);
dialog.setModal(true);
JLabel jLabel2=new JLabel("喜欢就直说嘛!那当我女朋友好不好!");
jLabel2.setBounds(0,0,500,50);
jLabel2.setFont(new Font("宋体",Font.BOLD,20));
dialog.add(jLabel2);
JLabel imgJLabel2=new JLabel(new ImageIcon("images\\bang.png"));
imgJLabel2.setBounds(200,20,100,100);
dialog.add(imgJLabel2);
btn3.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog1.setModal(true);
dialog1.setLocationRelativeTo(null);
JLabel jLabel3=new JLabel("爱你哟!");
jLabel3.setBounds(100,30,200,50);
jLabel3.setFont(new Font("黑体",Font.BOLD,20));
dialog1.add(jLabel3);
JLabel imgJLabel3=new JLabel(new ImageIcon("images\\aixin.png"));
imgJLabel3.setBounds(250,30,100,100);
dialog1.add(imgJLabel3);
btn6.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});
dialog1.setVisible(true);
}
});
dialog.setVisible(true);
}
});
btn2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setLocationRelativeTo(null);
dialog.setModal(true);
Component MyJFrame = null;
if(n==0){
JOptionPane.showMessageDialog(MyJFrame, "哼!\n不许再点了!\n再点我生气了!");
}else chageLocation();
n++;
}
});
btn4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
Component MyJFrame = null;
JOptionPane.showMessageDialog(MyJFrame, "咋滴,想关闭?想都不要想!");
}
});
}
public void chageLocation(){
Random rand=new Random();
int i=rand.nextInt(1500);
int j=rand.nextInt(600);
this.setLocation(i,j);
this.setVisible(true);
}
}
这是运行出来的效果,只能说很一般,有点粗糙,毕竟也就那点代码