0

所以,我已经被这段代码难住了大约一个星期,我想让代码在用户选择“否”或“取消”时发送一条错误消息,但是我收到一个错误,告诉我不和CANCEL 不是变量。有人对我如何克服这个问题有任何建议吗?

int mc = JOptionPane.QUESTION_MESSAGE;
    int bc = JOptionPane.YES_NO_CANCEL_OPTION;

    int ch = JOptionPane.showConfirmDialog (null, "Select:", "Title", bc, mc);

    if (bc == NO)
    {
        JOptionPane.showInputDialog("Sorry, you cannot continue without agreeing to the rules.");
    }
    else if (bc == CANCEL)
    {
        JOptionPane.showInputDialog("Sorry, you cannot continue without agreeing to the rules.");
    }
    else
    {
        JOptionPane.showInputDialog("Thank you, you may continue!");
    }
4

1 回答 1

1

这一切都在 JOptionPane Javadoc 中进行了解释。

在您提供的代码中,您单击的按钮由showConfirmDialog您分配给的返回值 from 标识,而ch不是bc。在你的情况下,逻辑应该是

if (ch == JOptionPane.NO_OPTION) {
    ...
}
else if (ch == JOptionPane.CANCEL_OPTION) {
    ...
}
else {
    ...
}
于 2015-10-11T08:52:00.877 回答