JPanel 内容窗格混乱

发布于 2024-12-09 20:30:44 字数 484 浏览 3 评论 0原文

我正在学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

行雁书 2024-12-16 20:30:44

一些随机的想法:

  • 是的,JPanel 和其他组件通常是“嵌套的”。这就是对 Swing/AWT 布局管理器的深入理解至关重要的地方。
  • 虽然 JFrame 的 getContentPane() 返回的类型在技术上是一个 Container,但它也是一个 JPanel(它最终从 Container 继承)。
  • 我相信您可以将任何从 Container 派生的内容设为 contentPane,但您需要注意它是不透明的。

Some random thoughts:

  • Yes, JPanels and other components are often "nested". This is where a firm understanding of the Swing/AWT layout managers is essential.
  • While the type returned by a JFrame's getContentPane() is technically a Container, it's also a JPanel (which inherits eventually from Container).
  • I believe that you can make anything that derives from Container the contentPane, but you need to take care that it is opaque.
孤独患者 2024-12-16 20:30:44

为此,今天的 Java Swing GUI 有一个方法

frame.setJMenuBar(menuBar);

,不需要从 Java5 声明 ContentPane,并且使用 BorderLayout 作为默认 LayoutManager

然后 frame.add(myPanel);
//与frame.add(myPanel, BorderLayout.CENTER)相同,并占据了整个Container

关于如何使用LayourManagers

for that there is method

frame.setJMenuBar(menuBar);

for todays Java Swing GUI, is not neccesary declare ContentPane, from Java5, and with BorderLayout as default LayoutManager

then frame.add(myPanel);
//is same as frame.add(myPanel, BorderLayout.CENTER) and occupated whole Container

basic stuff about how to use LayourManagers

和我恋爱吧 2024-12-16 20:30:44

getContentPane() 始终返回一个 Container 实例。但是,您应该注意,JPanel 对象是 Container 实例,以及 Swing 框架中的其他类。返回实例的实际类是无关紧要的,因为您无法控制将哪个 Container 实现用作 contentPane(除非您强制使用特定的 contentPane),并且大多数情况下这应该不是问题。

您可以在 JFrame 中添加许多 GUI 小部件,例如 JButtonJLabel 等。但是,它们会自动添加到关联的 contentPane 中。

JPanel 不处理对象定位,与面板关联的 LayoutManager 负责;要么自动基于其自己的规则集(例如FlowLayout),要么使用您在将对象添加到容器时指定的约束(GridBagLayout布局管理器是一个很好的例子)。 LayoutManager 上的 JavaDoc 通常包含足够的信息来帮助您开始使用它们。

你可以有嵌套面板,是的。 Container 可以包含其他 Container 实例。虽然这似乎是一个复杂的解决方案,但它确实使您能够准确控制 GUI 的显示方式。根据您使用的 LayoutManager、用户界面必须满足的需求以及您作为开发人员的偏好/习惯,您可能需要更少或更多的嵌套面板。

getContentPane() always returns a Container instance. However, you should note that JPanel objects are Container 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 of Container 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 as JButton, JLabel, etc. However, they will be automatically added to the associated contentPane.

JPanel does not handle the objects positioning, the LayoutManager 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 (the GridBagLayout 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 other Container instances. While it seems to be a complicated solution, it does enable you to control exactly how your GUI is displayed. Depending on which LayoutManager 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.

最近可好 2024-12-16 20:30:44

您需要查看此 API 文档 <代码>JComponent。

如果您查看继承层次结构,您将看到 JComponent 扩展了 Component,因此 JComponent 是一个组件。

另外,在直接已知子类下,您可以看到扩展JComponent的所有类的列表,包括JMenuBarJPanel >。

因此,JMenuBarJPanelJComponent(或 Container)的两个更专业的版本。

You need to see this API doc for JComponent.

If you look at the inheritance hierarchy, you will see that the JComponent extends Component so a JComponent is a Component.

Also under Direct Known Subclasses, you can see the list of all the classes that extend the JComponent including JMenuBar and JPanel.

So, JMenuBar and JPanel are two more specialized versions of JComponent (or Container).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文