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

How to get Java Frame object to use in Reset method?

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

问题是当我尝试对用户单击 "restart" 时发生的内容进行编码。假设调用在 TicTacToe class 中定义的 resetBoard() 方法。问题是,我不知道从 TicTacToe class 创建的对象的名称(在测试器 class' static void main 方法中我只是输入 "new TicTacToe",不需要定义一个名称)。我不能从静态的角度调用 resetBoard(即我不能 TicTacToe.resetBoard(); )因为 resetBoard 需要是非静态的。

我尝试过的: 我试过在 GameEnd class 的构造函数中包含一个 TicTacToe 对象。如果我这样做,GameEnd 对象创建者必须进入 TicTacToe class,因此我可以使用 'this' 关键字。这不起作用,因为在满足 WinCondition 时需要创建 GameEnd 对象,在 XOButton class.

中单击按钮时会检查该对象

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

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


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;
    }

}

这是井字棋 class。大多数代码不相关,因此已被隐藏。


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 class。当满足 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();
    }
}

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

看来你把项目分解得太过分了。如果您有一个游戏的 class,请将其所有方法保留在 class 中。我唯一能想到使用单独的 classes 的情况是,如果您尝试使用 Model-View-Controller (MVC) 体系结构等来创建它。这是使用 GUI、数据访问和控制器的应用程序的常用方法。

可能还有其他的时候,但我能想到的就这些了。

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


我认为这是更好的方法:

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();
    }
}

希望对您有所帮助!

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