【问题标题】:Restarting java game based on selection of JOptionPane基于选择 JOptionPane 重启 java 游戏
【发布时间】:2013-04-12 08:34:46
【问题描述】:

我有一个简单的游戏,其中有一个 JOptionPane 弹出窗口,可以选择是或否。

因此,如果用户点击“否”,那么游戏当然会关闭,但如果用户点击“是”,我希望游戏重新启动。

我通过以下方式使它工作:(只需再次调用 main() 方法。 main.Main.main(null);

问题:

它正在开始游戏的另一个实例,但前一个实例也在那里。

如何在启动新实例之前关闭之前的实例?或者还有什么其他方法可以解决这个问题?

这是带有 JOptionPane 的类代码

    public void popupWinMessage(String message)
{
     //default icon, custom title
    int n = JOptionPane.showConfirmDialog(
        null,
        message,
        "",
        JOptionPane.YES_NO_OPTION);

    if(n == JOptionPane.YES_OPTION){
        JOptionPane.showMessageDialog(null, "Alright, here we go again");
        main.Main.main(null);
    }
    else {
        JOptionPane.showMessageDialog(null, "Thanks for playing the battleships game");
        System.exit(0);
    }

这是我更新的主要方法(我仍然对发生的事情感到很困惑):

package main;

imports

public class Main {

public Controller theController = new Controller(new Game(), new Frame());
/**
 * @param args
 */
public static void main(String[] args) 
{
    newGame();
}


public static void newGame()
{
    theController.;
}

}

我整个游戏的目录是这样的

http://i.imgur.com/7iKJQwu.png

JOption窗格所在的方法在视图的Frame类中。

public Controller(final Game newGame, final Frame newView) 
{
    this.game = newGame;

    this.view = newView;
    this.view.setVisible(true);


    //Gets the board arrays into these local variables here for us to use
    playerBoard = newGame.DisplayPlayerBoard();
    computerBoard = newGame.DisplayCompBoard();


    colourGrids();

    this.view.addGridActionListener(new ActionListener() 
    {
        @Override
        public void actionPerformed(ActionEvent ae) 
        {
            Object o = ae.getSource();
            if(o instanceof JButton) 
            {
                JButton btn = (JButton) o;

                int xValue = (Integer) btn.getClientProperty("row");
                int yValue =  (Integer) btn.getClientProperty("column");

                int tempShipType = computerBoard[xValue][yValue];

                newGame.tryHitComputer(xValue, yValue);

                btn.setEnabled(false);


                if(tempShipType != -1)
                {
                   if(newGame.checkShipStatus(tempShipType) == true)
                      {
                         System.out.println(shipDowns[tempShipType]);
                         newView.popupMessage(shipDowns[tempShipType]);
                      }
                }

                //Code here for the computer to try hit your ships

                newGame.tryHitPlayer();



                colourGrids();

                System.out.println(newGame.isGameWon());

                if (newGame.isGameWon() == true)
                {
                    System.out.println("Game has been won, do something to stop it at some point");
                    newView.popupWinMessage("Game has been won\nDo you wish to play again?");
                }
                if (newGame.isGameLost() == true)
                {
                    System.out.println("Game has been lost, the silly AI has beaten you\nDo you wish to play again?");
                    newView.popupWinMessage("Game has been lost, the silly AI has beaten you\nDo you wish to play again?");
                }

            }
            else 
            {
                System.out.println("The listener was not attached to a JButton as expected - " + o.getClass());
            }
        }
    });





}

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE。在将标签贴在帖子上之前,请务必阅读标签的弹出窗口。这与 SO 中使用的restart 无关。

标签: java swing main


【解决方案1】:

不要再次调用 main 方法。有一个 start() 方法来启动你的游戏并让对话框在答案为“是”时调用它

【讨论】:

    【解决方案2】:

    一种选择是将restartGame() 方法添加到您的Controller 类。由于您从Controller 构造函数调用popupWinMessage(),因此您可以修改它以获取Controller 对象并将this 引用传递给它。那么popupWinMessage() 方法可以简单地在传递给它的实例上调用restartGame() 方法。

    【讨论】:

    • 我不太了解这个。在 main 方法所在的 Main 包中,我基本上将代码从创建游戏新实例的 main 方法中取出,并将其放入名为 newGame() 的公共静态 void 方法中。在 main() 方法中,我放置了 newGame() 方法。在控制器中,我放置了一个调用主要方法 newGame() 的方法。然后在我看来,当他们选择 JOption 时,我调用了 Controllers 方法。但是它不起作用,因为在我看来,我无法执行 controller.Controller.restartGame(); 方法。 - 不让我(使用 Eclipse)
    • 是的,仍然将之前的游戏实例留在后台 - i.imgur.com/hE4SfeG.png
    • 我在main方法中创建了Controller的引用:public Controller theController = new Controller(new Game(), new Frame()); ...现在我只是不知道我应该在控制器本身的 restartGame() 方法中做什么
    • @Jagga 请编辑您的问题以显示一些简化的代码来说明您所做的工作。
    • 嗯,这就是我的主要方法目前的样子:i.imgur.com/WPCG58J.png
    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多