Dialog对话框(Java)

效果图显示

1.点击前

2.点击后

 

代码实现 

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

//对话框
public class DialogDemo01 {
    public static void main(String[] args) {
        Frame frame = new Frame();

        //创建两个对话框,一个模式的一个非模式的
        Dialog d1 = new Dialog(frame,"模式对话框",true);
        Dialog d2 = new Dialog(frame,"非模式对话框",false);

        //往d1中添加内容
        Box box = Box.createVerticalBox();
        box.add(new TextField(20));
        box.add(new Button("确认"));
        d1.add(box);

        //设置位置及大小
        d1.setBounds(20,30,300,200);
        d2.setBounds(20,30,300,200);

        //创建两个按钮
        Button b1 = new Button("打开模式对话框");
        Button b2 = new Button("打开非模式对话框");

        //给这两个按钮添加选定后的行为
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                d1.setVisible(true);//显示对话框
            }
        });
        b2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                d2.setVisible(true);//显示对话框
            }
        });

        //把按钮添加到frame中
        frame.add(b1,BorderLayout.NORTH);//北部
        frame.add(b2,BorderLayout.SOUTH);//南部

        frame.pack();
        frame.setVisible(true);
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_54702911/article/details/121756681