8.3 JWindow类
JWindow类与JFrame类类似。他使用JRootPane用于组件管理并且实现了RootPaneContainer接口。他是一个无装饰的顶层窗口。
8.3.1 创建JWindow
JWindow类有五个构造函数:
public JWindow()
JWindow window = new JWindow();
public JWindow(Frame owner)
JWindow window = new JWindow(aFrame);
public JWindow(GraphicsConfiguration config)
GraphicsConfiguration gc = ...;
JWindow window = new JWindow(gc);
public JWindow(Window owner)
JWindow window = new JWindow(anotherWindow);
public JWindow(Window owner, GraphicsConfiguration config)
GraphicsConfiguration gc = ...;
JWindow window = new JWindow(anotherWindow, gc);
我们可以不指定父类或是将父类指定为Frame或Window。如果没有指定父类,则会一个不可见的。
8.3.2 JWindow属性
表8-7列出了JWindow的六个属性。这些属性与JFrame属性类似,所不同的是JWindow没有用于默认关闭操作或是菜单栏的属性。
JWindow属性
属性名
|
数据类型
|
访问性
|
accessibleContext
|
AccessibleContext
|
只读
|
contentPane
|
Container
|
读写
|
glassPane
|
Component
|
读写
|
layeredPane
|
JLayeredPane
|
读写
|
layout
|
LayoutManager
|
只写
|
rootPane
|
JRootPane
|
只读
|
8.3.3 处理JWindow事件
JWindow类在JFrame以及Window类之外并没有添加额外的事件处理功能。查看本章前面的“处理JFrame事件”一节可以了解我们可以关联到JWidnow的监听器列表。
8.3.4 扩展JWindow
如果我们需要扩展JWindow,这个类具有两个重要的protected方法:
protected void windowInit()
protected JRootPane createRootPane()
8.4 JDialog类
JDialog类表示用于显示与Frame相关信息的标准弹出窗口。其作用类似于JFrame,其JRootPane包含一个内容面板以及一个可选的JMenuBar,而且他实现了RootPaneContainer与WidnowConstants接口。
8.4.1 创建JDialog
有11个构造函数可以用来创建JDialog窗口:
public JDialog()
JDialog dialog = new JDialog();
public JDialog(Dialog owner)
JDialog dialog = new JDialog(anotherDialog);
public JDialog(Dialog owner, boolean modal)
JDialog dialog = new JDialog(anotherDialog, true);
public JDialog(Dialog owner, String title)
JDialog dialog = new JDialog(anotherDialog, "Hello");
public JDialog(Dialog owner, String title, boolean modal)
JDialog dialog = new JDialog(anotherDialog, "Hello", true);
public JDialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc)
GraphicsConfiguration gc = ...;
JDialog dialog = new JDialog(anotherDialog, "Hello", true, gc);
public JDialog(Frame owner)
JDialog dialog = new JDialog(aFrame);
public JDialog(Frame owner, String windowTitle)
JDialog dialog = new JDialog(aFrame, "Hello");
public JDialog(Frame owner, boolean modal)
JDialog dialog = new JDialog(aFrame, false);
public JDialog(Frame owner, String title, boolean modal)
JDialog dialog = new JDialog(aFrame, "Hello", true);
public JDialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc)
GraphicsConfiguration gc = ...;
JDialog dialog = new JDialog(aFrame, "Hello", true, gc);
注意,我们并不需要手动创建JDialog并进行装配,我们将会发现JOptionPane可以为我们自动创建并填充JDialog。我们将会在第9间探讨JOptionPane组件。
每一个构造函数都允许我们自定义对象拥有者,窗口标题以及弹出模式。当JDialog为模态时,他会阻止到其拥有者及程序其余部分的输入。当JDialog为非模态时,他会允许用户与JDialog以及程序的其余部分进行交互。
小心,为了使得对话框模式在不同的Java版本之间正常工作,我们要避免在JDialog中混合使用重量级的AWT组件以及轻量级的Swing组件。
8.4.2 JDialog属性
除了可以设置的图标,JDialog类具有与JFrame类相同的属性。表8-8中列出了这些八个属性。
JDialog属性
属性名
|
数据类型
|
访问性
|
accessibleContext
|
AccessibleContext
|
只读
|
contentPane
|
Container
|
读写
|
defaultCloseOperation
|
int
|
读写
|
glassPane
|
Component
|
读写
|
jMenuBar
|
JMenuBar
|
读写
|
layeredPane
|
JLayeredPane
|
读写
|
layout
|
LayoutManager
|
只写
|
rootPane
|
JRootPane
|
只读
|
用于指定所使用的默认关闭操作的常量是在前面的表8-6中所显示的WidnowConstants(除了EXIT_ON_CLOSE基本相同)。默认情况下,defaultCloseOperation属性设置为HIDE_ON_CLOSE,这是弹出对话框所要求的默认行为。
与JFrame类似,JDialog也有一个静态的defaultLookAndFeelDecorated属性。这可以控制默认情况下对话框是否由观感进行装饰。
8.4.3 处理JDialog事件
并没有需要我们特殊处理的JDialog事件;其事件处理与JFrame类相同。也许我们需要处理的一件与JDialog相关的事情就是指定当按下Escape按键时关闭对话框。处理这一事件的最简单的方法就是向对话框内的JRootPane里的键盘动作注册一个Escape按键,从而可以使得当按下Escape时JDialog变得不可见。列表8-4演示了这一行为。源码中的大部分重复了JDialog的构造函数。createRootPane()方法将Escape按键映射到自定义的Action。
package swingstudy.ch08;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JRootPane;
import javax.swing.KeyStroke;
public class EscapeDialog extends JDialog {
public EscapeDialog() {
this((Frame)null, false);
}
public EscapeDialog(Frame owner) {
this(owner, false);
}
public EscapeDialog(Frame owner, boolean modal) {
this(owner, null, modal);
}
public EscapeDialog(Frame owner, String title) {
this(owner, title, false);
}
public EscapeDialog(Frame owner, String title, boolean modal) {
super(owner, title, modal);
}
public EscapeDialog(Frame owner, String title, boolean modal, GraphicsConfiguration gc) {
super(owner, title, modal, gc);
}
public EscapeDialog(Dialog owner) {
this(owner, false);
}
public EscapeDialog(Dialog owner, boolean modal) {
this(owner, null, modal);
}
public EscapeDialog(Dialog owner, String title) {
this(owner, title, false);
}
public EscapeDialog(Dialog owner, String title, boolean modal) {
super(owner, title, modal);
}
public EscapeDialog(Dialog owner, String title, boolean modal, GraphicsConfiguration gc) {
super(owner, title, modal, gc);
}
protected JRootPane createRootPane() {
JRootPane rootPane = new JRootPane();
KeyStroke stroke = KeyStroke.getKeyStroke("ESCAPE");
Action actionListener = new AbstractAction() {
public void actionPerformed(ActionEvent event) {
setVisible(false);
}
};
InputMap inputMap = rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
inputMap.put(stroke, "ESCAPE");
rootPane.getActionMap().put("ESCAPE", actionListener);
return rootPane;
}
}
注意,如果我们使用JOptionPane的静态创建方法,则其所创建的JDialog窗口会自动将Escape按键注册为关闭对话框。
8.4.4 扩展JDialog
如果我们需要扩展JDialog,该类具有两个重要的protected方法:
protected void dialogInit()
protected JRootPane createRootPane()
后面的方法在前面的列表8-4中进行了演示。
分享到:
相关推荐
分层介绍java Swing组件,详细说明rootPane,LayeredPane,rootPane,contentPane之间的关系
NavigationPane使用实例 详细介绍了NavigationPane控件使用
前端开源库-source-pane源窗格,实体兼容窗格:源编辑器
解决Vue对Element中的el-tab-pane添加@click事件无效
前端开源库-folder-pane文件夹窗格,实体兼容窗格:文件浏览器
前端开源库-contacts-pane联系人窗格,实体窗格:通讯簿、组和个人的联系人管理器。
前端开源库-meeting-pane会议窗格,实体兼容窗格:会议协作工具
Windows-7-Navigation-Pane-Customizer
Github链接:https://github.com/JamesHoi/pane-fixer-centos 修复Centos图形界面中文乱码,亲测在Centos7.5环境下有效
Property name for specifying skin to be used on the specific root pane. This property can only be installed on a JRootPane and will affect all the controls in that root pane. The value must be an ...
DotnetBar 8.1.0.6教程-NavigationPane 从官方网站下载的教程,是用VB演示的如何使用DotnetBar的,是英文版的。 在C#中用起来一样,但愿对大家有用。
4pane_5.0.orig.tar.gz 4pane_5.0-2.debian.tar.xz 4pane_5.0-2.dsc 4pane_5.0-2_amd64.deb 4pane_5.0-2_i386.deb 4pane_5.0-2_arm64.deb 4pane_5.0-2_mips64el.deb 需要国产服务器操作系统或者服务器操作系统的...
Android Split Pane Layout An Android layout which splits the available space between two child views. An optionally movable bar exists between the children which allows the user to redistribute the ...
Outlook 2003 Style Navigation Pane
Vue分割窗格使用Vue.js的可调拆分窗格安装# yarn (recommend)$ yarn add vue-split-pane# npm$ npm install vue-split-pane --save用法CDN < script src =" https://unpkg.com/vue-split-pane " > </ ...
Vue多分割窗格 基于Vue.js的组件。 提供无限的可调整大小的多窗格支持。 演示版 | | | 安装 npm i vue-multi-split-pane ... onPaneCollapsed ( paneIndex , paneClass , containerClass ) { console . log
FilePane使用教程.docx
Split-Pane React组件,可以垂直或水平嵌套或拆分! 正在安装 npm install react-split-pane # or if you use yarn yarn add react-split-pane # # Example Usage ` ` ` jsx < /SplitPane > < ...
Atom-hey-pane.zip,用于放大活动窗格的Atom包嘿,窗格!,atom是一个用web技术构建的开源文本编辑器。
Pane&Olio Python flask Web应用程序Pane&Olio