Jump to content

View more

Image of the Day

#ld38 #screenshotsaturday Mimosa Fizz action gif #2 https://t.co/TUzdppvfUL
IOTD | Top Screenshots

The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.


Sign up now

JPanel switching? Please help a newbie...

4: Adsense

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.


  • You cannot reply to this topic
1 reply to this topic

#1 Judd Steven Garcia   Members   

101
Like
0Likes
Like

Posted 23 February 2012 - 04:02 PM

Hello! I need help in trying to switch JPanels in one JFrame. So far my game has two JPanels. One(MainMenu) is the main menu and the other is the actual game(GameFrame). I would like it so that when I push the ENTER key from the main menu, it removes the MainMenu panel and switch it to the GameFrame panel. I am suppose to use a CardLayout but I have not found any tutorial that I understand. Any help would be appreciated. Thank you!

#2 SimonForsman   Members   

7633
Like
0Likes
Like

Posted 23 February 2012 - 05:58 PM

You need 3 JPanels.

1 that uses the card layout
1 for your main menu
1 for your game

Add the JPanels for the menu and for the game to the jpanel that uses the cardlayout , pass an identifying string along with them.
mainPanel = new JPanel(new CardLayout());
mainPanel.add(gamePanel,"GamePanel");
mainPanel.add(menuPanel,"MenuPanel");

When you wish to change the visible JPanel you grab the layout from the main panel (The one that uses the card layout) using:
CardLayout layout = (CardLayout)(mainPanel.getLayout());
and then change the visible panel using
layout.show("GamePanel");
or
layout.show("MenuPanel");
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!




Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.