0

试图创建一个 joptionpane 来解决为什么这不起作用的任何想法?

public void playerLost() {

    if(player.getStaminaLevel() < player.getStaminaNeededToMove(Terrain.SAND)) {

                JOptionPane.YES_NO_OPTION(

        if (JOptionPane.NO_OPTION) {
            dispose();
        }
        else
        {
            game = new Game();
            createGridSquarePanels();
            update();
        }
}

我也试过这个,但说 JOptionPane.NO_OPTION 不能从 int 转换为 string

    public void playerLost() {

    if(player.getStaminaLevel() < player.getStaminaNeededToMove(Terrain.SAND)) {
        int choice = JOptionPane.showConfirmDialog(
        this, 
                "sorry you lost\nWould you like to restart?",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.INFORMATION_MESSAGE);
        if (choice == JOptionPane.NO_OPTION) {
            dispose();
        }
        else
        {
            game = new Game();
            createGridSquarePanels();
            update();
        }
}
}
4

1 回答 1

1

第一个片段无法编译,因为语法对于使用JOptionPane.

在第二段代码中,您缺少标题参数

int choice = JOptionPane.showConfirmDialog(this, 
                "sorry you lost\nWould you like to restart?",
                "Title",
                JOptionPane.YES_NO_OPTION,
                JOptionPane.INFORMATION_MESSAGE);

查阅JOptionPane javadoc

于 2013-10-24T22:42:34.203 回答