为什么 JFrame 最初需要 getContentPane() 来添加组件

发布于 2025-01-03 06:12:36 字数 318 浏览 2 评论 0原文

我知道,从 Java 1.5 开始,可以添加JFrame 的组件如下所示:

myFrame.add(myButton);

而不是:

myFrame.getContentPane().add(myButton);

为什么情况并非总是如此?

I know that, as of Java 1.5, one can add a component to a JFrame like this:

myFrame.add(myButton);

instead of:

myFrame.getContentPane().add(myButton);

Why wasn't this always the case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

请爱~陌生人 2025-01-10 06:12:36

正如 JFrame API 中所述,两者都做同样的事情:向 contentPane 添加一个组件。最近(也许是 Java 1.5?)Swing 添加了语法糖/便捷方法,允许您直接在 JFrame(或任何其他 Swing 顶级容器)上进行此调用,但您仍在添加到 contentPane。对于 remove(...)setLayout(...) 也是如此,如果您尝试通过 myJFrame 设置 JFrame 的背景颜色,这一切就会变得非常清楚.setBackground(Color.green); 没有任何反应。正是因为这个原因,我对这个改变不太满意。那也是因为我一定是个脾气暴躁的老家伙。

As is spelled out in the JFrame API, both do the same thing: add a component to the contentPane. It's just recently (Java 1.5 perhaps?) Swing added the syntactic sugar/convenience method to allow you to make this call directly on the JFrame (or any other Swing top-level container), but you're still adding to the contentPane. Same for remove(...) and setLayout(...) This becomes all too clear if you try to set the background color of the JFrame via myJFrame.setBackground(Color.green); and nothing happens. It is for this reason, I'm not too happy with this change. That and also because I must be an old curmudgeon.

自此以后,行同陌路 2025-01-10 06:12:36

4753342:Swing 的顶级组件应重定向添加/删除
ContentPane 的方法

说明:

与 AWT 编程相反,
JFrame/JDialg/JWindow/JApplet/JInternalFrame 不允许您添加
它的 Component,相反,您必须了解 JRootPane 并添加
它的子组件。这给新的内容增加了不必要的混乱
开发人员。

5.0 之前,尝试从其中之一添加或删除 Component
这些顶级组件将导致抛出异常。在
5.0 中,不会抛出异常,而是会在内容窗格中添加或删除 Component。这导致了多次修订
JFrameJDialogJWindowJApplet 的 javadoc 和
JInternalFrame。这已在 RootPaneContainer 中进行了总结
javadoc:

<前><代码> * 为了方便
* JFrameJDialogJWindow
* <代码>JApplet和 JInternalFrame,默认情况下,
* 将所有呼叫转发至 add;及其变体,
* <代码>删除和 setLayout
* <代码>contentPane。这意味着而不是写:
* <预>;
* rootPaneContainer.getContentPane().add(组件);
*
* 你可以这样做:
* <预>;
* rootPaneContainer.add(组件);
*
*

* add 的行为及其变体和
* setLayout;为了
* JFrameJDialogJWindow
* <代码>JApplet和JInternalFrame是由控制
* <代码>rootPaneCheckingEnabled财产。如果这个属性是
* true,默认,然后add;及其变体和
* setLayout;是
* 转发到contentPane,如果为false,那么这些
* 方法直接在 RootPaneContainer 上操作。这
* 属性仅适用于子类,因此受到保护。

另外,这里有一个相关的错误:

4753342: Swing's top level component should redirect add/remove
methods to ContentPane

Description:

Contrary to AWT programming,
JFrame/JDialg/JWindow/JApplet/JInternalFrame do not allow you to add
Components to it, instead you must learn about JRootPane and add
children Components to it. This adds needless confusion to new
developers.

Prior to 5.0, attempting to add or remove a Component from one of
these top level Components would result in an exception be thrown. In
5.0, no exception will be thrown, instead the Component will be added or removed from the content pane. This resulted in several revisions
to the javadoc of JFrame, JDialog, JWindow, JApplet and
JInternalFrame. This has been summarized in RootPaneContainer's
javadoc:

 * For conveniance
 * <code>JFrame</code>, <code>JDialog</code>, <code>JWindow</code>,
 * <code>JApplet</code> and <code>JInternalFrame</code>, by default,
 * forward all calls to <code>add</code> and its variants,
 * <code>remove</code> and <code>setLayout</code> to the
 * <code>contentPane</code>. This means rather than writing:
 * <pre>
 * rootPaneContainer.getContentPane().add(component);
 * </pre>
 * you can do:
 * <pre>
 * rootPaneContainer.add(component);
 * </pre>
 * <p>
 * The behavior of <code>add</code> and its variants and
 * <code>setLayout</code> for
 * <code>JFrame</code>, <code>JDialog</code>, <code>JWindow</code>,
 * <code>JApplet</code> and <code>JInternalFrame</code> is controlled by
 * the <code>rootPaneCheckingEnabled</code> property. If this property is
 * true, the default, then <code>add</code> and its variants and
 * <code>setLayout</code> are
 * forwarded to the <code>contentPane</code>, if it is false, then these
 * methods operate directly on the <code>RootPaneContainer</code>. This
 * property is only intended for subclasses, and is therefor protected.

Also, here's a related bug:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文