【发布时间】:2012-02-07 01:32:49
【问题描述】:
我知道,as of Java 1.5,可以像这样向 JFrame 添加组件:
myFrame.add(myButton);
代替:
myFrame.getContentPane().add(myButton);
为什么并非总是如此?
【问题讨论】:
-
我问的原因是我在教Intro CS,书中的例子都使用旧的符号。我希望能够给学生一些理由,说明为什么曾经有必要进行额外的步骤。
我知道,as of Java 1.5,可以像这样向 JFrame 添加组件:
myFrame.add(myButton);
代替:
myFrame.getContentPane().add(myButton);
为什么并非总是如此?
【问题讨论】:
正如JFrame API 中所述,两者都做同样的事情:向 contentPane 添加一个组件。就在最近(可能是 Java 1.5?)Swing 添加了语法糖/便利方法以允许您直接在 JFrame(或任何其他 Swing 顶级容器)上进行此调用,但您仍在向 contentPane 添加。 remove(...) 和 setLayout(...) 相同,如果您尝试通过 myJFrame.setBackground(Color.green); 设置 JFrame 的背景颜色并且没有任何反应,这将变得非常清楚。正因为如此,我对这种变化不太满意。那也是因为我一定是个老脾气。
【讨论】:
4753342:Swing 的顶级组件应该重定向添加/删除 ContentPane 的方法
说明:与 AWT 编程相反,
JFrame/JDialg/JWindow/JApplet/JInternalFrame不允许你添加Components 给它,而不是你必须了解JRootPane并添加 孩子们Components 给它。这给新的 开发人员。在 5.0 之前,尝试从其中之一添加或删除
Component这些顶级Components 将导致抛出异常。在 5.0,不会抛出异常,而是将在内容窗格中添加或删除Component。这导致了多次修订 到JFrame、JDialog、JWindow、JApplet和JInternalFrame。这已在 RootPaneContainer 的 文档:* 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.
另外,这里有一个相关的错误:
【讨论】: