Reputation: 389
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
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
Reputation: 109823
best of all options could be use JMenuItem to interact with Cards layed by CardLayout, options are described incl. images in the tutorial
best of questions asked about CardLayout ever
Upvotes: 3