1

I'm trying to work on a project - we are creating a little game with a GUI. I decided to start by working on a 'main menu'. Essentially, there will be buttons such as "Single Player", "Help", etc...

I've made the GUI with the menu, etc. I have added listeners as well.

How do I approach the problem now? If someone clicks, say, 'Single Player', I'd like the screen to change to display a title showing "SINGLE PLAYER" and get rid of the main menu. What do I need to do in my actionPerformed() method to get this effect?

I guess I will be able to work it out from there.

Any help would be greatly appreciated.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
user432584920684
  • 379
  • 1
  • 8
  • 19

2 Answers2

3
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

The best approach would be to put the corresponding windows onto a separate Jpanel and have one master panel in your gui. Whenever the user clicks on a button, you remove whatever was on the master panel add the corrseponding panel, then repaint the gui.

something like this:

    if (e.getSource() == button){           
                masterPanel.removeAll();            
                masterPanel.add(newWindow);                 
                this.setVisible(true);
                this.repaint();     
            }
gergiusz
  • 11
  • 1
  • 7