我已经创建了一个java游戏,并实现了一个GameOver方法。这是我的游戏方法的代码:
public void gameOver() throws IOException {
String message = "Game, Want to try Again?";
String title = "Game Over";
int reply = JOptionPane.showConfirmDialog(frame, message, title, JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION){
new Game();
} else {
this.getSoundEffect().stopAllMusic();
new Main().setVisible(true);
}
}我遇到的一个问题是关闭原始框架。例如,如果我点击是,游戏框架将打开,但是,它已经打开,所以我将有两个游戏实例工作。
为了让你们知道,我尝试了以下选项:
frame.dispose();
WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);我也在考虑做一个动作执行的事件,但是,由于JOptionPane是一个整数,所以我不能做。
发布于 2013-04-02 03:51:33
您的gameOver方法应该如下所示:
Main mMain ;
public void gameOver() throws IOException {
String message = "Game, Want to try Again?";
String title = "Game Over";
int reply = JOptionPane.showConfirmDialog(frame, message, title, JOptionPane.YES_NO_OPTION);
if (reply == JOptionPane.YES_OPTION){
frame.dispose();
frame.setVisible(true);
} else {
this.getSoundEffect().stopAllMusic();
if(mMain==null)
{
mMain = new Main();
}
mMain.setVisible(true);
}
}编辑
除此之外,您有两个名为frame的变量,第一个是实例变量,它是frame = new JFrame("Game");,另一个是final JFrame frame = new JFrame("The Birds Game");,您应该更改其他JFrame的变量名。
https://stackoverflow.com/questions/15750433
复制相似问题