GUI编程——弹窗

弹窗

JDialog , 用来被弹出,默认就有关闭事件!

 

 1 package com.kuang.lesson04;
 2 
 3 import javax.swing.*;
 4 import java.awt.*;
 5 import java.awt.event.ActionEvent;
 6 import java.awt.event.ActionListener;
 7 
 8 public class DialogDemo extends JFrame {
 9     public DialogDemo() {
10         this.setVisible(true);
11         this.setSize(700, 500);
12         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
13 
14         //JFrame
15         Container container = this.getContentPane();
16         //绝对布局
17         container.setLayout(null);
18 
19         //按钮
20         JButton button = new JButton("点击弹出一个");
21         button.setBounds(30, 30, 200, 50);
22 
23         //点击这个按钮时,弹出一个弹窗
24         button.addActionListener(new ActionListener() {
25             @Override
26             public void actionPerformed(ActionEvent e) {
27                 //弹窗
28                 new MyDialogDemo();
29             }
30         });
31         container.add(button);
32     }
33         public static void main (String[] args){
34             new DialogDemo();
35         }
36     }
37 
38     //弹窗的窗口
39     class MyDialogDemo extends JDialog {
40         public MyDialogDemo() {
41             this.setVisible(true);
42             this.setBounds(100,100,500,500);
43             //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
44 
45             Container container=this.getContentPane();
46             container.setLayout(null);
47 
48             container.add(new Label("欢迎学习Java!"));
49 
50         }
51     }

 

posted @ 2021-04-13 21:35  cengxuyuan  阅读(137)  评论(0)    收藏  举报