Why JMenuBar is not place in the JFrame content pane, but JToolbar place in the content pane

会有一股神秘感。 提交于 2020-01-19 17:58:27

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/21660699/why-jmenubar-is-not-place-in-the-jframe-content-pane-but-jtoolbar-place-in-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!