简体   繁体   中英

Java: JPanels not displaying properly in CardLayout

My JPanel startPan is supposed to display when the applet starts but right now, only the JButton setupGameBut displays. But if I click setupGameBut and then cancelBut to return to startPan, it displays the rules but still doesn't display the JLabel headingLab (which does show up on setupPan).

As well as that, it displays components from other panels behind the panel currently being shown but they're unable to be interacted with.

And on clicking a button, if you move the cursor away from it before it loads the next card, it won't update to the button supposed to be shown there until you mouse-over again. I've a feeling that I'm doing something fundamentally wrong with my code that I have these problems but I'm not sure.

/*
*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.Choice;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;


public class AUIApplet extends JApplet {
    private final int APPLET_WIDTH;
    private final int APPLET_HEIGHT;
    private final int[] highScores = new int[3];
    private int currentScore;

    final Font normalFont = new Font("Cambria", Font.PLAIN, 18);
    final Font headingFont = new Font("Cambria", Font.BOLD, 24);
    JPanel contPan, startPan, setupPan, gamePan, scorePan, startInfoPan, gameInfoPan, rulePan, setupInfoPan, gameOptPan;
    JButton setupGameBut, cancelBut, endGameBut, startGameBut;
    JLabel highScoreLab, currentScoreLab, headingLab, rulesLab, difficultyScoreLab;
    Choice difficulty;
    CardLayout cl = new CardLayout();

    public AUIApplet() {
        this.APPLET_HEIGHT = 400;
        this.APPLET_WIDTH = 600;
        for (int i : highScores) {
            i = 0;
        }
    }

    @Override
    public void init() {
        //set applet size
        setSize(APPLET_WIDTH, APPLET_HEIGHT);

        //start screen
        startPan = new JPanel();
        startPan.setLayout(new BorderLayout());

        //setup screen
        setupPan = new JPanel();
        setupPan.setLayout(new BorderLayout());

        setupGameBut = new JButton("New Game");
        cancelBut = new JButton("Cancel");

        //game screen
        gamePan = new JPanel();
        gamePan.setLayout(new BorderLayout());

        endGameBut = new JButton("Quit Game");

        //heading label
        headingLab = new JLabel("Number Memory");
        headingLab.setFont(headingFont);

        //rule panel
        rulePan = new JPanel();
        rulePan.setLayout(new GridLayout(8,1));
        rulePan.add(new JLabel("Rules:"));
        rulePan.add(new JLabel("1. You will be shown a series of numbers, one at a time."));
        rulePan.add(new JLabel("2. You must recite the series of numbers after the last number has been displayed."));
        rulePan.add(new JLabel("3. After each correct recitation of the sequence, another sequence will play with one extra number."));
        rulePan.add(new JLabel("Note: You can decrease/increase the time each number displays for by changing the difficulty."));

        //difficulty selection
        difficulty = new Choice();
        difficulty.add("Easy");
        difficulty.add("Normal");
        difficulty.add("Hard");
        difficulty.add("Extra Hard");
        difficulty.select(1);

        difficultyScoreLab = new JLabel("" + highScores[1] + "");

        switch(difficulty.getSelectedIndex()) {
            case 0: difficultyScoreLab.setText("" + highScores[0] + "");
                break;
            case 1: difficultyScoreLab.setText("" + highScores[1] + "");
                break;
            case 2: difficultyScoreLab.setText("" + highScores[2] + "");
                break;
            case 3: difficultyScoreLab.setText("" + highScores[3] + "");
                break;
        }

        //game option panel
        gameOptPan = new JPanel();
        gameOptPan.setLayout(new GridLayout(1,2));
        startGameBut = new JButton("Start Game");
        gameOptPan.add(startGameBut);
        gameOptPan.add(difficulty);

        //start info panel
        startInfoPan = new JPanel();
        startInfoPan.setLayout(new BorderLayout());
        startInfoPan.add(rulePan, BorderLayout.CENTER);

        //setup info panel
        setupInfoPan = new JPanel();
        setupInfoPan.setLayout(new BorderLayout());
        setupInfoPan.add(gameOptPan, BorderLayout.SOUTH);
        setupInfoPan.add(new JLabel("High Score:"), BorderLayout.NORTH);
        setupInfoPan.add(difficultyScoreLab, BorderLayout.CENTER);

        //game info panel
        gameInfoPan = new JPanel();
        gameInfoPan.setLayout(new BorderLayout());

        //score panel
        scorePan = new JPanel();
        scorePan.setLayout(new GridLayout(10,1));
        highScoreLab = new JLabel("High Score: " + highScores[difficulty.getSelectedIndex()] + "  ");
        currentScoreLab = new JLabel("Current Score: " + currentScore + "  ");
        scorePan.add(highScoreLab);
        scorePan.add(currentScoreLab);

        //adding to start panel
        startPan.add(setupGameBut, BorderLayout.SOUTH);
        startPan.add(headingLab, BorderLayout.NORTH);
        startPan.add(startInfoPan, BorderLayout.CENTER);

        //adding to setup panel
        setupPan.add(cancelBut, BorderLayout.SOUTH);
        setupPan.add(headingLab, BorderLayout.NORTH);
        setupPan.add(setupInfoPan, BorderLayout.CENTER);

        //adding to game panel
        gamePan.add(endGameBut, BorderLayout.SOUTH);
        gamePan.add(headingLab, BorderLayout.NORTH);
        gamePan.add(gameInfoPan, BorderLayout.CENTER);
        gamePan.add(scorePan, BorderLayout.EAST);

        //setting up container panel and adding each screen to it
        contPan = new JPanel();
        contPan.setLayout(cl);
        contPan.add(startPan, "Start Applet Screen");
        contPan.add(setupPan, "Setup Game Screen");
        contPan.add(gamePan, "New Game Screen");

        //action listeners
        setupGameBut.addActionListener((ActionEvent e) -> {
            newGame();
        });

        startGameBut.addActionListener((ActionEvent e) -> {
            startGame();
        });

        cancelBut.addActionListener((ActionEvent e) -> {
            quitGame();
        });

        endGameBut.addActionListener((ActionEvent e) -> {
            quitGame();
        });

        //add container panel
        this.add(contPan);
    }

    @Override
    public void paint(Graphics g) {

    }

    public void newGame() {
        cl.show(contPan, "Setup Game Screen");
    }

    public void startGame() {
        cl.show(contPan, "New Game Screen");
    }

    public void quitGame() {
        cl.show(contPan, "Start Applet Screen");
        if (currentScore > highScores[difficulty.getSelectedIndex()]) {
            highScores[difficulty.getSelectedIndex()] = currentScore;
        }
        currentScore = 0;
    }

    @Override
    public Insets getInsets() {
        return new Insets(10, 10, 10, 10);
    }
}

Don't override paint() .

There is rarely ever a reason to do this and I can't think of any reason for having an empty method. An empty method is bound to cause problems

I'm using it later in my program for painting stuff

Still don't override paint(). Custom painting is done by overriding the paintComponent(...) method of a JPanel (or JComponent) and then you add the panel to the applet.

Read the section from the Swing tutorial on Custom Painting for more information and examples.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM