user432584920684
user432584920684

Reputation: 389

Java GUI Game Menu

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.

Upvotes: 1

Views: 1647

Answers (2)

gergiusz
gergiusz

Reputation: 11

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();     
            }

Upvotes: 0

mKorbel
mKorbel

Reputation: 109823

Upvotes: 3

Related Questions