Home >Java >javaTutorial >How to Switch JPanels within a JFrame using CardLayout?

How to Switch JPanels within a JFrame using CardLayout?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-28 06:29:30899browse

How to Switch JPanels within a JFrame using CardLayout?

Switching JPanels within a JFrame

Question:
I'm new to Java components and having trouble switching JPanels inside a JFrame. When I call gameOn(), the Frame surface becomes blank without displaying the game panel. How can I fix this?

Answer:

Instead of adding and removing components, it's recommended to use a CardLayout. Here's how:

  1. Create a CardLayout object and a JPanel using it:

    <code class="java">CardLayout cardLayout = new CardLayout();
    JPanel mainPanel = new JPanel(cardLayout);</code>
  2. Add the menu and game panels to mainPanel:

    <code class="java">MenuPanel menu = new MenuPanel();
    GamePanel game = new GamePanel();
    mainPanel.add(menu, "menu");
    mainPanel.add(game, "game");</code>
  3. In the gameOn() method, simply show the game panel using cardLayout without adding or removing any components:

    <code class="java">public void gameOn() {
        cardLayout.show(mainPanel, "game");
    }</code>
  4. When gameOn() is called, the menu panel will be hidden behind the game panel.

Example:

<code class="java">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;

class GameFrame extends JFrame implements ActionListener {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new GameFrame().setVisible(true));
    }

    private CardLayout cardLayout;
    private JPanel mainPanel;
    private MenuPanel menuPanel;
    private GamePanel gamePanel;

    public GameFrame() {
        cardLayout = new CardLayout();
        mainPanel = new JPanel(cardLayout);

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

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

        JButton goGameButton = new JButton("Go to Game");
        goGameButton.addActionListener(this);

        add(mainPanel);
        add(goGameButton, BorderLayout.SOUTH);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
        setLocationRelativeTo(null);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        cardLayout.show(mainPanel, "game");
    }

}

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);
    }
}</code>

The above is the detailed content of How to Switch JPanels within a JFrame using CardLayout?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn