• Root Pane Container(三)


    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中进行了演示。

  • 相关阅读:
    linux tcp中time_wait
    linux查看系统信息
    运行程序出错无法找到库文件
    安装keepalived
    python 深拷贝与浅拷贝
    python import eventlet包时提示ImportError: cannot import name eventlet
    [原创] 用两个queue实现stack的功能
    [原创] 用两个stack实现queue的功能
    [原创] 编写函数,实现对链表元素的排序与分类
    python 装饰器
  • 原文地址:https://www.cnblogs.com/dyllove98/p/2461902.html
Copyright © 2020-2023  润新知