简体   繁体   English

完成游戏后如何重置JFrame?

[英]How do i reset JFrame after completing a game?

So I made a TicTacToe Game and everything is working but I don't know how to reset the gameboard. 因此,我制作了一个TicTacToe游戏,并且一切正常,但是我不知道如何重置游戏板。 Code: 码:

public TicTacToeV1(){
/*Create window*/
window.setSize(300,300);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLayout(new GridLayout(3,3));
window.setResizable(false);
window.setVisible(true);
/*add Buttons to the window*/
window.add(button1);
window.add(button2);
window.add(button3);
window.add(button4);
window.add(button5);
window.add(button6);
window.add(button7);
window.add(button8);
window.add(button9);
/*Add The Action Listener To The Buttons*/
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
button6.addActionListener(this);
button7.addActionListener(this);
button8.addActionListener(this);
button9.addActionListener(this);
}

This is the gameboard. 这是游戏板。

/*Restart the game*/
if(win == true){
int returnValue = JOptionPane.showOptionDialog(null, "Play Again?", "Play Again?", JOptionPane.YES_NO_OPTION, 0, null, null, null);
new TicTacToeV1();
}else if(count == 9 && win == false){
int returnValue = JOptionPane.showOptionDialog(null, "Play Again?", "Play Again?", JOptionPane.YES_NO_OPTION, 0, null, null, null);
new TicTacToeV1();
}

This is how i currently are resetting the game. 这就是我目前正在重置游戏的方式。

public static void main(String[] args){
 new TicTacToeV1();
    }

Edit: i have not added a no option yet, so yes is the same to no. 编辑:我还没有添加否选项,所以是与否相同。

Create a user defined reset() in that you can define the needed components default state. 创建用户定义的reset() ,您可以定义所需组件的默认状态。 And call the method where you want to restart. 并在要重新启动的位置调用该方法。

There are two options that you have here. 您有两个选择。 Both of these methods have been covered by the other answers but I will try to summarize them: 这两种方法都已包含在其他答案中,但我将尝试总结一下:

  • Create a new instance of the game 创建游戏的新实例

In this method, you automatically close your previous JFrame using the dispose method and just start a new TicTacToeV1 from the game restart logic. 在这种方法中,您将使用dispose方法自动关闭之前的JFrame ,并从游戏重启逻辑中启动一个新的TicTacToeV1

  • Set all the components back to their original state 将所有组件恢复为原始状态

With this method, you just make all the components look like they did at the beginning of the game. 使用这种方法,您只需使所有组件看起来像它们在游戏开始时一样。 Without seeing the game it is difficult to say exactly what this would entail. 如果不看游戏,很难确切地说出这将带来什么。 It would most likely involve taking Os and Xs off of buttons and resetting any counters that you have. 最有可能涉及从按钮上取消Os和Xs并重置您拥有的所有计数器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM