什么时候应该在java中使用JFrame.add(组件)和JFrame.getContentPane()。add(component) [英] when should I use JFrame.add(component) and JFrame.getContentPane().add(component) in java
问题描述
他们之间是否存在差异,是否有任何条件应该使用一个而不是另一个?
Is there a difference between them and are there any conditions in which one should be used instead of the other?
推荐答案
根据我从Javadocs的理解,JFrame.add称之为后者。这是一种方便的方法来解决AWT框架和Swings JFrame之间的不兼容问题。
From what I understand from Javadocs, JFrame.add calls the latter. It is a convenience method to get around the incompatibility between AWT's frame and Swings JFrame.
来自 javadocs for JFrame :
JFrame类与Frame略有不兼容。与所有其他
JFC / Swing顶级容器一样,JFrame包含一个JRootPane作为其
唯一子容器。根窗格提供的内容窗格应作为
规则包含JFrame显示的所有非菜单组件。
这与AWT Frame案例不同。作为一个方便的添加和
其变种,删除和setLayout已被覆盖,以根据需要转发到
的contentPane。这意味着你可以写:
The JFrame class is slightly incompatible with Frame. Like all other JFC/Swing top-level containers, a JFrame contains a JRootPane as its only child. The content pane provided by the root pane should, as a rule, contain all the non-menu components displayed by the JFrame. This is different from the AWT Frame case. As a conveniance add and its variants, remove and setLayout have been overridden to forward to the contentPane as necessary. This means you can write:
`frame.add(child);`
孩子将被添加到
contentPane。内容窗格始终为非null。尝试将
设置为null将导致JFrame抛出异常。
默认内容窗格将在其上设置BorderLayout管理器。有关添加,删除和设置JFrame的
LayoutManager的详细信息,请参阅
到RootPaneContainer。
And the child will be added to the contentPane. The content pane will always be non-null. Attempting to set it to null will cause the JFrame to throw an exception. The default content pane will have a BorderLayout manager set on it. Refer to RootPaneContainer for details on adding, removing and setting the LayoutManager of a JFrame.
这篇关于什么时候应该在java中使用JFrame.add(组件)和JFrame.getContentPane()。add(component)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!