[Using AWT to develop GUI programs] Using modal dialog boxes and non-modal dialog boxes
package com.hk.client.login;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
/**
* 使用模式对话框和非模式对话框
*
* - 模式对话框:要求用户在继续所有者窗口之前完成并关闭。 这些对话框最适合用于需要完成的关键任务或不频繁的一次性任务,然后才能继续。
* - 无模式对话框:允许用户根据需要在对话框和所有者窗口之间切换。 这些对话框最适合用于频繁、重复、正在进行的任务。
*
* @author: shipleyleo
* @create: 2023-06-05 17:51:57
*/
public class YongDialog {
Frame f = new Frame("Test");
Dialog d1 = new Dialog(f, "Modal Dialog", true); // 模式对话框(优先的窗口在未处理完成时,不能退出,不能切换窗口,在软件中经常遇到。切换时会有windows提示音。 )
Dialog d2 = new Dialog(f, "Modeless Dialog", false); // 非模式对话框
Button b1 = new Button("Open Modal Dialog"); // 打开模式对话框
Button b2 = new Button("Open Modeless Dialog"); // 打开非模式对话框
public void init() {
d1.setBounds(20, 30, 300, 400);
d2.setBounds(20, 30, 300, 400);
b2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d2.setVisible(true);
}
});
b1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
d1.setVisible(true);
}
});
f.add(b1);
f.add(b2, BorderLayout.SOUTH);
// f.pack();
f.setBounds(100, 100, 450, 450);
f.setVisible(true);
}
public static void main(String[] args) {
new YongDialog().init();
}
}
Output effect:
Open the Modal Dialog, as shown in the following figure:
Open the modeless dialog box (Modeless Dialog), as shown in the following figure: