弹窗(Dialog)的使用


弹窗窗口的建立:

 1 //弹窗的窗口
 2 class MyDialogDemo extends JDialog{
 3     public MyDialogDemo() {
 4         this.setVisible(true);
 5         this.setBounds(100,100,500,500);
 6         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
 7         
 8         Container container = this.getContentPane();
 9         container.setLayout(null);
10         
11         container.add(new Label("1234"));
12     }
13 }
 1 //主窗口
 2 public class DialogDemo extends JFrame {
 3     public static void main(String[] args) {
 4         new DialogDemo();
 5     }
 6     
 7     public DialogDemo() {
 8         this.setVisible(true);
 9         this.setSize(700,500);
10         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
11         
12         //JFrame 放东西,容器
13         Container container = this.getContentPane();
14         //绝对布局
15         container.setLayout(null);
16         
17         //按钮
18         JButton button = new JButton("点击弹出一个对话框");
19         button.setBounds(30,30,200,50);
20         
21         //点击这个按钮时候,弹出一个弹窗
22         button.addActionListener(new ActionListener() {
23             public void actionPerformed(ActionEvent e) {
24                 new MyDialogDemo();
25             }
26         });
27         
28         container.add(button);
29     }
30 }

相关