java 窗体 swing 实现表白窗体
没有太多技术东西, 几个点,
- 关闭按钮锁死
- click触发标记
- 触发标记后开始随机移动时间
上代码
package com.vknow.test.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;
/**
* @author lxz
* @version 1.0
* @className ValentinesDay
* @description TODO to
* @Date 2019/8/7 13:49
**/
public class ValentinesDay {
private static boolean fangFlag =false;
public static void main(String[] args) {
//创建主窗口
JFrame jframe = new JFrame();
jframe.setTitle("来自一个猫了你好几年的 喵~");
//背景图片
ImageIcon biuImg = new ImageIcon("src/biu.png");
//创建面板
JLabel imgLabel = new JLabel(biuImg);
imgLabel.setSize(200, 200);
//rightPanel
JPanel rightPanel =new JPanel();
rightPanel.setBounds(450,40,300,300);
JPanel leftPanel = new JPanel();
JLabel one = new JLabel(
"<html><div style='margin-top:-80px;margin-right:50px'><p>猫你好久了</p></div>" +
"<div style='margin-top: 20px'><b><p>你能做我女朋友么</p></b></div></html>"
);
one.setHorizontalAlignment(SwingConstants.LEFT);
leftPanel.add(one);
leftPanel.setLayout(new FlowLayout(FlowLayout.LEFT, 40, 30));
JButton btnY = new JButton("行");
JButton btnN = new JButton("不行");
rightPanel.setLayout(new FlowLayout(FlowLayout.RIGHT,20,20));
rightPanel.add(one);
rightPanel.add(imgLabel);
JPanel southPanel = new JPanel();
southPanel.add(btnY);
southPanel.add(btnN);
southPanel.setLayout(new FlowLayout(FlowLayout.CENTER,50,30));
btnN.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
if (fangFlag) {
Random xRandom = new Random();
int x = xRandom.nextInt(300);
Random yRandom = new Random();
int y = yRandom.nextInt(50);
btnN.setBounds(x,y, btnN.getWidth(), btnN.getHeight());
}else{
int fang = JOptionPane.showConfirmDialog(jframe, "你想好了么劳资可是有房的人?", "", 0);
if (fang==0) {
int che = JOptionPane.showConfirmDialog(jframe, "你可要确定了啊,劳资还有车?", "", 0);
if (che==0) {
JOptionPane.showMessageDialog(jframe,"好吧,你点一下不行就可以退出了!","",0);
fangFlag=true;
}
}
}
}
@Override
public void mouseExited(MouseEvent e) {
}
});
btnY.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
JOptionPane.showMessageDialog(jframe,"同意了就不同反悔了!","恭喜,喜结良缘",
JOptionPane.PLAIN_MESSAGE);
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
});
jframe.add(southPanel, BorderLayout.SOUTH);
jframe.add(rightPanel);
jframe.setBounds(300, 300, 400, 250);
jframe.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
jframe.setVisible(true);
}
}