JAVA: Ways to fill a Frame. add(), setContentPane(), getContentPane()(JAVA:填充框架的方法.添加(),setContentPane(),getContentPane())
问题描述
我找到了三种方法来填充我的 JFrame frame = new JFrame("...")createContentPanel 返回一个 JPanel,createToolBar 返回一个 ToolBar.
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());
现在我想知道为什么我应该使用 getContentPane()/setContentPane() 方法,如果我可以使用一个简单的 frame.add(...) 来填充我的框架.
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.
推荐答案
你是对的,你使用哪个并不重要(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.
这篇关于JAVA:填充框架的方法.添加(),setContentPane(),getContentPane()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:JAVA:填充框架的方法.添加(),setContentPane(),getContentPane()
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01