问题
I got an JOptionPane and yes and no buttons. But, whichever button you click it still exists. HELP! Heres the code:
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);
}
}
回答1:
You should actually take the result from the option pane:
dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);
Otherwise, it remains set to JOptionPane.YES_NO_OPTION
.
Cleaner would be:
if (JOptionPane.showConfirmDialog(null, "Are you sure?", "WARNING",
JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
// yes option
} else {
// no option
}
Although, I'm not sure what this line is expected to do in the posted code: remove(dialogButton);
.
For more details and examples check out How to Make Dialogs tutorial.
回答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);}
this is the correct!
回答3:
Change the code to
int dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);
回答4:
if(dialogButton == JOptionPane.YES_OPTION) { // <<< start
System.exit(0);
if(dialogButton == JOptionPane.NO_OPTION) {
remove(dialogButton);
}
}// <<< stop
The result is caused by the fact that the outer if
encloses the other if
statement, make sure you don't next the if
statement, it should be as follows : -
if(dialogButton == JOptionPane.YES_OPTION) {
System.exit(0);
}else {
remove(dialogButton);
}
Another thing is this line int dialogButton = JOptionPane.YES_NO_OPTION;
, change it to
int dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);
回答5:
If you want the JOptionPane to be gone then you can:
optionPane.setVisible(false);
If you don't then look at a different answer.
回答6:
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);
}
来源:https://stackoverflow.com/questions/15853112/joptionpane-yes-no-option