2013-04-06 28 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); 
}