欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java Swing组件 实现521简单表白界面

程序员文章站 2022-03-30 10:47:44
...

Java Swing组件 实现521简单表白界面

首先GUI界面的代码块为:

package iloveyou;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class MyFrame extends JFrame{

    //设计一个构造方法
    //调用其他方法,直接构造GUI界面
    public MyFrame(){
        this.setOtherAttribute();
        this.addElements();
        this.addListener();
        this.setMyself();
    }

    //创建一个面板对象
    private JPanel mainPanel = new JPanel();
    //创建几个组件
    private JLabel pictureLabel = new JLabel();//用来存放图片
    private JLabel questionLabel = new JLabel("你是不是喜欢我???");//用来存放问题
    private JButton yesButton = new JButton("YES");
    private JButton noButton = new JButton("NO");

    /**
     * 用来处理图片
     * 图片路径 宽度 高度
     * @param path
     * @param width
     * @param height
     * @return
     */
    private ImageIcon drawImage(String path,int width,int height){
        ImageIcon imageIcon = new ImageIcon(path);
        imageIcon.setImage(imageIcon.getImage().getScaledInstance(width,height,Image.SCALE_DEFAULT));
        return imageIcon;
    }

    /**
     * 处理各种组件的状态
     */
    private void setOtherAttribute(){
        //设置一下每个组件的 颜色 边框 字体 图片等等
        //将panel默认的布局方式清除掉
        mainPanel.setLayout(null);
        //设置图片的位置及边框颜色
        pictureLabel.setBounds(40,40,140,200);
        pictureLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        pictureLabel.setIcon(this.drawImage("timg.jpg",140,200));
        //设置问题的位置及边框颜色
        questionLabel.setBounds(220,80,260,40);
        questionLabel.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        questionLabel.setFont(new Font("微软雅黑",Font.BOLD,22));
        questionLabel.setHorizontalAlignment(SwingConstants.CENTER);//设置字体位置
        //设置yes按钮的位置及边框颜色
        yesButton.setBounds(240,160,100,40);
        yesButton.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        yesButton.setFont(new Font("微软雅黑",Font.BOLD,14));
        yesButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        //设置no按钮的位置及边框颜色
        noButton.setBounds(360,160,100,40);
        noButton.setBorder(BorderFactory.createLineBorder(Color.BLUE));
        noButton.setFont(new Font("微软雅黑",Font.BOLD,14));
        noButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
    }

    /**
     * 处理所有组件之间互相添加
     */
    private void addElements(){
        //将所有的组件添加到Panel里面,这个Panel再添加到Frame里
        mainPanel.add(pictureLabel);
        mainPanel.add(questionLabel);
        mainPanel.add(yesButton);
        mainPanel.add(noButton);
        this.add(mainPanel);
    }

    /**
     * 用来给按钮添加事件监听
     */
    private void addListener(){
    	//YES按钮的鼠标监听事件
        yesButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                new ILoveYou();
            }

            public void mouseExited(MouseEvent e) {
                yesButton.setFont(new Font("微软雅黑",Font.BOLD,14));
                yesButton.setForeground(Color.BLACK);
            }
        });
        yesButton.addMouseMotionListener(new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                yesButton.setFont(new Font("微软雅黑",Font.BOLD,16));
                yesButton.setForeground(Color.RED);
            }
        });
		//NO按钮的鼠标监听事件
        noButton.addMouseListener(new MouseAdapter() {
            public void mouseClicked(MouseEvent e) {
                System.out.println("鼠标单击");
                new ILoveYou();
            }

            public void mouseExited(MouseEvent e) {
                noButton.setText("NO");
                noButton.setFont(new Font("微软雅黑",Font.BOLD,16));
                noButton.setForeground(Color.BLACK);
            }
        });
        noButton.addMouseMotionListener(new MouseAdapter() {
            public void mouseMoved(MouseEvent e) {
                noButton.setText("YES");
                noButton.setFont(new Font("微软雅黑",Font.BOLD,16));
                noButton.setForeground(Color.RED);
            }
        });
		//设置关闭时的事件监听
        this.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                int value = JOptionPane.showConfirmDialog(MyFrame.this,"你要把小可爱关掉吗,嘤嘤嘤");
                if (value == 0){
                    JOptionPane.showMessageDialog(MyFrame.this,"关掉就有用吗");
                }else {
                    JOptionPane.showMessageDialog(MyFrame.this,"是不是舍不得我呀");
                }
            }
        });
    }

    /**
     * 处理当前窗体自身的状态
     */
    private  void setMyself(){
        //设置窗体对象的一些属性
        //设置窗体的默认大小
        //左上角的坐标点,窗体的宽度,高度
        this.setBounds(400,300,500,300);
        //设置窗体不可以被随意拖拽大小
        this.setResizable(false);
        //设置窗体单击右上角关闭按钮的时候 不做任何操作
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        //设置可见性
        //需要最后设置,以保证所有组件的可见性
        this.setVisible(true);
    }
}

心形图案坐标的设置:
Java Swing组件 实现521简单表白界面

画心形的代码:

package iloveyou;

import javax.swing.*;
import java.awt.*;

public class ILoveYou extends JFrame {

    private static final int WIDTH = 450;
    private static final int HEIGHT = 450;

    public ILoveYou(){
        this.setBackground(Color.BLACK);//背景色
        this.setBounds(500,500,WIDTH,HEIGHT);//设置窗体自己的大小
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//点击关闭
        this.setVisible(true);//设置窗口可见
    }

    //需要重写paint方法
    public void paint(Graphics g){
        for(int count=1;count<=10;count++){
            for(int r=1;r<=100-count;r++){//控制从小变大
                Image image = this.createImage(WIDTH,HEIGHT);
                Graphics pic = image.getGraphics();
                this.drawHeart(pic,WIDTH/2,HEIGHT/2-200+r,r);
                try {
                    Thread.sleep(3);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //每一次画完后 让当前的心留在这里
                for(int show=1;show<=count;show++){
                    this.drawHeart(pic,WIDTH/2,HEIGHT/2-100-show,100-show);
                }
                pic.drawString("我也喜欢你",WIDTH/2-30,HEIGHT/2);
                g.drawImage(image,0,0,this);
            }
        }
    }

    /**
     * 画心形图案
     * @param pic
     * @param x
     * @param y
     * @param r
     */
    private void  drawHeart(Graphics pic,int x,int y,int r){
        int offset = (int)(r/1.414);//直角边长
        int c1x = x - (r - offset)/2;
        int c1y = y - (r - offset)/2;
        int c2x = c1x - 2 * offset;
        int c2y = c1y + 2 * offset;
        int c3x = x + (r - offset)/2;
        int c3y = y - (r - offset)/2;
        int c4x = c3x + 2 * offset;
        int c4y = c3y + 2 * offset;
        int ex = x;
        int ey = c2y + (x - c2x);
        int leftArcX = c1x - r - offset;
        int leftArcY = c1y - (r - offset);
        int rightArcX = c1x;
        int rightArcY = leftArcY;
        //画图
        pic.setColor(Color.MAGENTA);
        pic.drawLine(x,y,c1x,c1y);//O C1
        pic.drawLine(x,y,c3x,c3y);//O C3
        pic.drawLine(c2x,c2y,ex,ey);//
        pic.drawLine(c4x,c4y,ex,ey);//C4 e
        pic.drawArc(leftArcX,leftArcY,r*2,r*2,45,180);
        pic.drawArc(rightArcX,rightArcY,r*2,r*2,315,180);
    }
}

方法执行程序的入口为(main方法)

package iloveyou;

public class TestMain {
    public static void main(String[] args) {
        new MyFrame();
    }
}

相关标签: Swing