麻烦JOptionPane并让它工作

时间:2013-10-24 22:25:19

标签: java swing joptionpane

尝试创建一个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转换为字符串

    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();
        }
}
}

1 个答案:

答案 0 :(得分: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