I've got a JMenuBar and a JPanel. I'd like to add the JMenuBar to the JPanel. How would I do so?
Then the answer to your question is no, there is no other way to do this, the API was designed from the start to make use of the layout managers.
JMenuBar is an implementation of menu bar . the JMenuBar contains one or more JMenu objects, when the JMenu objects are selected they display a popup showing one or more JMenuItems . JMenu basically represents a menu . It contains several JMenuItem Object . It may also contain JMenu Objects (or submenu).
Just call the method pack() after setting the ContentPane , ( java 1.7 , maybe older) like this: JFrame frame = new JFrame(); JPanel panel1 = new JPanel(); JPanel panel2 = new JPanel(); .... frame. setContentPane(panel1); frame. pack(); ... frame.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With