I'm working on a game and I am using a CardLayout to switch between different JPanels in the same JFrame.
Problem: I have a helpPanel that I can go to pushing a button in the main-menuPanel and pushing a button in the gamePanel.
What I want: I want to be able to place a JButton
in my helpPanel
that (when pressed) goes back to the main-menuPanel or the gamePanel (depending if you came from the main-menuPanel or from the gamePanel).
What I've tried: I've tried using the actionlistener
in combination with .previous(container parent)
but that only takes me back to the JPanel
that was added to the CardLayout
in the position previous to the helpPanel. I've also tried nesting the actionListeners
to try and get different situations but that didn't work.
What I searched: I've searched for solutions in other Questiones but it's always about switching between 2 different JPanels
in stead of 3.
Can anyone help me please? I find it to be a very difficult situation.
Read the section from the Swing tutorial on How to Use Card Layout . It shows you how to switch between cards by specifying a String that identifies the card to display.
I have been working on a game menu recently(which you'll see bits and pieces of here), and one way to do it is to have a common ActionListener for all the panel's buttons within. then set their action commands to have specific Strings. in this example, overallgame is the master CardLayout panel. GAME, CREDITS, and MAIN-MENU are String constants used to title panels inside the overallgame panel
ActionListener listener=new ActionListener() {public void actionPerformed(ActionEvent e){ if(e.getActionCommand().equals("start")){ //dummyframe.add(newgame); //mainmenupanel=null; CardLayout layout=(CardLayout)(overallgame.getLayout());//retrieves the overall layout layout.show(overallgame, GAME);//uses it to switch to main game panel newgame.requestFocus(true);//need to redirect focus to the game rather than original main menu dummyframe.repaint(); } else if(e.getActionCommand().equals("show credits")){ CardLayout layout=(CardLayout)(overallgame.getLayout()); layout.show(overallgame, CREDITS); } else if(e.getActionCommand().equals("switch to main")){ CardLayout layout=(CardLayout)(overallgame.getLayout()); layout.show(overallgame, MAIN_MENU); } else if(e.getActionCommand().equals("show chapter panel")){ CardLayout layout=(CardLayout)(overallgame.getLayout()); layout.show(overallgame, CHAPTERSTART); } else{ JOptionPane.showMessageDialog(null, "hey"); } } };
NewGame.setActionCommand("start");
to make a panel have a title that can be used by the CardLayout, you have to define the name when adding the panel to the CardLayout. this line adds a JPanel called mainmenupanel to my overallgame JPanel, and then allows the overallgame panel to refer to it with the String constant MAIN_MENU.
overallgame.add(mainmenupanel, MAIN_MENU);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.