JAVA:填充框架的方法。添加(),设置内容窗格(),获取内容窗格()
我找到了三种方法来填充我的 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());
现在我想知道如果我可以使用简单的frame.add(...) 来填充我的框架,为什么我应该使用 getContentPane()/setContentPane() 方法。
I found three ways to fill my JFrame frame = new JFrame("...")
createContentPanel returns a JPanel and createToolBar returns a 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你是对的,使用哪个并不重要(
JFrame#add(...)
vs.JFrame#getContentPane().add(...)
)因为它们本质上都调用相同的代码,但是将来有时您需要访问 contentPane 本身,例如如果您想更改其边框、设置其背景颜色或确定其尺寸,那么您'可能会在某个时候使用 getContentPane() ,因此了解它并熟悉它会很有帮助。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.您需要了解布局管理器的工作原理。默认内容窗格是使用 BorderLayout 的 JPanel。当您添加组件且未指定约束时,它将默认为 CENTER。但是,您只能在中心放置一个组件,因此布局管理器只知道最后添加的组件。当调用布局管理器时,它会设置该组件的 size() 和 location()。另一个组件的大小为 0,因此永远不会被绘制。
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.
在Java 1.6中,您可以只使用JFrame的
add
方法:http://download.oracle.com/javase/6 /docs/api/javax/swing/JFrame.html
(它将被委托给 contentPane。)
In Java 1.6, you can just use the
add
method of JFrame:http://download.oracle.com/javase/6/docs/api/javax/swing/JFrame.html
(It will be delegated to the contentPane.)
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: