Search code examples
javaswingappletlayout-managercardlayout

Java: How to display card JPanel fully when using CardLayout


I'm having trouble with this JApplet. At the moment I have a CardLayout JPanel which contains two BorderLayout JPanels. Whenever I run it, the components added to each 'card' (a JButton to go back to the other JPanel) don't display unless I use setVisible(true) for each LayoutManager. Furthermore, none of my ActionListeners work. I'm assuming because they only use show() and there's something else I have to do that's alluding me.

Must I use setVisible(true)? It seems from other questions that there's a way of doing this without that. Here's the code I'm having trouble with:

/*
*Java Version:      1.8.0_25
*Author:            Peadar Ó Duinnín
*Student Number:    R00095488
*/

package As1;

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


public class AUIJApplet extends JApplet implements ActionListener {
    private final int WIDTH = 600;
    private final int HEIGHT = 400;
    private int highScore;
    private int currentScore;

    JPanel panelCont = new JPanel();
    JPanel startPanel = new JPanel();
    JPanel gamePanel = new JPanel();
    JButton newGameButton = new JButton("New Game");
    JButton endGameButton = new JButton("End Game");
    JLabel highScoreLabel;
    JLabel currentScoreLabel;
    CardLayout cl = new CardLayout();
    BorderLayout bl = new BorderLayout();

    public AUIJApplet() {
        highScore = 0;
    }

    @Override
    public void init() {
        setSize(WIDTH, HEIGHT);

        panelCont.setLayout(cl);
        startPanel.setLayout(bl);
        gamePanel.setLayout(bl);

        startPanel.add(newGameButton, BorderLayout.SOUTH);
        gamePanel.add(endGameButton, BorderLayout.SOUTH);
        startPanel.setBackground(Color.BLUE);
        gamePanel.setBackground(Color.GREEN);

        panelCont.add(startPanel, "Start Applet Screen");
        panelCont.add(gamePanel, "New Game Screen");

        newGameButton.addActionListener((e) -> {
            newGame();
        });

        endGameButton.addActionListener((e) -> {
            quitGame();
        });

        cl.show(panelCont, "Start Applet Screen");
        this.add(panelCont);
    }

    public void newGame() {
        cl.show(panelCont, "New Game Screen");
        showScores(gamePanel);
    }

    public void quitGame() {
        cl.show(panelCont, "Start Applet Screen");
        if (currentScore > highScore) {
            highScore = currentScore;
        }
        currentScore = 0;
    }

    public void showScores(JPanel currentPanel) {
        currentPanel.add(new JLabel("High Score:") , BorderLayout.EAST);
        currentPanel.add(highScoreLabel, BorderLayout.EAST);
        currentPanel.add(new JLabel("Current Score:"), BorderLayout.EAST);
        currentPanel.add(currentScoreLabel, BorderLayout.EAST);
    }

    @Override
    public void actionPerformed(ActionEvent ae) {

    }
}

Solution

  • I have made the a little similar code to perform same operation it works for me try to write the code from scratch. Here is my code.

    import java.awt.BorderLayout;
    import java.awt.CardLayout;
    import java.awt.Color;
    import java.awt.event.ActionEvent;
    import javax.swing.JApplet;
    import javax.swing.*;
    
    
        public class Example extends JApplet {
    
            JPanel panel1,panel2,mainPanel;
            JButton start,stop;
            CardLayout cl = new CardLayout();
            @Override
            public void init() {
                panel1 = new JPanel();
                panel1.setBackground(Color.red);
                panel1.setLayout(new BorderLayout());
                panel2 = new JPanel();
                panel2.setBackground(Color.blue);
                panel2.setLayout(new BorderLayout());
                start = new JButton("Start");
                stop = new JButton("stop");
                panel1.add(start,BorderLayout.SOUTH);
                panel2.add(stop,BorderLayout.SOUTH);
                mainPanel = new JPanel();
                mainPanel.setLayout(cl);
                mainPanel.add(panel1,"First Panel");
                mainPanel.add(panel2, "Second Panel");
                start.addActionListener((ActionEvent e) -> {
                    newGame();
                });
                stop.addActionListener((ActionEvent e) ->{
                    endGame();
                });
                this.add(mainPanel);
            }
    
            public void newGame()
            {
                cl.show(mainPanel, "Second Panel");
            }
            public void endGame()
            {
                cl.show(mainPanel,"First Panel");
            }
        }