JAVA:填充框架的方法。添加()、setContentPane()、getContentPane()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6486857/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 06:20:26  来源:igfitidea点击:

JAVA: Ways to fill a Frame. add(), setContentPane(), getContentPane()

javajframejtoolbar

提问by froehli

I found three ways to fill my JFrame frame = new JFrame("...") createContentPanel returns a JPanel and createToolBar returns a ToolBar.

我找到了三种方法来填充我的 JFrame frame = new JFrame("...") createContentPanel 返回一个 JPanel,createToolBar 返回一个 ToolBar。

frame.add(this.createToolBar(), BorderLayout.PAGE_START); //this works and puts the ToolBar above and the ContentPanel under it<br>
frame.add(this.createContentPanel(), BorderLayout.CENTER);

frame.setContentPane(this.createContentPanel()); //this lets the JToolBar hover over the ContentPanel
frame.getContentPane().add(this.createToolBar()); 

frame.getContentPane().add(this.createContentPanel()); //this only puts the last one into the JFrame
frame.getContentPane().add(this.createToolBar());

And now I am wondering why should i use the getContentPane()/setContentPane() method if i could just use a simple frame.add(...) to fill my frame.

现在我想知道如果我可以使用一个简单的 frame.add(...) 来填充我的框架,我为什么要使用 getContentPane()/setContentPane() 方法。

回答by Hovercraft Full Of Eels

You are right that it doesn't matter which you use (JFrame#add(...)vs. JFrame#getContentPane().add(...)) since they both essentially call the same code, however there will be times in the future when you'll need access to the contentPane itself, such as if you want to change its border, set its background color or determine its dimensions, and so you'll likely use getContentPane() at some point, and thus getting to know it and be familiar with it would be helpful.

你是对的,你使用哪个(JFrame#add(...)vs. JFrame#getContentPane().add(...))并不重要,因为它们本质上都调用相同的代码,但是将来有时你需要访问 contentPane 本身,例如如果你想更改其边框、设置其背景颜色或确定其尺寸,因此您可能会在某个时候使用 getContentPane(),因此了解它并熟悉它会很有帮助。

回答by camickr

//this only puts the last one into the JFrame

//这只会将最后一个放入JFrame

You need to understand how layout managers work. The default content pane is a JPanel that uses a BorderLayout. When you add a component and don't specify a constraint, then it defaults to the CENTER. However you can only has a single component in the center so the layout manager only knows about the last one added. When the layout manager is invoked it sets the size() and location() of that component. The other component has a size of 0, so it is never painted.

您需要了解布局管理器的工作原理。默认内容窗格是使用 BorderLayout 的 JPanel。当您添加一个组件并且不指定约束时,它默认为 CENTER。但是,您只能在中心有一个组件,因此布局管理器只知道添加的最后一个组件。当布局管理器被调用时,它会设置该组件的 size() 和 location()。另一个组件的大小为 0,因此它永远不会被绘制。

回答by mvmn

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html

http://download.oracle.com/javase/1.4.2/docs/api/javax/swing/JFrame.html

Which says:

其中说:

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. For example, to add a child to an AWT frame you'd write:

   frame.add(child);   

However using JFrame you need to add the child to the JFrame's content pane instead:

   frame.getContentPane().add(child);  

The same is true for setting layout managers, removing components, listing children, and so on. All these methods should normally be sent to the content pane instead of the JFrame itself. 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.

JFrame 类与 Frame 稍微不兼容。与所有其他 JFC/Swing 顶级容器一样,JFrame 包含一个 JRootPane 作为其唯一子项。根窗格提供的内容窗格通常应包含 JFrame 显示的所有非菜单组件。这与 AWT Frame 情况不同。例如,要将子项添加到 AWT 框架中,您可以编写:

   frame.add(child);   

但是,使用 JFrame,您需要将子项添加到 JFrame 的内容窗格中:

   frame.getContentPane().add(child);  

设置布局管理器、删除组件、列出子项等也是如此。所有这些方法通常应该发送到内容窗格而不是 JFrame 本身。内容窗格将始终为非空。尝试将其设置为 null 将导致 JFrame 抛出异常。默认内容窗格将设置一个 BorderLayout 管理器。

回答by Alan

In Java 1.6, you can just use the addmethod of JFrame: http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html(It will be delegated to the contentPane.)

在 Java 1.6 中,您可以只使用addJFrame的方法:http: //download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html(它将委托给 contentPane。)