4

我有一个 JOptionPane 和是和否按钮。但是,无论您单击哪个按钮,它仍然存在。帮助!继承人的代码:

int dialogButton = JOptionPane.YES_NO_OPTION;
            JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);
            if(dialogButton == JOptionPane.YES_OPTION) {
                System.exit(0);
            if(dialogButton == JOptionPane.NO_OPTION) {
                  remove(dialogButton);
                }
              }
4

6 回答 6

21

您实际上应该从选项窗格中获取结果:

dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);

否则,它仍然设置为JOptionPane.YES_NO_OPTION

清洁剂将是:

if (JOptionPane.showConfirmDialog(null, "Are you sure?", "WARNING",
        JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
    // yes option
} else {
    // no option
}

虽然,我不确定这一行在发布的代码中应该做什么:remove(dialogButton);.

有关更多详细信息和示例,请查看如何制作对话框教程。

于 2013-04-06T16:16:59.253 回答
3
int dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING",JOptionPane.YES_NO_OPTION);

if(dialogButton == JOptionPane.YES_OPTION) {
System.exit(0);}else {remove(dialogButton);}

这是正确的!

于 2015-06-28T06:58:19.993 回答
1

将代码更改为

int dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);

于 2013-04-06T16:18:08.950 回答
0
if(dialogButton == JOptionPane.YES_OPTION) { // <<< start
    System.exit(0);
        if(dialogButton == JOptionPane.NO_OPTION) {
            remove(dialogButton);
        }
}// <<< stop

结果是由于外部if包含另一个if语句,请确保您没有在if语句旁边,它应该如下: -

if(dialogButton == JOptionPane.YES_OPTION) {
    System.exit(0);
}else {
    remove(dialogButton);
}

另一件事是这一行int dialogButton = JOptionPane.YES_NO_OPTION;,将其更改为

int dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);
于 2013-04-06T16:19:33.607 回答
0

如果您希望 JOptionPane 消失,那么您可以:

optionPane.setVisible(false);

如果你不那么看一个不同的答案。

于 2014-03-29T23:28:21.077 回答
-1
if (JOptionPane.showConfirmDialog(this, "sfd", "sd", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
    jProgressBar1.setValue(jProgressBar1.getValue() + 10);
    jProgressBar1.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
}
else {
    System.exit(0);
}
于 2016-02-09T11:25:53.790 回答