15.JDialog弹窗
JDialog,用来被弹出,默认就有关闭事件
1 package com.gui.lesson4; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 8 //主窗口 9 public class DialogDemo extends JFrame { 10 11 public DialogDemo() { 12 this.setVisible(true); 13 this.setSize(700, 500); 14 this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 15 16 //JFrame 放东西,容器 17 Container container = this.getContentPane(); 18 //绝对布局 19 container.setLayout(null); 20 21 //按钮 22 JButton button = new JButton("点击弹出一个对话框"); 23 button.setBounds(30, 30, 200, 50); 24 25 //点击这个按钮的时候,弹出一个弹窗 26 button.addActionListener(new ActionListener() {//监听器 27 @Override 28 public void actionPerformed(ActionEvent e) { 29 //弹窗 30 new MyDialogDemo(); 31 } 32 }); 33 container.add(button); 34 } 35 36 public static void main(String[] args) { 37 new DialogDemo(); 38 } 39 } 40 41 //弹窗的窗口 42 class MyDialogDemo extends JDialog { 43 public MyDialogDemo() { 44 this.setVisible(true); 45 this.setBounds(100, 100, 500, 500); 46 //this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 47 48 Container container = this.getContentPane(); 49 container.add(new JLabel("Hello,Java!",SwingConstants.CENTER)); 50 } 51 }