I'm trying to make a menu with submenus. For some reason my menu will not show up and I can't figure out why. JFrame
appears but the JMenuBar
does not. Can someone help me figure out what the problem is?
public class AWorldPanel extends JPanel {
/** Declaring all the menu items within the GUI **/
private JMenuItem Fileitem1 = new JMenuItem("New configuration");
private JMenuItem Fileitem2 = new JMenuItem("Open configuration file ");
private JMenuItem Fileitem3 = new JMenuItem("Save");
private JMenuItem Fileitem4 = new JMenuItem("Save As");
private JMenuItem Fileitem5 = new JMenuItem("Exit");
private JMenuItem Viewitem1 = new JMenuItem("Display configuration");
private JMenuItem Viewitem2 = new JMenuItem("Edit configuration");
private JMenuItem Viewitem3 = new JMenuItem("Info about Bugs");
private JMenuItem Viewitem4 = new JMenuItem("Info about Map");
private JMenuItem Edititem1 = new JMenuItem("Remove");
private JMenuItem Edititem2 = new JMenuItem("Add");
private JMenuItem Simulationitem1 = new JMenuItem("Simulation");
private JMenuItem Helpitem1 = new JMenuItem("Info about application");
private JMenuItem Helpitem2 = new JMenuItem("Info about author");
JLabel theLabel;
JPanel thePanel;
JButton Run, Pause, Reset;
JFrame GUI = new JFrame("Graphical User Interface");
public void AWorldPanel() {
// Create the container
JFrame GUI = new JFrame("Graphical User Interface");
GUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/** Creating the menu **/
JMenuBar menubar = new JMenuBar();
JMenu File = new JMenu("File");
JMenu View = new JMenu("View");
JMenu Edit = new JMenu("Edit");
JMenu Simulation = new JMenu("Simulation");
JMenu Help = new JMenu("Help");
/** sub menus **/
menubar.add(File);
File.add(Fileitem1);
File.add(Fileitem2);
File.add(Fileitem3);
File.add(Fileitem4);
File.add(Fileitem5);
menubar.add(View);
View.add(Viewitem1);
View.add(Viewitem2);
View.add(Viewitem3);
View.add(Viewitem4);
menubar.add(Edit);
Edit.add(Edititem1);
Edit.add(Edititem2);
menubar.add(Simulation);
Simulation.add(Simulationitem1);
menubar.add(Help);
Help.add(Helpitem1);
Help.add(Helpitem2);
}
private static void createAndShowGUI() {
AWorldPanel newworld = new AWorldPanel();
newworld.GUI.pack();
newworld.GUI.setVisible(true);
newworld.GUI.setLocation(300, 100);
newworld.GUI.setSize(500, 500);
}
public static void main(String args[]) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
You create a menu, but don't set it to the frame.
Use this:
after you create it.