gpt4 book ai didi

Java:JPanel 在 CardLayout 中无法正确显示

转载 作者:行者123 更新时间:2023-12-02 05:20:20 25 4
gpt4 key购买 nike

我的JPanel startPan 应该在小程序启动时显示,但现在只显示JButton setupGameBut。但是,如果我单击 setupGameBut,然后单击 cancelBut 返回到 startPan,它会显示规则,但仍然不显示 JLabel headerLab(它确实显示在 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()

很少有理由这样做,而且我想不出有任何理由使用空方法。空方法必然会出现问题

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

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

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

关于Java:JPanel 在 CardLayout 中无法正确显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26566484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com