23,407
社区成员
发帖
与我相关
我的任务
分享
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
class MyDialog extends JDialog {
/**
*
*/
private static final long serialVersionUID = 3015530070082861574L;
Container container;
JLabel label;
public MyDialog(JFrame parent, String title) { // 构造函数
super(parent, title);
container = this.getContentPane();
label = new JLabel("This is a Dialog"); // 创建 Label
container.add(label); // 添加 Label
this.setBounds(240, 240, 200, 100); // 设置对话框位置,大小
this.setVisible(true); // 设置对话框可见
}
}
public class HelloJava implements ActionListener {
private JFrame window; // 主窗口
private Container container;
private JLabel label; // Label
private JButton button; // Button
public HelloJava(String title) { // 构造函数
window = new JFrame(title); // 创建窗口对象
label = new JLabel("This is a Swing window"); // 创建 Label 对象
button = new JButton("New Dialog"); // 创建 Button 对象
container = window.getContentPane();
label.setHorizontalAlignment(SwingConstants.CENTER); // 设置 Label 对齐方式
button.setBounds(0, 0, 75, 25); // 设置 button 大小,位置
button.addActionListener(this); // 设置 button 响应
container.add(label);
container.add(button);
container.setBackground(Color.white); // 设置窗口背景色
window.setVisible(true);
window.setBounds(120, 120, 800, 600);
window.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new HelloJava("Swing window");
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new MyDialog(window, "Swing Dialog");
}
}
container.setLayout(null);