完成游戏后如何重置JFrame?

时间:2013-12-18 16:59:06

标签: java swing

所以我做了一个TicTacToe游戏,一切正常,但我不知道如何重置游戏板。 代码:

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

这是游戏板。

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

这就是我目前正在重置游戏的方式。

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

编辑:我还没有添加任何选项,所以是和否相同。

2 个答案:

答案 0 :(得分:0)

创建用户定义的reset(),您可以定义所需的组件默认状态。 并调用要重新启动的方法。

答案 1 :(得分:0)

这里有两个选项。其他答案都涵盖了这两种方法,但我会尝试总结它们:

  • 创建新的游戏实例

在此方法中,您使用JFrame方法自动关闭之前的dispose,然后从游戏重启逻辑中启动新的TicTacToeV1

  • 将所有组件设置回原始状态

使用此方法,您只需使所有组件看起来像在游戏开始时一样。没有看到游戏,很难准确说出这将带来什么。它很可能涉及从按钮上取下Os和Xs并重置你拥有的任何计数器。