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

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

时间:2023-02-04 12:33:47浏览次数:45  
标签:java Dialog 对话框 dialog new 100 构造函数


文章目录

  • ​​一、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 对话框添加布局组件 )_Frame






二、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 对话框添加布局组件 )_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 对话框添加布局组件 )_Dialog_03


标签:java,Dialog,对话框,dialog,new,100,构造函数
From: https://blog.51cto.com/u_14202100/6037104

相关文章

  • java的构造函数和方法之间的区别
    Java构造函数构造器用于初始化对象的状态(数据)。构造函数不能有返回类型。构造函数隐式调用。如果没有指定任何构造函数,java编译器提供一个默认构造函数。构造函数名......
  • JavaScript 中URL构造函数
    前言URL对于我们开发人员来讲,应该是非常熟悉了。在对URL进行参数拼接时,我们一般都会直接进行字符串拼接或使用模版字符串,因为这样非常方便,但是我们这样其实会在不知不觉......
  • Qt | QDialogButtonBox使用示例
    Qt|QDialogButtonBox使用示例1、简介QDialogButtonBox类,该类包含很多按钮控件,在窗体(widget)或者对话框(dialog)有多个按钮的时候,为方便管理就可以使用该类成组进行管理。......
  • dialog 没有居中或者显示不全(小米手机)
    解决办法Windowwindow=dialog.getWindow();//window.setGravity(Gravity.CENTER);WindowManager.LayoutPar......
  • HTML中dialog元素的使用
    1.直接贴代码<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="view......
  • 春哥博客 - 构造函数
    作用:帮助我们初始化对象(给对象的每个属性依次赋值) 先创建对象-然后执行构造函数 构造函数是一个特殊的方法:1)构造函数没有返回值,连void也不能写2)构造函数的名称必......
  • 拷贝构造函数
    ​配合这篇文章食用更佳:构造析构函数拷贝构造函数调用时机使用一个已经创建完毕的对象来初始化一个新对象(复制)值传递的方式给函数参数传值(实参传递给形参的过程,复制)......
  • 实现ElementUI的Dialog弹窗可以拖拽移动
    1、创建自定义指令directive/el-dragDialog/index.jsimportdragfrom'./drag'constinstall=function(Vue){Vue.directive('el-drag-dialog',drag)}if(w......
  • C++成员初始化列表比在构造函数内部赋值效率更高
    A是个类,B中包含A类的对象在执行构造函数的时候,如果内部有类对象,使用列表初始化效率会更高B中的a和b都是A的对象a是用的列表初始化b是在构造函数内部初始化a只会执行一......
  • Windows and Dialogs 窗体 对话框
       <Windowx:Class="WpfTestBlankApp.Views.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http:/......