java 窗體 swing 實作表白窗體
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsICM38FdsYkRGZkRG9lcvx2bjxiNx8VZ6l2cs0TPn1ENrRUT61keNBDOsJGcohVYsR2MMBjVtJWd0ckW65UbM5WOHJWa5kHT20ESjBjUIF2X0hXZ0xCMx81dvRWYoNHLrdEZwZ1Rh5WNXp1bwNjW1ZUba9VZwlHdssmch1mclRXY39CXldWYtlWPzNXZj9mcw1ycz9WL49zZuBnL1cDO2UzMzYTM3ADOwkTMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
沒有太多技術東西, 幾個點,
- 關閉按鈕鎖死
- 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);
}
}