【问题标题】:Closing Java Frame关闭 Java 框架
【发布时间】:2013-12-21 04:21:44
【问题描述】:

我创建了一个 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);

我也在考虑做一个 actionperformed 事件,但是,由于 JOptionPane 是一个 int,我无法做到。

【问题讨论】:

  • 您是否保留了对已打开的Game 对象的引用?
  • @Aubin,这将关闭整个系统,因此不允许我打开另一个 Java 类。
  • 你应该有..这将帮助你检查游戏窗口是否打开..如果打开了不要做任何其他事情创建新的。
  • @VishalK 我该怎么做?

标签: java netbeans


【解决方案1】:

你的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 的变量名..

【讨论】:

  • 您好,感谢您的回复,但还是不行。游戏冻结,游戏重新打开。
  • @JamesDanny 查看我的更新。我还想问你一件事,如果你的游戏框架打开了..那么你想隐藏Main框架吗?
  • 主机已经被处理掉了。在我的主框架上,它让我可以选择启动我的 Game.Java,当我启动它时,主框架会被释放。
  • 主要问题是框架没有被处理。我不确定为什么。我认为它正在处理一个不同的框架而不是 Game.Java 框架。
  • 你的意思是调用 dispose 后框架的所有内容都没有重置?
猜你喜欢
  • 1970-01-01
  • 2014-11-21
  • 2012-12-25
  • 2010-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-17
相关资源
最近更新 更多