Switch JPanels inside a JFrame

0 votes

Quite green regarding javas component-stuff etc so please excuse me if information given by me isn't enough!

Considet the code below. Adding menu and menu showing in frame, no problem. I want when gameOn() is called to remove the menu and instead start the game. The code below only makes the Frames surface "blank", no gamepanel added.

Any thoughts/suggestions on how to fix it? The MenuPanel has a mouselistener.

public class GameFrame extends JFrame {

private MenuPanel mp; //extends JPanel
private GamePanel gp; //extends JPanel

public GameFrame() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(new Dimension(GameFrame.XSIZE, GameFrame.YSIZE));
    setLocationRelativeTo(null);
    setVisible(true);
    mp = new MenuPanel(this);

    add(mp);
}
public void gameOn() {
    remove(mp);
    GamePanel gp = new GamePanel(5);
    add(gp);
}
}
Feb 14, 2019 in Java by Sushmita
• 6,920 points
13,082 views

1 answer to this question.

0 votes

Instead of trying to add an remove components, use a CardLayout

CardLayout cardLayout = new CardLayout();
JPanel mainPanel = new JPanel(cardLayout);

MenuPanel menu = new MenuPanel();
GamePanel game = new GamePanel();
mainPanel.add(menu, "menu");
mainPanel.add(game, "game");

...
public void gameOn() {
    cardLayout.show(mainPanel, "game");
}

When gameOn() is called, the menu will get pushed to the back, and the game to the front.

This way you don't have to keep adding and removing

Here's an example you can run

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GameFrame extends JFrame implements ActionListener{

    CardLayout cardLayout;
    JPanel mainPanel;
    MenuPanel menu;
    GamePanel game;

    public GameFrame() {
        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);
        menu = new MenuPanel();
        game = new GamePanel();
        mainPanel.add(menu, "menu");
        mainPanel.add(game, "game");


        JButton goGame = new JButton("Go TO Game");
        goGame.addActionListener(this);

        add(mainPanel);
        add(goGame, BorderLayout.SOUTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationByPlatform(true);
        setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        gameOn();
    }

    public void gameOn() {
        cardLayout.show(mainPanel, "game");
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            @Override
            public void run() {
                GameFrame gameFrame = new GameFrame();
            }
        });
    }
}

class MenuPanel extends JPanel {

    public MenuPanel() {
        setBackground(Color.GREEN);
        add(new JLabel("Menu"));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }
}

class GamePanel extends JPanel {

    public GamePanel() {
        setBackground(Color.BLUE);
        add(new JLabel("Game"));
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(300, 300);
    }
}
answered Feb 14, 2019 by developer_1
• 3,350 points

Related Questions In Java

0 votes
2 answers

Counting no of Occurrence of a particular character inside a string in Java

We can find out the no. of ...READ MORE

answered Sep 7, 2018 in Java by Sushmita
• 6,920 points
2,817 views
0 votes
2 answers

Store String inside a File using Java

We can use Apache Commons IO. It ...READ MORE

answered Jul 20, 2018 in Java by Sushmita
• 6,920 points
1,444 views
0 votes
1 answer

Programmatically closing a JFrame

If you want the GUI to behave ...READ MORE

answered Jun 21, 2018 in Java by Parth
• 4,640 points
1,693 views
0 votes
1 answer

Should a class file always be inside package for importing it in jsp page?

Hey there! It is not necessary for the ...READ MORE

answered May 24, 2019 in Java by Karan
2,220 views
0 votes
0 answers

Track the exact location of IP address

I looked into IP addresses and a ...READ MORE

Feb 14, 2022 in Cyber Security & Ethical Hacking by Edureka
• 13,620 points
885 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

What is the difference between jdk and jre?

JRE: It stands for Java Runtime Environment. ...READ MORE

answered Apr 20, 2018 in Java by Akrati
• 3,190 points
1,994 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,140 points
3,739 views
0 votes
3 answers

Convert date object to a String

We parse the full date to time ...READ MORE

answered Jul 31, 2018 in Java by samarth295
• 2,220 points
1,583 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP