java JFrame java中setContentPane()、doLayout()和setLayout()的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14174310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between setContentPane(), doLayout() & setLayout() in a JFrame java
提问by Joseph Farrugia
Can someone explain to me the following because I'm confused which to use & when: setContentPane(), doLayout()& setLayout()in a JFrame java.
有人可以向我解释以下内容,因为我很困惑在 JFrame java 中使用哪个和何时:setContentPane()、doLayout()和setLayout()。
Another query: Am I correct if I say that when using JFrame & other swing containers other than JPanel there is no need to set/create a content Pane manually but in others I'm required to set it manually ?
另一个问题:如果我说在使用 JFrame 和除 JPanel 以外的其他 Swing 容器时不需要手动设置/创建内容窗格,但在其他情况下我需要手动设置它,我是否正确?
Thanks in advance
提前致谢
回答by Daniel Alder
setContentPane is used to set the top-level visual element inside a Window while setLayout sets a layout manager and doLayout invokes the layout manager. Normally doLayout doesn't need to be called because it is called automatically when major properties change, for example elements were added/removed or somthing resized.
setContentPane 用于设置 Window 内的顶级视觉元素,而 setLayout 设置布局管理器,doLayout 调用布局管理器。通常不需要调用 doLayout ,因为它会在主要属性更改时自动调用,例如添加/删除元素或调整大小。
I don't see any reason to be confused with these functions:
我认为没有任何理由与这些功能混淆:
If you want to add visual elements to your frame, use
setContentPane(component)
(oradd(component)
).If you want to set a Layout Manager (like
GridBagLayout
orFlowLayout
), usesetLayout(layoutmanager)
.If you have changed something and your frame looks strange, call
doLayout()
.
如果要将视觉元素添加到框架中,请使用
setContentPane(component)
(或add(component)
)。如果要设置布局管理器(如
GridBagLayout
或FlowLayout
),请使用setLayout(layoutmanager)
.如果您更改了某些内容并且您的框架看起来很奇怪,请调用
doLayout()
。
回答by David Kroukamp
As per java docs:
根据java文档:
Sets the LayoutManager. Overridden to conditionally forward the call to the contentPane.
设置布局管理器。重写以有条件地将调用转发到 contentPane。
So basically it allows us to set the LayoutManager
of our JFrame
s contentPane, as calling setLayout(..)
on JFrame
will be forwarded to contentpane.
所以基本上它允许我们设置LayoutManager
我们的JFrame
小号的contentPane,作为调用setLayout(..)
上JFrame
会被转发到contentPane中。
Sets the contentPane property. This method is called by the constructor. Swing's painting architecture requires an opaque JComponent in the containment hiearchy. This is typically provided by the content pane. If you replace the content pane it is recommended you replace it with an opaque JComponent.
设置 contentPane 属性。该方法由构造函数调用。Swing 的绘画架构要求包含层次结构中的不透明 JComponent。这通常由内容窗格提供。如果您替换内容窗格,建议您将其替换为不透明的 JComponent。
This refers to the main component added to our JFrame
(and spans the JFrame
s entire size), this is where all other components will be added on calls to add(..)
on JFrame
instance.
这是指添加到我们的主要成分JFrame
(和跨度JFrame
的整个大小),这是在所有其他部件将在通话将被添加add(..)
的JFrame
实例。
Causes this container to lay out its components. Most programs should not call this method directly, but should invoke the validate method instead.
使此容器布置其组件。大多数程序不应直接调用此方法,而应调用validate 方法。
This will cause the visual changes to be shown when we changed the LayoutManager
of a container, but as we can see we should not call this and rather use validate()
/revalidate()
followed by repaint()
to make visual changes from size, re-layout and/or components added/removed froma container, visible.
这将导致在我们更改LayoutManager
容器时显示视觉变化,但正如我们所见,我们不应该调用它,而是使用validate()
/revalidate()
后跟从repaint()
大小、重新布局和/或添加/删除组件进行视觉更改从容器中,可见。
As for:
至于:
Am I correct if I say that when using JFrame & other swing containers other than JPanel there is no need to set/create a content Pane manually but in others I'm required to set it manually ?
如果我说在使用 JFrame 和除 JPanel 以外的其他摆动容器时不需要手动设置/创建内容窗格,但在其他情况下我需要手动设置它,我是否正确?
No, this applies to JPanel
too.
不,这也适用JPanel
。
A JPanel
is a Container
, JFrame uses setContentPane
, which allows us to set a container, thus JPanel
/JComponent
extendsConatiner
so that it can be added as a contentPane. hence its exposure to the setContentPane(..)
method.
A JPanel
by default has a content pane or how else would we add components to it?
A JPanel
is a Container
,JFrame 使用setContentPane
,它允许我们设置一个容器,因此JPanel
/ JComponent
extendsConatiner
以便它可以添加为contentPane。因此它暴露于该setContentPane(..)
方法。JPanel
默认情况下,A有一个内容窗格,否则我们将如何向其中添加组件?
So thus no we dont have to manually create JPanel
contentPane.
因此,我们不必手动创建JPanel
contentPane。