JPanel的内容存在混淆 [英] The JPanel contentpane confusion

查看:157
本文介绍了JPanel的内容存在混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Java Swing,并且我将一个menuBar附加到框架中。默认情况下,这应该调用 jframe.getContentPane()。add(child)。当我运行脚本时,menuBar没有出现。但是按钮位于顶部y = 0,如果这是有意义的。

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.

然后我意识到我的错误,我实际上必须放入菜单栏中的菜单。然后menuBar出现了。让我想到的是......menubarcontentpane实际上是2个面板?这让我感到困惑。因为这很像一个小组。但 getContentPane()返回一个Container,而不是JPanel对象,所以我很困惑。

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.

如果是这样,这是否意味着直接转储到框架中的唯一东西就是Jpanel对象?因此,JButtons,JLabel不直接在一个框架中...
这是否意味着,jpanels是嵌套的?
还有一件令我困惑的事情。如果jpanel可以控制事物的位置,那么LayoutManager是什么? :S
谢谢,请回答好像是2岁的老人问为什么天空是蓝色的,哈哈;)

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 ;)

推荐答案

一些随意的想法:


  • 是的,JPanels和其他组件通常是嵌套的。这是对Swing / AWT布局管理器的必要理解。

  • 虽然JFrame的返回类型是 getContentPane()在技​​术上是一个Container,它也是一个JPanel(最终从Container继承)。

  • 我相信你可以制作任何派生自ContainerPane的东西,但你需要注意它是不透明的。

  • 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.

这篇关于JPanel的内容存在混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆