JPanel 内容窗格混乱
我正在学习 Java Swing 并在框架中附加了一个菜单栏。默认情况下,这应该调用jframe.getContentPane().add(child)
。当我运行脚本时,菜单栏没有显示。但如果有意义的话,按钮位于最顶部“y=0”。
然后我意识到我的错误,我实际上必须在菜单栏中放入一个菜单。然后菜单栏出现了。这让我开始思考……“菜单栏”“内容窗格”实际上是两个面板吗?这让我很困惑。因为它的作用很像一个面板。但是 getContentPane() 返回一个 Container,而不是 JPanel 对象,所以我很困惑。
如果是这样,这是否意味着直接转储到框架中的唯一内容只是 Jpanel 对象?因此 JButtons、JLabels 并不直接位于框架中...... 这是否意味着 jpanels 是“嵌套的”? 还有一件事让我困惑。如果 jpanel 可以控制事物的定位方式,那么 LayoutManager 有何用处? :S 谢谢,请像回答一个两岁的孩子问为什么天空是蓝色的一样回答,哈;)
I am learning Java Swing and I appended a menuBar to the frame. By default this should call jframe.getContentPane().add(child)
. When I ran the script the menuBar didn't show up. But the button was at the very top "y=0" if that makes sense.
Then I realized my mistake I actually had to put in a menu in the menubar. Then the menuBar showed up. So that got me thinking...is the "menubar" "contentpane" actually 2 panels? It is confusing the hell out of me. Because that acted a lot like a panel. But getContentPane()
returns a Container, not a JPanel object so I'm confused.
If so, does that mean that the only thing that is dumped directly into a frame are just Jpanel objects? Hence JButtons, JLabels are not directly in a frame...
Does that mean, jpanels are "nested"?
One more thing that is confusing me. If a jpanel can control how things are positioned, what is a LayoutManager for? :S
Thanks, and please answer as if to a 2yr old asking why the sky is blue,ha ;)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一些随机的想法:
getContentPane()
返回的类型在技术上是一个 Container,但它也是一个 JPanel(它最终从 Container 继承)。Some random thoughts:
getContentPane()
is technically a Container, it's also a JPanel (which inherits eventually from Container).为此,今天的 Java Swing GUI 有一个方法
,不需要从 Java5 声明
ContentPane
,并且使用 BorderLayout 作为默认LayoutManager
然后
frame.add(myPanel);
//与
frame.add(myPanel, BorderLayout.CENTER)
相同,并占据了整个Container关于如何使用LayourManagers
for that there is method
for todays Java Swing GUI, is not neccesary declare
ContentPane
, from Java5, and with BorderLayout as defaultLayoutManager
then
frame.add(myPanel);
//is same as
frame.add(myPanel, BorderLayout.CENTER)
and occupated whole Containerbasic stuff about how to use LayourManagers
getContentPane()
始终返回一个Container
实例。但是,您应该注意,JPanel
对象是Container
实例,以及 Swing 框架中的其他类。返回实例的实际类是无关紧要的,因为您无法控制将哪个 Container 实现用作 contentPane(除非您强制使用特定的 contentPane),并且大多数情况下这应该不是问题。您可以在
JFrame
中添加许多 GUI 小部件,例如JButton
、JLabel
等。但是,它们会自动添加到关联的 contentPane 中。JPanel
不处理对象定位,与面板关联的LayoutManager
负责;要么自动基于其自己的规则集(例如FlowLayout
),要么使用您在将对象添加到容器时指定的约束(GridBagLayout
布局管理器是一个很好的例子)。 LayoutManager 上的 JavaDoc 通常包含足够的信息来帮助您开始使用它们。你可以有嵌套面板,是的。
Container
可以包含其他Container
实例。虽然这似乎是一个复杂的解决方案,但它确实使您能够准确控制 GUI 的显示方式。根据您使用的 LayoutManager、用户界面必须满足的需求以及您作为开发人员的偏好/习惯,您可能需要更少或更多的嵌套面板。getContentPane()
always returns aContainer
instance. However, you should note thatJPanel
objects areContainer
instances, as well as other classes in the Swing framework. The actual class of the instance returned is irrelevant, as you do not have control over which implementation ofContainer
is used as a contentPane (unless you have forced a specific contentPane), and most of the time this should not be a problem.You can add many GUI widgets in a
JFrame
, such asJButton
,JLabel
, etc. However, they will be automatically added to the associated contentPane.JPanel
does not handle the objects positioning, theLayoutManager
associated with your panel does; either automatically based on its own set of rules (e.g.FlowLayout
), or by using the constraints you have specified when adding the object to the container (theGridBagLayout
layout manager is a good example). The JavaDoc on the LayoutManagers usually contain enough information to get you started on using them.You can have nested panels, yes. A
Container
can contain otherContainer
instances. While it seems to be a complicated solution, it does enable you to control exactly how your GUI is displayed. Depending on whichLayoutManager
you are using, on the needs you have to fulfill with your user interface, and on your own preferences/habits as a developper, you might need less or more nested panels.您需要查看此 API 文档 <代码>JComponent。
如果您查看继承层次结构,您将看到
JComponent
扩展了Component
,因此 JComponent 是一个组件。另外,在直接已知子类下,您可以看到扩展
JComponent
的所有类的列表,包括JMenuBar
和JPanel
>。因此,
JMenuBar
和JPanel
是JComponent
(或Container
)的两个更专业的版本。You need to see this API doc for
JComponent
.If you look at the inheritance hierarchy, you will see that the
JComponent
extendsComponent
so a JComponent is a Component.Also under Direct Known Subclasses, you can see the list of all the classes that extend the
JComponent
includingJMenuBar
andJPanel
.So,
JMenuBar
andJPanel
are two more specialized versions ofJComponent
(orContainer
).