Java:JPanel在CardLayout中未正确显示

Java:JPanel在CardLayout中未正确显示,java,swing,applet,layout-manager,cardlayout,Java,Swing,Applet,Layout Manager,Cardlayout,我的JPanelstartPan应该在小程序启动时显示,但是现在,只有JButtonsetupGameBut显示。但如果我单击setupGameBut,然后单击cancelBut返回startPan,它会显示规则,但仍然不会显示JLabelheadingLab(它会显示在setupPan上) 除此之外,它还会在当前显示的面板后面显示来自其他面板的组件,但这些组件无法与之交互 单击按钮时,如果在加载下一张卡之前将光标移离该按钮,则在再次鼠标悬停之前,该按钮不会更新为应该显示在那里的按钮。我有一种感

我的
JPanel
startPan应该在小程序启动时显示,但是现在,只有
JButton
setupGameBut显示。但如果我单击setupGameBut,然后单击cancelBut返回startPan,它会显示规则,但仍然不会显示
JLabel
headingLab(它会显示在setupPan上)

除此之外,它还会在当前显示的面板后面显示来自其他面板的组件,但这些组件无法与之交互

单击按钮时,如果在加载下一张卡之前将光标移离该按钮,则在再次鼠标悬停之前,该按钮不会更新为应该显示在那里的按钮。我有一种感觉,我的代码有一些根本性的错误,我有这些问题,但我不确定

/*
*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);
    }
}
不要覆盖
paint()

很少有理由这样做,我想不出有什么理由使用空方法。空方法必然会导致问题

我稍后会在我的绘画程序中使用它

仍然不覆盖paint()。自定义绘制通过重写JPanel(或JComponent)的
paintComponent(…)
方法完成,然后将面板添加到小程序中


有关更多信息和示例,请阅读上Swing教程的部分。

不要覆盖
paint()
。很少有理由这样做,我想不出有什么理由使用空方法。空方法肯定会引起问题。@camickr我稍后将在我的绘画材料程序(大学的应用程序用户界面模块)中使用它,因此我们必须展示我们知道如何使用图形类绘制材料。但这解决了一切。谢谢我必须找到一种方法,当我实现它时,它不会破坏一切…@camickr我不知道在这种情况下该怎么办。我是否要删除该问题?还是要添加答案?添加了包含更多信息的答案。