我正在尝试制作一个带有子菜单的菜单。由于某种原因,我的菜单不会出现,我不知道为什么。JFrame
出现,但JMenuBar
没有。有人能帮我弄清楚问题出在哪里吗?
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();
}
});
}
}
您创建了一个菜单,但不将其设置为框架。
使用此:
GUI.setMenuBar(menubar);
创建之后。