Why JMenuBar is not place in the JFrame content pane, but JToolbar place in the content pane(为什么 JMenuBar 没有放在 JFrame 内容窗格中,而是 JToolbar 放在了内容窗格中)
问题描述
为什么JMenuBar没有放在内容窗格中?制作java gui程序特别是使用jframe时有什么原因或影响吗?谢谢
如 必须处理这个特定的组件.通过将菜单栏与内容窗格分离,布局菜单栏和其余组件的责任得到了很好的划分.此外,菜单栏位置是固定的,并且该组件不可拖动,因此可以以与其他组件不同的方式进行布局.
关于 JToolBar 这个组件是可拖动的,你可能有几个,将一个放在另一个之上,并根据需要重新排列它们的顺序.所以这个组件与菜单栏完全不同,它可以像文本字段或其他常规组件一样放置在内容窗格中.
建议阅读:
- 使用顶级容器
- 如何使用根窗格
- 如何使用菜单
Why JMenuBar is not place in the content pane?is there any reason or effects when make a java gui program especially when using jframe? Thanks
As stated in Using Top-Level Containers article, the manu bar is managed by the Root Pane:
Each top-level container relies on a reclusive intermediate container called the root pane. The root pane manages the content pane and the menu bar, along with a couple of other containers. You generally don't need to know about root panes to use Swing components. However, if you ever need to intercept mouse clicks or paint over multiple components, you should get acquainted with root panes.
A menu bar is tipically placed at the top of a window, so if it would be placed in the content pane then the Layout managers would have to deal with this particular component. By segregating the menu bar from the content pane then the responsibility of laying out the menu bar and the rest of components is well divided. In addition, the menu bar location is fixed and this component is not dragabble so this can be laid out in a different way than the rest of components.
About the JToolBar this component is draggable and you may have several of them, placing one above another one and rearranging their order as you wish. So this component is totally different of a menu bar and makes sense it be placed in the content pane just as a text field or another regular component.
Suggested readings:
- Using Top-Level Containers
- How to Use Root Panes
- How to Use Menus
这篇关于为什么 JMenuBar 没有放在 JFrame 内容窗格中,而是 JToolbar 放在了内容窗格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么 JMenuBar 没有放在 JFrame 内容窗格中,而是 JToolbar 放在了内容窗格中


基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01