Java:JPanels在CardLayout中无法正常显示

时间:2014-10-25 19:39:49

标签: java swing applet layout-manager cardlayout

我的JPanel startPan应该在applet启动时显示,但是现在只显示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);
    }
}

1 个答案:

答案 0 :(得分:2)

不要覆盖paint()

很少有理由这样做,我想不出任何有空方法的理由。空方法必然会导致问题

  

我稍后在我的程序中使用它来绘制东西

仍然不要覆盖paint()。自定义绘制是通过覆盖JPanel(或JComponent)的paintComponent(...)方法完成的,然后将面板添加到applet中。

阅读Custom Painting上Swing教程中的部分,了解更多信息和示例。