Java 点击菜单栏后尝试开始一个新的井字游戏

Java 点击菜单栏后尝试开始一个新的井字游戏,java,swing,user-interface,Java,Swing,User Interface,我有一个用javagui制作的tic-tac-toe游戏,试图通过让用户点击菜单栏来实现重启游戏的方法 请让我知道还有什么其他方法可以尝试获得工作重启功能 到目前为止,我尝试的是在单击菜单栏项时调用playGame()方法,但由于playGame方法中的while循环使其卡住,因此这不起作用 公共交通 { /*创建JFrame并设置大小和可见性*/ 框架=新的JFrame(“Tic Tac Toe”); frame.setDefaultCloseOperation(JFrame.EXIT_ON

我有一个用javagui制作的tic-tac-toe游戏,试图通过让用户点击菜单栏来实现重启游戏的方法

请让我知道还有什么其他方法可以尝试获得工作重启功能

到目前为止,我尝试的是在单击菜单栏项时调用playGame()方法,但由于playGame方法中的while循环使其卡住,因此这不起作用


公共交通
{
/*创建JFrame并设置大小和可见性*/
框架=新的JFrame(“Tic Tac Toe”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
/*创建菜单栏*/
JMenuBar menubar=新的JMenuBar();
JMenu游戏=新JMenu(“游戏”);
新游戏=新项目(“新游戏”);
退出=新项目(“退出”);
newGame.addActionListener(newActionListener())
{
已执行的公共无效操作(操作事件evt){
执行新游戏操作(evt);
}
});
quit.addActionListener(新建ActionListener())
{
已执行的公共无效操作(操作事件evt){
已执行的操作(evt);
}
});
game.add(newGame);
游戏。添加(退出);
菜单栏。添加(游戏);
frame.setJMenuBar(菜单栏);
/*将文本区域添加到框架*/
frame.add(scrollableTextArea);
frame.setLocationRelativeTo(空);
框架。设置尺寸(400400);
frame.setVisible(假);
}
已执行的私有操作(ActionEvent evt){
系统出口(0);
}
私有void newGameActionPerformed(ActionEvent evt){
透明板();
}
公共游戏
{
frame.setVisible(true);
clearBoard();//清除该板
//循环直到游戏结束
虽然(胜者==空){//游戏仍在进行中
while(playerWent==false){
if(topRight.getModel().isPressed()){
右上角.setText(播放器);
topRight.setEnabled(false);
playerWent=true;
} 
else if(topMid.getModel().isPressed()){
topMid.setText(播放器);
topMid.setEnabled(false);
playerWent=true;
}
else if(topLeft.getModel().isPressed()){
左上角.setText(播放器);
左上角。设置启用(false);
playerWent=true;
}
else if(midRight.getModel().isPressed()){
中右。塞特克斯(球员);
右中。设置已启用(false);
playerWent=true;
}
else if(center.getModel().isPressed()){
中锋。塞特克斯(球员);
center.setEnabled(false);
playerWent=true;
}
else if(midLeft.getModel().isPressed()){
中左。塞特克斯(球员);
左中。设置已启用(false);
playerWent=true;
}
else if(botRight.getModel().isPressed()){
botRight.setText(播放器);
botRight.setEnabled(false);
playerWent=true;
}
else if(botMid.getModel().isPressed()){
botMid.setText(播放器);
botMid.setEnabled(false);
playerWent=true;
}
else if(botLeft.getModel().isPressed()){
botLeft.setText(播放器);
botLeft.setEnabled(false);
playerWent=true;
}  
} 
numFreeSquares--;//减少自由方块的数量
//看看比赛是否结束
if(haveWinner()){
winner=player;//必须是刚才去的玩家
如果(赢家相等(玩家X)){
gameState.setText(“玩家X获胜”);
返回;
}否则{
gameState.setText(“玩家O获胜”);
返回;
}
}
else if(numFreeSquares==0){
winner=TIE;//棋盘已满,所以是平局
gameState.setText(“平局游戏”);
返回;
}
//更改为其他玩家(如果游戏结束,这将不会起任何作用)
if(player==player_X){
player=player\u O;
gameState.setText(“游戏进行中,轮到玩家O”);
playerWent=false;
}
否则{
player=player_X;
gameState.setText(“游戏进行中,轮到玩家X”);
playerWent=false;
}
}
}

循环将覆盖您试图实现的重启功能。我的建议是,您应该尝试重构代码,使其以事件驱动的方式工作。简而言之:事件驱动编程是响应(并确认)用户输入的用户界面。您可以通过一个管理游戏回合的类来实现这一点

以下是一些示例代码供参考,我已经将其用于我自己的tic-tac-toe项目(JavaFX框架):

private void playersGameProcess(){
//循环通过按钮字段添加鼠标点击事件,如果按下按钮,游戏状态在交叉和零之间切换。
对于(int i=0;i private void playersGameProcess() {
        //Looping through the button field to add mouseclick events, if there is pressed on a button the game's state switches between CROSS and NOUGHT.
        for (int i = 0; i < buttons2D.length; i++) {
            for (int j = 0; j < buttons2D[i].length; j++) {
                Button button = buttons2D[i][j];
                buttons2D[i][j].setOnMouseClicked(event -> {
                    if (game.getGameState() == "X" && button.getId() == null) {
                        game.incrementTurnCount();
                        System.out.println("Turn: " + game.getTurnCount());
                        notification.setText("It's nought's turn!");
                        game.setGameState("O");
                        button.setId("buttonClickX");
                        checkWinningConditions("X");
                    } else if (game.getGameState() == "O" && button.getId() == null) {
                        game.incrementTurnCount();
                        System.out.println("Turn: " + game.getTurnCount());
                        notification.setText("It's cross's turn!");
                        button.setId("buttonClickO");
                        game.setGameState("X");
                        checkWinningConditions("O");
                    }
                });
            }
        }
    }