简体   繁体   English

将JMenuBar添加到JPanel?

[英]add JMenuBar to a JPanel?

I've got a JMenuBar and a JPanel. 我有一个JMenuBar和一个JPanel。 I'd like to add the JMenuBar to the JPanel. 我想将JMenuBar添加到JPanel。 How would I do so? 我该怎么办?

You can use a BorderLayout for your JPanel and put the JMenuBar into the NORTH area of the panel with 您可以为JPanel使用BorderLayout并将JMenuBar放入面板的NORTH区域

JPanel p = new JPanel();
p.setLayout(new BorderLayout());
p.add(menubar, BorderLayout.NORTH);

JMenuBar is a JComponent and can be added to a Container like any other JComponent. JMenuBar是一个JComponent,可以像任何其他JComponent一样添加到Container中。

JMenuBars are set to the JFrame using the setJMenuBar method. 使用setJMenuBar方法将JMenuBars设置为JFrame。

See the following tutorial on how to use them. 请参阅以下教程,了解如何使用它们。

http://download.oracle.com/javase/tutorial/uiswing/components/menu.html http://download.oracle.com/javase/tutorial/uiswing/components/menu.html

Try putting a jDesktopPane on your panel and then add a menubar to that. 尝试在面板上放置一个jDesktopPane,然后添加一个菜单栏。 I'm using a tabbed pane in my example below, but it should work the same for a panel. 我在下面的示例中使用了选项卡式窗格,但它对于面板应该是相同的。

    JDesktopPane desktopPane = new JDesktopPane();
    tabbedPane.addTab("New tab", null, desktopPane, null);

    JMenuBar menuBar_1 = new JMenuBar();
    menuBar_1.setBounds(0, 0, 441, 21);
    desktopPane.add(menuBar_1);

I have another solution, although you have to add the JMenuBar in the "Other Components" in NetBeans (good enough). 我有另一个解决方案,虽然你必须在NetBeans的“其他组件”中添加JMenuBar(足够好)。 Create a JPanel and then add another JPanel inside (call it child) that fills the entire outter JPanel. 创建一个JPanel,然后添加另一个JPanel(称为子),填充整个outter JPanel。 Place your controls in the child panel. 将控件放在子面板中。 Then add the JMenuBar but NetBeans will place it in the "Other Components". 然后添加JMenuBar,但NetBeans会将其放在“其他组件”中。 Edit your source and in the ctor after it calls "initComponents" place a call to this function: 在调用“initComponents”调用此函数后,编辑源代码并在ctor中:

public static void setJPanelMenuBar(JPanel parent, JPanel child, JMenuBar menuBar) {
    parent.removeAll();
    parent.setLayout(new BorderLayout());
    JRootPane root = new JRootPane();
    parent.add(root, BorderLayout.CENTER);
    root.setJMenuBar(menuBar);
    root.getContentPane().add(child);
    parent.putClientProperty("root", root);  //if you need later
  }

For example, your ctor might look like this: 例如,您的ctor可能如下所示:

  public MyPanel() {
    initComponents();
    setJPanelMenuBar(this, child, myMenuBar);
  }

Works for me. 适合我。 Got the idea by looking at JInternalFrame source code. 通过查看JInternalFrame源代码获得了这个想法。 All it does is replace the child JPanel with a JRootPane() and then put the child into the root pane's content pane. 它所做的就是用JRootPane()替换子JPanel,然后将子项放入根窗格的内容窗格中。

I tried too but JMenuItem with Jmenu and JmenuBar was not added to JPanel . 我也尝试了,但JMenuItemJmenuJmenuBar没有添加到JPanel But you can get that feel if you declare JFrame 's layout as null then use setBounds(x, y, width, height) on JMenuBar instance then add the menu bar to JFrame . 但是,如果将JFrame的布局声明为null然后在JMenuBar实例上使用setBounds(x, y, width, height)然后将菜单栏添加到JFrame则可以获得这种感觉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM