0

Im trying to get it so that when i click on a menu option, the windows changes from the 'welcome text' to 4 buttons which the user can then click on. However, when i click on the simulation button, nothing happens.. the window doesnt change at all.. Ive summarized my code to the basic stuff by the way. anyone see anything i cant?

 JFrame GUI = new JFrame("Graphical User Interface");
public gui()
    {
    JMenuBar menubar = new JMenuBar();
    JMenu Simulation = new JMenu("Simulation");

    theLabel = new JLabel("Welcome to the Main Menu. ",JLabel.CENTER);
    GUI.add(theLabel);

    menubar.add(Simulation);
    Simulation.add(Simulationmenu);

    Simulationmenu.addActionListener(this);

    GUI.setJMenuBar(menubar);
    GUI.setLocation(500,250);
    GUI.setSize(300, 200);
    GUI.setVisible(true);
    }

    public void actionPerformed(ActionEvent E){

 if(E.getSource() == Simulationmenu){
    // Buttons in the menu i want to output once clicked on 'simulation'
                thePanel = new JPanel(new GridLayout(4,0));

                Run = new JButton("Run");
                Pause = new JButton("Pause");
                Reset = new JButton("Reset");
                DisplayMaps = new JButton("Display Maps?");

                // Add the components to the panel:
                thePanel.add("West", Run);
                thePanel.add("Center", Pause);
                thePanel.add("East", Reset); 
                thePanel.add("West", DisplayMaps);

                // Add the panel to the contentPane of the frame:
                GUI.add(thePanel);

                // add this object as listener to the two buttons:
                Run.addActionListener(this);
alex2410
  • 10,904
  • 3
  • 25
  • 41
user2964762
  • 93
  • 2
  • 11
  • 1) For better help sooner, post an [SSCCE](http://sscce.org/). 2) Use a consistent and logical indent for code blocks. The indentation of the code is intended to help people understand the program flow. 3) For many components in one space, use a [`CardLayout`](http://docs.oracle.com/javase/7/docs/api/java/awt/CardLayout.html) as seen in this [short example](http://stackoverflow.com/a/5786005/418556). – Andrew Thompson Nov 27 '13 at 23:06

1 Answers1

2

Seems your problem in next, you add a new panel(thePanel) to your JFrame(GUI) when it is showing, but in this case you must to call revalidate() method of JFrame(GUI).

Add GUI.revalidate() after GUI.add(thePanel);, it helps you.

alex2410
  • 10,904
  • 3
  • 25
  • 41