我有一个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);
}
}
答案 0 :(得分:16)
您应该从选项窗格中获取结果:
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);
。
有关详细信息和示例,请查看How to Make Dialogs教程。
答案 1 :(得分: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);}
这是正确的!
答案 2 :(得分:1)
将代码更改为
int dialogButton = JOptionPane.showConfirmDialog (null, "Are you sure?","WARNING", dialogButton);
答案 3 :(得分: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);
答案 4 :(得分:0)
如果你希望JOptionPane消失,那么你可以:
optionPane.setVisible(false);
如果不这样做,请查看其他答案。
答案 5 :(得分:-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);
}