Java 将 JMenuBar 添加到 JPanel?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4299846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:34:32  来源:igfitidea点击:

add JMenuBar to a JPanel?

javaswingjpaneljmenubar

提问by Skizit

I've got a JMenuBar and a JPanel. I'd like to add the JMenuBar to the JPanel. How would I do so?

我有一个 JMenuBar 和一个 JPanel。我想将 JMenuBar 添加到 JPanel。我该怎么做?

采纳答案by Reboot

You can use a BorderLayoutfor 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 一样添加到容器中。

回答by Codemwnci

JMenuBars are set to the JFrame using the setJMenuBar method.

使用 setJMenuBar 方法将 JMenuBar 设置为 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

回答by Panday Manish Sahay

I tried too but JMenuItemwith Jmenuand JmenuBarwas not added to JPanel. But you can get that feel if you declare JFrame's layout as null then use setBounds(x, y, width, height)on JMenuBarinstance then add the menu bar to JFrame.

我想太多,但JMenuItemJmenuJmenuBar没有加入JPanel。但是,如果您将JFrame的布局声明为 null,然后setBounds(x, y, width, height)JMenuBar实例上使用,然后将菜单栏添加到JFrame.

回答by user2248926

Try putting a jDesktopPane on your panel and then add a menubar to that. I'm using a tabbed pane in my example below, but it should work the same for a panel.

尝试在您的面板上放置一个 jDesktopPane,然后向其添加一个菜单栏。我在下面的示例中使用了选项卡式窗格,但它对于面板的工作方式应该相同。

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

回答by Peter Quiring

I have another solution, although you have to add the JMenuBar in the "Other Components" in NetBeans (good enough). Create a JPanel and then add another JPanel inside (call it child) that fills the entire outter JPanel. Place your controls in the child panel. Then add the JMenuBar but NetBeans will place it in the "Other Components". Edit your source and in the ctor after it calls "initComponents" place a call to this function:

我有另一个解决方案,尽管您必须在 NetBeans 的“其他组件”中添加 JMenuBar(足够好)。创建一个 JPanel,然后在内部添加另一个 JPanel(称为子项)以填充整个外部 JPanel。将您的控件放在子面板中。然后添加 JMenuBar,但 NetBeans 会将其放置在“其他组件”中。编辑您的源代码,并在调用“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. All it does is replace the child JPanel with a JRootPane() and then put the child into the root pane's content pane.

为我工作。通过查看 JInternalFrame 源代码得到了这个想法。它所做的只是用 JRootPane() 替换子 JPanel,然后将子项放入根窗格的内容窗格中。