showInputDialog使“if”工作错误

时间:2014-12-17 22:22:44

标签: java input joptionpane

我制作简易版的扫雷并且对这部分代码有疑问:

if ((uncover == 0) && inGame) {
        inGame = false;
        long elapsedTime = (System.currentTimeMillis() - start)/1000;
        JOptionPane.showInputDialog(null, "Congrats, you won! \n Your result is "
                + elapsedTime + " sec. Please, enter your name: ");
        statusbar.setText("Game won");
        }
    else if (!inGame)
        statusbar.setText("Game lost");

所以这就是事情:如果游戏获胜,它会显示输入对话框,但状态栏仍然会变为"游戏丢失"。如果我删除或评论此

JOptionPane.showInputDialog(null, "Congrats, you won! \n Your result is "
                + elapsedTime + " sec. Please, enter your name: ");

一切正常。问题是什么?

1 个答案:

答案 0 :(得分:0)

您需要捕获输入,并且您没有填写所有可能导致警告或编译错误的inputdialog选项。

String getInput = "";
getInput = (String)JOptionPane.showInputDialog(
        null,
        "Congrats, you won! \n Your result is "
            + elapsedTime + " sec. Please, enter your name: ",
        null,
        JOptionPane.PLAIN_MESSAGE,
        null,
        null,
        null);

希望这有帮助