模块  java.desktop
软件包  javax.swing

Class JRootPane

  • 实现的所有接口
    ImageObserverMenuContainerSerializableAccessible

    public class JRootPane
    extends JComponent
    implements Accessible
    通过在后台使用一个轻量级容器JFrameJDialogJWindowJAppletJInternalFrame 有关根窗格提供的功能的面向任务的信息,请参阅“Java教程” How to Use Root Panes部分。

    下图显示了使用根窗格的类之间的关系。

    以下文字描述了此图形。

    “重量级”组件(委托给对等方的组件或主机系统上的本机组件)显示为更暗,更重的盒子。 四个重量级JFC / Swing容器( JFrameJDialogJWindow ,和JApplet )被示出相对于它们扩展AWT类。 这四个组件是Swing库中唯一的重量级容器。 还示出了轻质容器JInternalFrame 所有这五个JFC / Swing容器都实现了RootPaneContainer接口,它们都将操作委托给JRootPane (顶部带有一个小“句柄”)。
    Note: The JComponent method getRootPane can be used to obtain the JRootPane that contains a given component.
    Example The following text describes this graphic.
    右图显示了JRootPane的结构。 JRootpaneglassPane ,可选menuBarcontentPane JLayeredPane管理menuBarcontentPaneglassPane位于所有内容的顶部,它可以拦截鼠标移动。 由于glassPane (如contentPane )可以是任意组件,因此也可以设置glassPane进行绘图。 然后, glassPane上的线条和图像可以在下面的帧上进行范围调整,而不受其边界的限制。

    虽然menuBar组件是可选的,该layeredPanecontentPaneglassPane始终存在。 尝试将它们设置为null生成异常。

    要将组件添加到JRootPane (可选菜单栏除外),请将对象添加到contentPaneJRootPane ,如下所示:

      rootPane.getContentPane().add(child); 
    同样的原则适用于设置布局管理器,删除组件,列出子项等。所有这些方法都在contentPane上调用,而不是在JRootPaneJRootPane
    Note: The default layout manager for the contentPane is a BorderLayout manager. However, the JRootPane uses a custom LayoutManager. So, when you want to change the layout manager for the components you added to a JRootPane, be sure to use code like this:
        rootPane.getContentPane().setLayout(new BoxLayout());
     
    如果JMenuBar部件被设定在JRootPane ,它沿框的上边缘定位。 contentPane位置和大小进行了调整,以填补剩余的区域。 JMenuBarcontentPane被添加到JLayeredPane.FRAME_CONTENT_LAYER层的layeredPane组件中。)

    layeredPanelayeredPane中所有子项的父JRootPane - 既作为菜单的直接父项,又添加到contentPane的所有组件的祖父contentPane 它是JLayeredPane一个实例,它提供了在多个层添加组件的功能。 使用菜单弹出窗口,对话框和拖动时,此功能非常有用 - 在这种情况下,您需要将组件放在窗格中的所有其他组件之上。

    glassPane位于glassPane中所有其他组件的JRootPane 这为在所有其他组件上方绘制提供了一个方便的位置,并且可以拦截鼠标事件,这对于拖动和绘图都很有用。 开发人员可以使用setVisible上的glassPane来控制glassPane显示在其他子项上的时间。 默认情况下, glassPane不可见。

    自定义LayoutManager通过使用JRootPane确保:

    1. glassPane填充了JRootPane (bounds-insets)的整个可视区域。
    2. layeredPane充满整个可视区域JRootPane (界限 - 插图)
    3. 所述menuBar位于的上边缘layeredPane
    4. contentPane填充整个可视区域,减去menuBar (如果存在)。
    将忽略JRootPane视图层次结构中的任何其他视图。

    如果更换LayoutManagerJRootPane ,则您负责管理所有这些视图。 因此,通常您需要确保更改contentPane的布局管理器而不是JRootPane本身!

    Swing的绘制体系结构要求在所有其他组件上方的包含层次结构中存在不透明的JComponent 这通常通过内容窗格提供。 如果替换内容窗格,建议您通过setOpaque(true)使内容窗格不透明。 此外,如果内容窗格覆盖paintComponent ,则需要在paintComponent中以不透明的颜色完全填充背景。

    警告: Swing不是线程安全的。 有关更多信息,请参阅Swing's Threading Policy

    警告:此类的序列化对象与以后的Swing版本不兼容。 当前的序列化支持适用于运行相同版本Swing的应用程序之间的短期存储或RMI。 从1.4开始, java.beans软件包中添加了对所有JavaBeans java.beans长期存储的支持。 请参阅XMLEncoder

    从以下版本开始:
    1.2
    另请参见:
    JLayeredPaneJMenuBarJWindowJFrameJDialogJAppletJInternalFrameJComponentBoxLayoutMixing Heavy and Light ComponentsSerialized Form
    • 字段详细信息

      • NONE

        public static final int NONE
        用于windowDecorationStyle属性的常量。 表示JRootPane不应提供任何种类的窗口装饰。
        从以下版本开始:
        1.4
        另请参见:
        常数字段值
      • FRAME

        public static final int FRAME
        用于windowDecorationStyle属性的常量。 表示JRootPane应提供适合Frame的装饰。
        从以下版本开始:
        1.4
        另请参见:
        常数字段值
      • PLAIN_DIALOG

        public static final int PLAIN_DIALOG
        用于windowDecorationStyle属性的常量。 表示JRootPane应提供适合Dialog的装饰。
        从以下版本开始:
        1.4
        另请参见:
        常数字段值
      • INFORMATION_DIALOG

        public static final int INFORMATION_DIALOG
        用于windowDecorationStyle属性的常量。 表示JRootPane应提供适合用于显示信息性消息的对话框的装饰。
        从以下版本开始:
        1.4
        另请参见:
        常数字段值
      • ERROR_DIALOG

        public static final int ERROR_DIALOG
        用于windowDecorationStyle属性的常量。 表示JRootPane应提供适合用于显示错误消息的Dialog的装饰。
        从以下版本开始:
        1.4
        另请参见:
        常数字段值
      • COLOR_CHOOSER_DIALOG

        public static final int COLOR_CHOOSER_DIALOG
        用于windowDecorationStyle属性的常量。 表示JRootPane应提供适合用于显示JColorChooser的对话框的JColorChooser
        从以下版本开始:
        1.4
        另请参见:
        常数字段值
      • FILE_CHOOSER_DIALOG

        public static final int FILE_CHOOSER_DIALOG
        用于windowDecorationStyle属性的常量。 表示JRootPane应提供适合用于显示JFileChooser的对话框的JFileChooser
        从以下版本开始:
        1.4
        另请参见:
        常数字段值
      • QUESTION_DIALOG

        public static final int QUESTION_DIALOG
        用于windowDecorationStyle属性的常量。 表示JRootPane应提供适合用于向用户提出问题的Dialog的装饰。
        从以下版本开始:
        1.4
        另请参见:
        常数字段值
      • WARNING_DIALOG

        public static final int WARNING_DIALOG
        用于windowDecorationStyle属性的常量。 表示JRootPane应提供适合用于显示警告消息的对话框的装饰。
        从以下版本开始:
        1.4
        另请参见:
        常数字段值
      • menuBar

        protected JMenuBar menuBar
        菜单栏。
      • contentPane

        protected Container contentPane
        内容窗格。
      • layeredPane

        protected JLayeredPane layeredPane
        用于管理菜单栏和内容窗格的分层窗格。
      • glassPane

        protected Component glassPane
        覆盖菜单栏和内容窗格的玻璃窗格,因此它可以拦截鼠标移动等。
      • defaultButton

        protected JButton defaultButton
        窗格具有焦点时激活的按钮以及按 Enter键的特定于UI的操作。
    • 构造方法详细信息

      • JRootPane

        public JRootPane()
        创建 JRootPane ,建立其 glassPanelayeredPanecontentPane
    • 方法详细信息

      • setDoubleBuffered

        public void setDoubleBuffered​(boolean aFlag)
        设置此组件是否应使用缓冲区进行绘制。 如果设置为true,则此组件中的所有绘图都将在屏幕外绘制缓冲区中完成。 屏幕外的绘画缓冲区将被复制到屏幕上。 如果Component被缓冲并且其祖先之一也被缓冲,则将使用祖先缓冲区。
        重写:
        setDoubleBufferedJComponent
        参数
        aFlag - 如果为true,则将此组件设置为双缓冲
        从以下版本开始:
        1.6
      • getWindowDecorationStyle

        public int getWindowDecorationStyle()
        返回一个常量,标识 JRootPane提供的Window装饰类型。
        结果
        一个 NONEFRAMEPLAIN_DIALOGINFORMATION_DIALOGERROR_DIALOGCOLOR_CHOOSER_DIALOGFILE_CHOOSER_DIALOGQUESTION_DIALOGWARNING_DIALOG
        从以下版本开始:
        1.4
        另请参见:
        setWindowDecorationStyle(int)
      • setWindowDecorationStyle

        @BeanProperty(expert=true,
                      visualUpdate=true,
                      enumerationValues={"JRootPane.NONE","JRootPane.FRAME","JRootPane.PLAIN_DIALOG","JRootPane.INFORMATION_DIALOG","JRootPane.ERROR_DIALOG","JRootPane.COLOR_CHOOSER_DIALOG","JRootPane.FILE_CHOOSER_DIALOG","JRootPane.QUESTION_DIALOG","JRootPane.WARNING_DIALOG"},
                      description="Identifies the type of Window decorations to provide")
        public void setWindowDecorationStyle​(int windowDecorationStyle)
        设置JRootPane应提供的窗口装饰类型(如边框,关闭窗口的小部件,标题...)。 默认设置是不提供Window装饰( NONE )。

        这只是一个暗示,一些外观和感觉可能不支持这一点。 这是一个绑定属性。

        参数
        windowDecorationStyle - 不断识别要提供的窗口装饰。
        异常
        IllegalArgumentException -如果 style不是一个: NONEFRAMEPLAIN_DIALOGINFORMATION_DIALOGERROR_DIALOGCOLOR_CHOOSER_DIALOGFILE_CHOOSER_DIALOGQUESTION_DIALOG ,或 WARNING_DIALOG
        从以下版本开始:
        1.4
        另请参见:
        JDialog.setDefaultLookAndFeelDecorated(boolean)JFrame.setDefaultLookAndFeelDecorated(boolean)LookAndFeel.getSupportsWindowDecorations()
      • getUI

        public RootPaneUI getUI()
        返回呈现此组件的L&F对象。
        重写:
        getUI在类 JComponent
        结果
        LabelUI对象
        从以下版本开始:
        1.3
      • createLayeredPane

        protected JLayeredPane createLayeredPane()
        由构造方法调用以创建默认值layeredPane Bt默认它创建一个新的JLayeredPane
        结果
        默认 layeredPane
      • createContentPane

        protected Container createContentPane()
        由构造函数方法调用以创建默认值contentPane 默认情况下,此方法创建一个新的JComponent添加集BorderLayout作为其LayoutManager
        结果
        默认 contentPane
      • createGlassPane

        protected Component createGlassPane()
        由构造函数方法调用以创建默认值glassPane 默认情况下,此方法会创建一个新的JComponent ,其可见性设置为false。
        结果
        默认 glassPane
      • createRootLayout

        protected LayoutManager createRootLayout()
        由构造函数方法调用以创建默认值 layoutManager
        结果
        默认 layoutManager
      • setJMenuBar

        public void setJMenuBar​(JMenuBar menu)
        添加或更改分层窗格中使用的菜单栏。
        参数
        menu - 要添加的 JMenuBar
      • setMenuBar

        @Deprecated
        public void setMenuBar​(JMenuBar menu)
        Deprecated.
        As of Swing version 1.0.3 replaced by setJMenuBar(JMenuBar menu).
        指定菜单栏值。
        参数
        menu - 要添加的 JMenuBar
      • getJMenuBar

        public JMenuBar getJMenuBar()
        从分层窗格返回菜单栏。
        结果
        窗格中使用的 JMenuBar
      • getMenuBar

        @Deprecated
        public JMenuBar getMenuBar()
        Deprecated.
        As of Swing version 1.0.3 replaced by getJMenuBar().
        返回菜单栏值。
        结果
        窗格中使用的 JMenuBar
      • setContentPane

        public void setContentPane​(Container content)
        设置内容窗格 - 容纳由根窗格作为父级的组件的容器。

        Swing的绘画架构在包含层次结构中需要不透明的JComponent 这通常由内容窗格提供。 如果更换内容窗格,建议您使用不透明的JComponent替换它。

        参数
        content - 用于组件内容的 Container
        异常
        IllegalComponentStateException - (运行时异常),如果内容窗格参数为 null
      • getContentPane

        public Container getContentPane()
        返回内容窗格 - 容纳由根窗格作为父级的组件的容器。
        结果
        保存组件内容的 Container
      • setLayeredPane

        public void setLayeredPane​(JLayeredPane layered)
        设置根窗格的分层窗格。 分层窗格通常包含内容窗格和可选的JMenuBar
        参数
        layered - 使用 JLayeredPane
        异常
        IllegalComponentStateException - (运行时异常),如果分层窗格参数为 null
      • getLayeredPane

        public JLayeredPane getLayeredPane()
        获取根窗格使用的分层窗格。 分层窗格通常包含内容窗格和可选的JMenuBar
        结果
        目前正在使用的 JLayeredPane
      • setGlassPane

        public void setGlassPane​(Component glass)
        将指定的Component设置为此根窗格的玻璃窗格。 玻璃窗格通常应该是一个轻量级的透明组件,因为当根窗格需要抓取输入事件时,它将变为可见。

        新玻璃窗格的可见性将更改为与当前玻璃窗格的可见性相匹配。 这意味着当您想要更换玻璃窗格并使其可见时必须小心。 以下任何一种都可以使用:

          root.setGlassPane(newGlassPane);
           newGlassPane.setVisible(true); 
        要么:
          root.getGlassPane().setVisible(true);
           root.setGlassPane(newGlassPane); 
        参数
        glass - Component的玻璃窗格的 JRootPane
        异常
        NullPointerException - 如果 glass参数是 null
      • isValidateRoot

        public boolean isValidateRoot()
        如果此JRootPane的后代调用revalidate ,请从此处进行验证。

        延迟再次布局组件及其后代的请求。 例如,对revalidate调用会向上推送到JRootPaneJScrollPane因为这两个类都会覆盖isValidateRoot以返回true。

        重写:
        isValidateRoot在类 JComponent
        结果
        真正
        另请参见:
        JComponent.isValidateRoot()Container.isValidateRoot()
      • isOptimizedDrawingEnabled

        public boolean isOptimizedDrawingEnabled()
        glassPanecontentPane具有相同的边界,这意味着JRootPane不会平铺其子JRootPane ,这应该返回false。 另一方面, glassPane通常不可见,因此如果glassPane不可见,则返回true。 因此,此处的返回值取决于glassPane的可见性。
        重写:
        isOptimizedDrawingEnabled在类 JComponent
        结果
        如果此组件的子项不重叠,则为true
      • setDefaultButton

        @BeanProperty(description="The button activated by default in this root pane")
        public void setDefaultButton​(JButton defaultButton)
        设置defaultButton属性,该属性确定此JRootPane的当前默认按钮。 默认按钮是当根窗格中出现UI定义的激活事件(通常是Enter键)时将激活的按钮,无论该按钮是否具有键盘焦点(除非根窗格中有另一个组件消耗激活事件,例如JTextPane )。 要使默认激活起作用,当激活发生时,该按钮必须是根窗格的启用后代。 要从此根窗格中删除默认按钮,请将此属性设置为null
        参数
        defaultButton - JButton ,它是默认按钮
        另请参见:
        JButton.isDefaultButton()
      • paramString

        protected String paramString()
        返回此JRootPane的字符串表示JRootPane 此方法仅用于调试目的,返回字符串的内容和格式可能因实现而异。 返回的字符串可能为空,但可能不是null
        重写:
        paramString在类 JComponent
        结果
        JRootPane的字符串表示 JRootPane
      • getAccessibleContext

        public AccessibleContext getAccessibleContext()
        获取AccessibleContext与这个JRootPane相关联。 为根窗格,所述AccessibleContext需要一个的形式AccessibleJRootPane 如有必要,将创建新的AccessibleJRootPane实例。
        Specified by:
        getAccessibleContext在接口 Accessible
        重写:
        getAccessibleContext在类 Component
        结果
        AccessibleJRootPane ,作为 AccessibleContextJRootPane