English 中文(简体)
add JMenuBar to a JPanel?
原标题:

I ve got a JMenuBar and a JPanel. I d like to add the JMenuBar to the 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 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.

问题回答

JMenuBars are set to the JFrame using the setJMenuBar method.

See the following tutorial on how to use them.

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

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:

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:

  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.

I tried too but JMenuItem with Jmenu and JmenuBar was 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 JMenuBar instance then add the menu bar to JFrame.

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




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签