0
点赞
收藏
分享

微信扫一扫

【Java AWT 图形界面编程】Dialog 对话框 ( 简介 | 模式对话框 | 非模式对话框 | Dialog 构造函数 | Dialog 代码示例 | 向 Dialog 对话框添加布局组件 )


文章目录

  • ​​一、Dialog 对话框简介​​
  • ​​二、Dialog 构造函数​​
  • ​​三、Dialog 对话框代码示例​​
  • ​​四、向 Dialog 对话框添加布局组件​​






一、Dialog 对话框简介


Dialog 对话框 是 Window 的子类 , 在 AWT 图形界面编程 中 , 最常见的 三种 Container 容器就是 Frame , Dialog , Panel ;



Dialog 对话框 需要 依赖一个 Frame 窗口 , 该 Frame 窗口就是该对话框的父窗口



Dialog 对话框有两种模式 :

  • 非模式 : 对话框 与 窗口 是 相对独立的 , 互不影响 ;
  • 模式 : 对话框总是位于 父窗口 上面 , 对话框没有关闭时 , 父窗口无法操作 ;


Dialog 与 Window 的关系如下图 , Window 类有 2 个子类 , Frame 窗口类 和 Dialog 对话框类 ;

【Java AWT 图形界面编程】Dialog 对话框 ( 简介 | 模式对话框 | 非模式对话框 | Dialog 构造函数 | Dialog 代码示例 | 向 Dialog 对话框添加布局组件 )_Dialog






二、Dialog 构造函数


Dialog 构造函数 原型 :

  • Frame owner 参数 : 是 Dialog 对话框 依赖的父窗口
  • String title 参数 : Dialog 对话框的 标题 ;
  • boolean modal 参数 : 设置对话框是 模式 还是非模式 , true 为模式 抢占父窗口焦点 , false 为非模式 与 父窗口独立操作 ;

public Dialog(Frame owner, String title, boolean modal) {
this(owner, title, modal ? DEFAULT_MODALITY_TYPE : Dialog.ModalityType.MODELESS);
}

Dialog 构造函数 原型 文档 :

/**
* Constructs an initially invisible <code>Dialog</code> with the
* specified owner <code>Frame</code>, title and modality.
*
* @param owner the owner of the dialog or <code>null</code> if
* this dialog has no owner
* @param title the title of the dialog or <code>null</code> if this dialog
* has no title
* @param modal specifies whether dialog blocks user input to other top-level
* windows when shown. If <code>false</code>, the dialog is <code>MODELESS</code>;
* if <code>true</code>, the modality type property is set to
* <code>DEFAULT_MODALITY_TYPE</code>
* @exception java.lang.IllegalArgumentException if the <code>owner</code>'s
* <code>GraphicsConfiguration</code> is not from a screen device
* @exception HeadlessException when
* <code>GraphicsEnvironment.isHeadless()</code> returns <code>true</code>
*
* 构造一个初始不可见的对话框与指定的所有者框架,标题和模式。
* @param owner对话框的所有者,如果这个对话框没有所有者,则为空
* @param title对话框的标题,如果对话框没有标题,则为空
* @param modal指定对话框显示时是否阻止用户输入到其他顶级窗口。如果为false,则对话框为MODELESS;
* 如果为真,则modality类型属性设置为DEFAULT_MODALITY_TYPE
* @exception java.lang.IllegalArgumentException如果所有者
* GraphicsConfiguration不是来自屏幕设备
* 当GraphicsEnvironment.isHeadless()返回true时,@exception HeadlessException异常
*
* @see java.awt.Dialog.ModalityType
* @see java.awt.Dialog.ModalityType#MODELESS
* @see java.awt.Dialog#DEFAULT_MODALITY_TYPE
* @see java.awt.Dialog#setModal
* @see java.awt.Dialog#setModalityType
* @see java.awt.GraphicsEnvironment#isHeadless
* @see Component#setSize
* @see Component#setVisible
*/
public Dialog(Frame owner, String title, boolean modal) {
this(owner, title, modal ? DEFAULT_MODALITY_TYPE : Dialog.ModalityType.MODELESS);
}






三、Dialog 对话框代码示例


要想显示 Dialog 对话框 , 执行下面 3 个步骤操作即可 :

  • 首先 , 创建 Dialog 对话框 ;
  • 然后 , 设置 Dialog 对话框 位置 和 大小 ;
  • 最后 , 设置 Dialog 对话框 可见 ;

// 1. 创建非模式对话框
Dialog dialog = new Dialog(frame, "对话框", false);
// 2. 设置对话框位置及大小
dialog.setBounds(100, 100, 400, 200);
// 3. 设置对话框可见
dialog.setVisible(true);



代码示例 :

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

public class HelloAWT {
public static void main(String[] args) {
Frame frame = new Frame("AWT 图形界面编程");

// 创建非模式对话框
Dialog dialog = new Dialog(frame, "对话框", false);
// 设置对话框位置及大小
dialog.setBounds(100, 100, 400, 200);

// 设置打开对话框按钮
Button button = new Button("打开对话框");
frame.add(button);

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);
}
});

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

执行结果 :

【Java AWT 图形界面编程】Dialog 对话框 ( 简介 | 模式对话框 | 非模式对话框 | Dialog 构造函数 | Dialog 代码示例 | 向 Dialog 对话框添加布局组件 )_对话框_02






四、向 Dialog 对话框添加布局组件


将 ​​【Java AWT 图形界面编程】Frame 窗口标题栏大小问题 ( Container 容器的空白边框 Insets | 通过调用 frame.getInsets().top 获取窗口标题栏高度 )​​ 博客中的布局组件放到对话框中 ;



在第一章已经提到 Dialog 是 Window 的子类 , Dialog 也是 Container 容器的一种 , 可以设置布局管理器 , 可以向其中添加子组件 ;



代码示例 :

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

public class HelloAWT {
public static void main(String[] args) {
Frame frame = new Frame("AWT 图形界面编程");

// 创建非模式对话框
Dialog dialog = new Dialog(frame, "对话框", false);
dialog.setLayout(null);
// 设置对话框位置及大小
dialog.setBounds(100, 100, 300, 331);

// 设置 5 个布局, 分别在 4 个角和 中心位置显示

// 绘制左上角布局
Panel panel1 = new Panel();
panel1.setBackground(Color.BLUE);
panel1.setBounds(0, 31, 100, 100);
dialog.add(panel1);

// 绘制右上角布局
Panel panel2 = new Panel();
panel2.setBackground(Color.RED);
panel2.setBounds(200, 31, 100, 100);
dialog.add(panel2);

// 绘制左下角布局
Panel panel3 = new Panel();
panel3.setBackground(Color.BLACK);
panel3.setBounds(0, 231, 100, 100);
dialog.add(panel3);

// 绘制右下角布局
Panel panel4 = new Panel();
panel4.setBackground(Color.GREEN);
panel4.setBounds(200, 231, 100, 100);
dialog.add(panel4);

// 绘制中间布局
Panel panel5 = new Panel();
panel5.setBackground(Color.MAGENTA);
panel5.setBounds(100, 131, 100, 100);
dialog.add(panel5);


// 设置打开对话框按钮
Button button = new Button("打开对话框");
frame.add(button);

button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
dialog.setVisible(true);
}
});

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

执行结果 :

【Java AWT 图形界面编程】Dialog 对话框 ( 简介 | 模式对话框 | 非模式对话框 | Dialog 构造函数 | Dialog 代码示例 | 向 Dialog 对话框添加布局组件 )_AWT_03


举报

相关推荐

0 条评论