gpt4 book ai didi

java - 在 Java Swing 中使用从一个类到另一个类的变量

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

我正在创建一个问答游戏,但 StartGamePanel 中的变量存在一些问题。我有开始游戏面板、游戏结束面板和主菜单面板。我在开始游戏面板中的分数变量最初为 0,但对于每个用户的正确答案,它都会增加 100 分。我的想法是,如果用户给出错误答案,则在游戏结束面板中向他显示​​他的总分是多少。但不幸的是,分数变量为 0。在开始游戏面板中,游戏 UI 显示正确的分数,并且逻辑正常工作,为每个正确答案添加 100 分,但当我尝试在游戏结束面板中使用分数变量时,它每次都是 0时间。这是我的 StartGamePanel:

public class StartGamePanel extends JPanel {
...
private GameData gameData;

public StartGamePanel(Frame frame, GameData gameData) {
this.frame = frame;
this.gameData = gameData;
...//creating the upper panel and set defaults for the panel

//label for time
timeLabel = new JLabel("Time: " + gameData.getTime());
timeLabel.setFont(font);
timeLabel.setForeground(Color.BLACK);

/** updating the timeLabel */
timer = new Timer(1000, new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (gameData.getStartTime() > 0) {
//getStartTime -= 1000;
//time--;
gameData.updateStartTime(-1000);
gameData.updateTime(-1);
if (gameData.getTime() == 0) {
frame.swapView("gameover");
}
timeLabel.setText("Time: " + gameData.getTime());
timeLabel.repaint();
}
}
});
timer.start();

//getting instance of QuestionRandomizer;
QuestionRandomizer qRand = new QuestionRandomizer();


/*question 1 panel*/
//------------------
//------------------
//------------------
JPanel card1 = new JPanel(new GridBagLayout());
card1.setBackground(new Color(0, 128, 43));
Question q1 = qRand.getRandomQuestion();

//label for the first question
JLabel question1Label = new JLabel(q1.getText());
question1Label.setFont(questionLabelsFont);
question1Label.setForeground(Color.BLACK);

//gbc for question1Label
...

card1.add(question1Label, gbc2);

//Panel holding buttons
JPanel buttonsPanel = new JPanel(new GridLayout(2, 2));


//Answer buttons

JButton answer1 = new JButton(q1.getAnswers()[0].getText()+ "");
answer1.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if (q1.getAnswers()[0].getIsCorrect() == true) {
CardLayout cl = (CardLayout) cards.getLayout();
cl.show(cards, "question2");

//updating the scores
gameData.updateScore(100);
scoresLabel.setText("Scores: " + gameData.getScore());
scoresLabel.repaint();

//updating the timer
timer.stop();
gameData.setTime(16);
gameData.setStartTime(16*1000);
timer.start();
}

//IF THE ANSWER IS INCORRECT GO TO THE GAMEOVER PANEL!!!
if (q1.getAnswers()[0].getIsCorrect() == false) {
frame.swapView("gameover");
}
}
});
//I have 3 more buttons for the answers in this question card and more questions cards but the logic is the same for everything.
public GameData getGameData() {
return this.gameData;
}

看看我的游戏结束面板:

public class GameOverPanel extends JPanel {
private StartGamePanel sgp;
private JLabel gameOverLabel;
private GameData gameData;

public GameOverPanel(StartGamePanel sgp) {
this.sgp = sgp;
this.gameData = sgp.getGameData(); // i created a method in the start game panel which returns start game panel's gameData object
//default settings
this.setLayout(new GridBagLayout());
this.setBackground(new Color(0, 128, 43));
this.setBorder(new EmptyBorder(10, 10, 10, 10));


gameOverLabel = new JLabel("");


gameOverLabel.setText("<html><h1>You didn't answer correctly!</h1><hr><h2></h2><h3>Your score: " + gameData.getScore() + "</h3></html>");

gameOverLabel.setForeground(Color.BLACK);

//constraints for the gameoverlabel
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.gridwidth = GridBagConstraints.REMAINDER;

this.add(gameOverLabel, gbc);

}
}

MainFrame 类切换面板:

public class Frame extends JFrame {
CardLayout cl = new CardLayout();
GameData gameData = new GameData();
MainMenu menuPanel = new MainMenu(this);
StartGamePanel startPanel = new StartGamePanel(this, gameData);
GameOverPanel gameOverPanel = new GameOverPanel(startPanel);


public Frame() {
this.setLayout(cl);
this.setSize(800, 600);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
//this.setEnabled(true);


Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int screenWidth = screenSize.width;
int screenHeight = screenSize.height;

this.setLocation((screenWidth - getWidth()) / 2, (screenHeight - getHeight()) / 2);

this.add(menuPanel, "menu");
this.add(startPanel, "start");
this.add(gameOverPanel, "gameover");
swapView("menu");
}

public void swapView(String view) {
cl.show(getContentPane(), view);
}
}

最佳答案

您没有正确建模您的问题域。这就是你面临这个问题的原因。您应该将 questionscoretime 等保留在单独的域类中(例如 GameData),而不是在JPanel 类。

在用户开始游戏的事件处理程序中,您应该首先创建一个 GameData 实例,然后将该实例传递给您的 StartGamePanel 面板类,例如,作为构造函数参数。同样,在用户结束游戏的事件处理程序中,您应该使用传递给 spg 的相同 GameData 实例,并将其传递给 GameOverPanel

编辑:

根据您提供的进一步信息,除了上述将游戏数据分离到独立 GameData 类的建议之外,您还需要执行以下操作:

GameOverPanel 类中添加一个新的 setter 方法。

public void setGameData(GameData gd) {
this.gameData = gd;
}

startGamepanel 类中添加一个新的 getter 方法。

public void getGameData() {
return gamedata;
}

将主框架类中的 swapView 方法更新为以下内容:

public void swapView(String view) {
if("gameover".equals(view)) {
gameOverPanel.setGameData(startPanel.getGameData());
}
cl.show(getContentPane(), view);
gameOverLabel.setText("<html><h1>You didn't answer correctly!</h1><hr><h2></h2><h3>Your score: " + gameData.getScore() + "</h3></html>");

}

关于java - 在 Java Swing 中使用从一个类到另一个类的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43048658/

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