GUI-Swing-弹窗

弹窗

制作一个弹窗

方法:

 1 package com.luckylu.gui;
 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     //调用构造器 alt + insert
10     public DialogDemo(){  // 初始值
11         this.setVisible(true);  //可见
12         this.setTitle("主界面");
13         this.setBounds(300,300,700,500);  //设置大小
14         this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  // 关闭
15 
16         //JFrame 定义容器,存东西
17         Container container = this.getContentPane();
18         //绝对布局
19         container.setLayout(null);
20         //按钮
21         JButton button = new JButton("点击弹出对一个话框");  //定义按钮
22         button.setBounds(100,100,200,100); //设置按钮大小及位置
23 
24         //按钮监听,点击按钮时弹窗弹窗;
25         button.addActionListener(new ActionListener() {
26             @Override
27             public void actionPerformed(ActionEvent e) {
28                 //执行弹窗
29                 new MyDialogDemo();
30             }
31         });
32 
33         //把按钮加入容器
34         container.add(button);
35     }
36 
37     public static void main(String[] args) {
38         new DialogDemo();
39     }
40 
41 }
42 class MyDialogDemo extends JDialog{
43     public MyDialogDemo() {
44         this.setVisible(true);
45         this.setTitle("弹窗");
46         this.setBounds(100,100,400,300);
47         //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);  //弹出啊关闭默认执行,不用写
48 
49         Container container = this.getContentPane();
50         container.setLayout(null);
51         Label label = new Label("这是一个弹窗");
52         label.setBounds(20,20,80,50);
53         container.add(label);
54 
55     }
56 }

结果:

 

 常用弹窗网址:

www.layui.com

 

posted @ 2022-04-19 10:56  luckylu1983  阅读(47)  评论(0)    收藏  举报