3.2 弹窗
package com.GGp.lesson04;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class DialogDemo extends JFrame{
//主窗口
public DialogDemo() {
setVisible(true);
setBounds(200,200,500,500);
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
//设置容器
Container contentPane = getContentPane();
contentPane.setBackground(Color.blue);
//绝对定位 位置需要x,y坐标
contentPane.setLayout(null);
//按钮创建
JButton button = new JButton("点击跳出弹框");
button.setBounds(30,30,200,100);//绝对定位,相对于容器
//设置按钮监听事件
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
new MyDialog();//点击按钮后,运行弹窗类
}
});
contentPane.add(button);
}
public static void main(String[] args) {
new DialogDemo();
}
}
//弹窗
class MyDialog extends JDialog {
public MyDialog() {
//设置弹窗 和主窗口相似
//但不用设置关闭功能,因为弹窗自带关闭功能
setVisible(true);
setBounds(300,300,400,400);
//容器
Container contentPane = getContentPane();
contentPane.setBackground(Color.gray);
JLabel jLabel = new JLabel("欢迎学习Java相关系列,加油!!!");
jLabel.setHorizontalAlignment(SwingConstants.CENTER);//设置标签居中
contentPane.add(jLabel);
}
}