[英]Closing Java Frame
I have created a java game and have implemented a GameOver method. 我创建了一个Java游戏并实现了GameOver方法。 This is the code i have for the game over method:
这是我的游戏结束方法的代码:
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);
}
}
An issue i am having is closing the original frame. 我遇到的一个问题是关闭原始框架。 For eg if i click yes the Game frame will open, however, it is already open so i would have two instances of the game working.
例如,如果我单击“是”,游戏框架将打开,但是它已经打开,因此我将有两个游戏实例。
Just to let you guys know, i have tried the following options: 为了让大家知道,我尝试了以下选择:
frame.dispose();
WindowEvent wev = new WindowEvent(this, WindowEvent.WINDOW_CLOSING);
Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(wev);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
I was also thinking of doing an actionperformed event, however, i was unable due to the JOptionPane being an int. 我也在考虑做一个动作执行的事件,但是,由于JOptionPane是一个int,所以无法执行。
Your gameOver
method should be like this: 您的
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);
}
}
EDIT 编辑
Aside all thse , You have two variables named frame
first one is instance variable which is frame = new JFrame("Game");
除了所有这些,您还有两个名为
frame
变量,第一个是实例变量,它是frame = new JFrame("Game");
and other is final JFrame frame = new JFrame("The Birds Game");
另一个是
final JFrame frame = new JFrame("The Birds Game");
you should change the variable name of other JFrame
.. 您应该更改其他
JFrame
的变量名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.