2013-04-06 98 views
3

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

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); 
       } 
       } 
+0

为更好地帮助更快张贴[SSCCE(http://sscce.org/),短,可运行,编译,否则回答您非常模糊的问题不能......,确保之前阅读[Oracle教程如何制作对话框](http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html) – mKorbel 2013-04-06 16:26:27

回答

14

你应该真正从选项窗格拍摄效果:

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

否则,它仍然设置为​​。

清洁是:

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

虽然,我不知道这是什么线,预计在发布代码来完成:remove(dialogButton);

有关更多详细信息和示例,请查看How to Make Dialogs教程。

1

更改代码

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

+1

这工作谢谢 – 2013-04-06 16:20:17

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;,ch如果你想消失了的JOptionPane然后折边到

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

这不是问题。否则,如果不工作我 – 2013-04-06 16:22:20

0

,您可以:

optionPane.setVisible(false); 

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

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

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

这是正确的!

+0

但删除(dialogBu​​tton);是错误的 – gus 2015-06-28 07:01:55

+1

欢迎来到StackOverflow!你可以[编辑]你的帖子并删除你的评论。请继续,并将您的评论添加到您的答案。当你处理它时,你应该描述为什么你认为这是一个很好的解决方案,为什么'remove()'是错误的。 – 2015-06-28 18:32:09

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