gpt4 book ai didi

java - 如何获取 Java Frame 对象以在 Reset 方法中使用?

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

我正在用 Java 制作 Tic-Tac-Toe 游戏。我有四门课:TicTacTester 只是调用(创建一个对象)TicTacToe 类。TicTacToe 类提供游戏的 GUI(它是 JFrame 的子类)。它还创建 9 个按钮供 JPanel 显示并供用户单击。XOButton 类定义了按钮的功能和 actionPerformed 方法。最后,GameEnd 类定义游戏结束时会发生什么(创建一个新的 JFrame 来显示分数并为用户提供 2 个按钮:退出和重新启动)。

问题是当我尝试对用户单击“重新启动”时发生的内容进行编码时。假设调用 ResetBoard() 方法,该方法在 TicTacToe 类中定义。问题是,我不知道从 TicTacToe 类创建的对象的名称(在测试器类的 static void main 方法中我只是输入“new TicTacToe”,不需要定义名称)。我无法从静态角度调用resetBoard(即我无法执行TicTacToe.resetBoard();),因为resetBoard需要是非静态的。

我尝试过的:我尝试在 GameEnd 类的构造函数中包含 TicTacToe 对象。如果我这样做,GameEnd 对象创建者必须进入 TicTacToe 类,因此我可以使用“this”关键字。这不起作用,因为当满足 WinCondition 时需要创建 GameEnd 对象,在 XOButton 类中单击按钮时会检查 WinCondition。

但是如果我将 GameEnd 对象创建器放在 XOButton 类中(现在的位置,应该是它应该在的位置),在 GameEnd(String s, TicTacToe a) 的构造函数中,我就不能使用“this” TicTacToe 对象的关键字。

这是我的按钮类。大多数代码不相关,因此已被隐藏。


public class XOButton extends JButton implements ActionListener {

//Hidden code
private void winCheck() {
for(int j = 0; j < 3; j++) {
if(board[j][0] == 1 && board[j][1] == 1 && board[j][2] == 1 || board[0][j] == 1 && board[1][j] == 1 && board[2][j] == 1) {
player1Score++;
GameEnd end = new GameEnd("X wins this round!");
finished = true;
break;
}
else if(board[j][0] == 2 && board[j][1] == 2 && board[j][2] == 2 || board[0][j] == 2 && board[1][j] == 2 && board[2][j] == 2) {
player2Score++;
GameEnd end = new GameEnd("O wins this round!");
finished = true;
break;
}
}

if(board[0][0] == 1 && board[1][1] == 1 && board[2][2] == 1 || board[0][2] == 1 && board[1][1] == 1 && board[2][0] == 1) {
player1Score++;
GameEnd end = new GameEnd("X wins this round!");
finished = true;
}
else if(board[0][0] == 2 && board[1][1] == 2 && board[2][2] == 2 || board[0][2] == 2 && board[1][1] == 2 && board[2][0] == 2) {
player2Score++;
GameEnd end = new GameEnd("O wins this round!");
finished = true;
}
if(turn == 9 && !finished) {
GameEnd end = new GameEnd("This round is a Draw");
finished = true;
}

}

public void resetButton() {
this.setIcon(null);
markSpot(0);
this.clicked = false;
}

public static void resetStatics() {
turn = 0;
finished = false;
}

}

这是 TicTacToe 类。大多数代码不相关,因此已被隐藏。


public class TicTacToe extends JFrame {

//Hidden code

public void resetBoard() {
for(int j = 0; j < 3; j++) {
for(int i = 0; i < 3; i++) {
buttons[j][i].resetButton();
}
}
XOButton.resetStatics();
}


}

这是 GameEnd 类。当满足 WinCondition 时,将创建此对象。大多数代码不相关,因此已被隐藏。

public class GameEnd extends JFrame implements ActionListener {

//Hidden code

public void actionPerformed(ActionEvent e) {
if(e.getSource() == exit) {
System.exit(0);
}
else if(e.getSource() == retry) {
TicTacToe.resetBoard();
}

}

}

//This is the Tester class

public class TicTacTester {
public static void main(String[] args) {
new TicTacToe();
}
}

我期望发生的是游戏板重新启动。

最佳答案

看来您对项目的分解太过分了。如果您有一个游戏类,请将其所有方法保留在该类中。我唯一能想到使用单独的类的情况是,如果您尝试使用模型- View - Controller (MVC) 架构等来创建它。这是使用 GUI、数据访问和 Controller 的应用程序的常见方法。

可能还有其他时候,但我脑子里只能想到这些。

从您所展示的内容来看,我认为它不适用。

<小时/>

我认为这将是一个更好的方法:

TicTacToe.java

public class TicTacToe extends JFrame {
private int scorePlayer1;
private int scorePlayer2;
private boolean initialized; // default = false

// Reset the board
// Reset the markers, etc.
public void resetGame() {}

// Check to see if there is a winner
// If so, announce the winner and return true
// Otherwise, return false.
public boolean winCheck() {}

// Set up frame, buttons, score, etc.
public void newGame(){
// Create however many JPanels you need
// Add it to a single JFrame

// When you're ready...
// and this game has not been initialized for the first time.
// In this example jframe is an instance of JFrame
if (!this.initialized)
jframe.setVisible(true);
}

// Play the game
public void play(){
// For each round,
// a player chooses where to put their "X" or "O"
// add scores, etc.

// Then at the end of each round
if (this.winCheck())
this.resetGame();
}
}
<小时/>

TicTacToeTester.java:

public class TicTacToeTester {
public static void main(String[] args){
TicTacToe game = new TicTacToe();
game.play();
}
}
<小时/>

希望这会有所帮助!

如果您有任何疑问,请告诉我。 :)

关于java - 如何获取 Java Frame 对象以在 Reset 方法中使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56319262/

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