用jframe写一个表白
以下是使用JFrame编写的一个简单的表白程序:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Confession extends JFrame {
private JLabel label;
private JButton button;
public Confession() {
setTitle("表白");
setSize(400, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
label = new JLabel("我喜欢你,愿意做你的男朋友吗?");
label.setFont(new Font("微软雅黑", Font.BOLD, 20));
label.setHorizontalAlignment(SwingConstants.CENTER);
add(label, BorderLayout.CENTER);
button = new JButton("接受");
button.setFont(new Font("微软雅黑", Font.PLAIN, 16));
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "太好了!我们在一起吧!");
}
});
add(button, BorderLayout.SOUTH);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
new Confession().setVisible(true);
}
});
}
}
这个程序创建了一个JFrame窗口,显示了一个标签和一个按钮。标签中显示了一段表白的话,按钮上显示“接受”。当点击按钮时,会弹出一个对话框显示“太好了!我们在一起吧!”。你可以根据需要修改标签中的表白内容
原文地址: https://www.cveoy.top/t/topic/h6vw 著作权归作者所有。请勿转载和采集!