Reputation: 695
I'm learning GUI programming in java, and trying to modify an existing programme to add a new menu bar at the top of the frame.
The main method is below. The MainPanel class extends JPanel and contains the main components of the programme (a basic game).
public static void main(String[] args) {
JFrame frame = new JFrame("Sokuban");
MainPanel panel = new MainPanel();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.setContentPane(panel);
frame.pack();
frame.setVisible(true);
}
I'm not sure if I should add a new JPanel, add it to the JFrame, and then within that add buttons? Or create a JMenuBar, within the existing panel, or frame, and then use BorderLayout.NORTH to arrange it?
Just playing around with things I found on google, I've tried the following snippets separately (haven't put all code in):
JMenuBar menuBar = new JMenuBar();
frame.add(new Button("Button"), BorderLayout.SOUTH);
panel.BorderLayout.SOUTH;
JPanel frame2 = new JPanel();
window.add(frame2, BorderLayout.NORTH);
JButton b1 = new JButton();
frame2.setSize(500,500);
b1.setSize(400,400);
b1.setVisible(true);
b1.setText("Button");
frame2.add(b1);
frame2.setVisible(true);
I can't figure out which direction I should be going in. Any pointers v much appreciated!
Upvotes: 0
Views: 110
Reputation: 917
Don't do that, check this out it's what you're looking for
This link is for a JMenuBar https://docs.oracle.com/javase/7/docs/api/java/awt/MenuBar.html
This link is for a JMenu https://docs.oracle.com/javase/7/docs/api/javax/swing/JMenu.html
This link is for a JMenuItem https://docs.oracle.com/javase/7/docs/api/javax/swing/JMenuItem.html
You don't need a new JFrame to create a menu, you can use the JMenuBar();
JMenuBar myMenu = new JMenuBar();
//The above snippet does not create or add menus it's simply the container that holds them
JMenu fileMenu = new JMenu("File"); // This will create a menu named file
JMenuItem openChoice = new JMenuItem("Open"); /* This will create an option under
the fileMenu named open*/
//To Actually add these things you would do this
setJMenuBar(myMenu);
myMenu.add(fileMenu); // This adds fileMenu to the menubar
fileMenu.add(openChoice); // This adds openChocie to the JMenu named fileOption
Now the above code was a very basic example as to how to setup a menu I would sugesst following the code I outlined here and as you learn to improve upon this as this is just a starting point for you!
Upvotes: 1
Reputation: 17454
I'm not sure if I should add a new JPanel, add it to the JFrame, and then within that add buttons? Or create a JMenuBar, within the existing panel, or frame, and then use BorderLayout.NORTH to arrange it?
Here are some personal experience that I can share with you:
I will usually plan the appearance of my user interface first (Either on paper on in my mind). After that, decide a suitable layout manager for the container.
I can have nested panels with more than 1 layout if the UI is complicated.
But ultimately, I will usually have a main panel which hold every other components (subpanels / buttons / textFields..).
Add the main panel into the JFrame. (You can have a customized JPanel, and we rarely need a customized JFrame).
As for the menu bar:
frame.setJMenuBar(menuBar);
The following image should help you in understanding of the hierarchy:
Upvotes: 1
Reputation: 31
First : https://docs.oracle.com/javase/tutorial/uiswing/components/menu.html :) This is quite easy tutorial of JMenuBar. :) And if you one want to only a button on JBarMenu : How to make a JMenu have Button behaviour in a JMenuBar
Upvotes: 1