如何使用JDialog获取输入

时间:2014-09-01 02:55:39

标签: java swing jdialog

在SpaceInvadersApp.gameEnded()(如下所示)中,我如何使用JOptionPane.showInputDialog() 在用户初始Game Over消息对话框后显示一个对话框 谁以大于零的分数赢得比赛(isGameWon方法测试这个)。新对话框应告知用户他们已获得高分,并请求他们的名称为高分表。

 public void gameEnded() {
    String message;
    if (game.isGameWon()) {
        message = "You defeated the alien menace!  Congratulations!\n\n"
                + "Your score was " + game.getScore();
    } else {
        message = "Oh no! The aliens have defeated you.";
    }

    JOptionPane.showMessageDialog(this,
            message, "Game Over",
            JOptionPane.INFORMATION_MESSAGE);

    menuItemGamePause.setEnabled(false);
}

1 个答案:

答案 0 :(得分:3)

首先看一下How to Make Dialogs

但基本上,你可以做点像......

if (game.isGameWon()) {
    message = "You defeated the alien menace!  Congratulations!\n\n"
            + "Your score was " + game.getScore();
    String name = JOptionPane.showInputDialog(this, message, "Game Over", JOptionPane.INFORMATION_MESSAGE)
    if (name != null) {
        // Save name
    }
} else {
    message = "Oh no! The aliens have defeated you.";
    JOptionPane.showMessageDialog(this,
            message, "Game Over",
            JOptionPane.INFORMATION_MESSAGE);
}