JFrame之弹窗

package com.deng.lesson04;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

//主窗口
public class DialogDemo extends JFrame {

    public void init(){
        this.setVisible(true);
        this.setBounds(100,100,200,200);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        //JFrame 容器,放东西
        Container container = getContentPane();

        //绝对布局
        container.setLayout(null);

        //按钮
        JButton jButton = new JButton("点击弹出窗口");
        jButton.setBounds(50,50,200,50);

        //点击这个按钮弹出一个弹窗
        jButton.addActionListener(new ActionListener() { //监听器
            @Override
            public void actionPerformed(ActionEvent e) {
                new MyDialog();
            }
        });

        //添加按钮
        container.add(jButton);

    }


    public static void main(String[] args) {
        new DialogDemo().init();
    }
}

//弹出窗口
class MyDialog extends JDialog{
    public MyDialog() {
        setVisible(true);
        setBounds(100,100,200,200);
        //setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); 弹出窗口自带关闭

        Container container = getContentPane();
        container.setLayout(null);

        JLabel jLabel = new JLabel("阿斯顿马丁--超级跑车");
        jLabel.setSize(200,100);
        container.add(jLabel);
    }
}

这是一个简单的弹窗程序。


版权声明:本文为dty2465449331原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。