Home > Java > javaTutorial > body text

How to Use CardLayout to Switch JPanels Inside a JFrame?

Susan Sarandon
Release: 2024-10-28 05:46:02
Original
356 people have browsed it

How to Use CardLayout to Switch JPanels Inside a JFrame?

How to Switch JPanels inside a JFrame Using CardLayout

Switching JPanels within a JFrame can be a straightforward task. Let's explore how to effectively achieve this using a technique called CardLayout.

The following code snippet provides an example of how to use CardLayout:

<code class="java">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");</code>
Copy after login

Here, we create a CardLayout instance called cardLayout and a main panel, mainPanel, that uses this layout manager. We then add two panels, menu and game, to the mainPanel with their respective names, "menu" and "game".

When you call the gameOn() method, it changes the visibility of the panels using cardLayout.show(). For instance, the following code:

<code class="java">public void gameOn() {
    cardLayout.show(mainPanel, "game");
}</code>
Copy after login

will hide the menu panel and display the game panel at the front. This method avoids the need for adding and removing components constantly, which can be inefficient.

Here's an example you can run:

<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;

public class GameFrame extends JFrame implements ActionListener{

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

    public GameFrame() {
        // ... (similar to previous example)
    }

    // ...

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

    public void gameOn() {
        // ... (similar to previous example)
    }

    public static void main(String[] args) {
        // ... (similar to previous example)
    }
}</code>
Copy after login

This example demonstrates the use of CardLayout to seamlessly switch between the menu and game panels within a JFrame.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!