下面是一个使用Swing编写的简单的表白程序示例:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ConfessionProgram extends JFrame {

    private JLabel label;
    private JButton button;

    public ConfessionProgram() {
        setTitle("表白程序");
        setSize(300, 200);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        label = new JLabel("我喜欢你,愿意做你的男(女)朋友吗?");
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setFont(new Font("微软雅黑", Font.BOLD, 16));
        add(label, BorderLayout.CENTER);

        button = new JButton("确定");
        button.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JOptionPane.showMessageDialog(null, "谢谢你的答复!");
                dispose();
            }
        });
        add(button, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ConfessionProgram().setVisible(true);
            }
        });
    }
}

这个程序创建了一个窗口,窗口中央显示了一个标签,内容为“我喜欢你,愿意做你的男(女)朋友吗?”,窗口底部有一个按钮,点击按钮后会弹出一个提示框,显示答复信息,并关闭程序

用swing写一个表白程序

原文地址: https://www.cveoy.top/t/topic/h6wv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录