打开其他弹出窗口,如果你确定出确定/取消按钮,然后用JOptionPane.showConfirmDialog和响应返回的值,如果它代表JOptionPane.OK_OPTION:
public static void main(String[] args) {
int optionType = JOptionPane.OK_CANCEL_OPTION;
int messageType = JOptionPane.PLAIN_MESSAGE;
int value = JOptionPane.showConfirmDialog(null, "Whatever",
"Whatever Fun", optionType, messageType);
if (value == JOptionPane.OK_OPTION) {
System.out.println("OK pressed");
}
}
否则,你可以使用JOptionPane.showOptionsDialog只显示OK按钮:
import javax.swing.Icon;
import javax.swing.JOptionPane;
public class JOptionPaneFun {
public static void main(String[] args) {
int optionType = JOptionPane.OK_CANCEL_OPTION;
int messageType = JOptionPane.PLAIN_MESSAGE;
int value = JOptionPane.showConfirmDialog(null, "Whatever",
"Whatever Fun", optionType, messageType);
if (value == JOptionPane.OK_OPTION) {
System.out.println("OK pressed");
}
String message = "Whatever";
String title = "JOptionPane Fun";
Icon icon = null;
Object[] options = { "OK" };
Object initialValue = options[0];
int anotherValue = JOptionPane.showOptionDialog(null, message, title,
optionType, messageType, icon, options, initialValue);
if (anotherValue >= 0 && initialValue.equals(options[anotherValue])) {
System.out.println("OK Pressed Again");
}
}
}
为什么你要连接一个ActionListener到这个按钮?按下时你想采取什么行动? – copeg
这将帮助你,看到一个自定义对话框的这个[示例](http://www.java2s.com/Tutorial/Java/0240__Swing/extendsJDialogtocreateyourowndialog.htm)。 –
点击好的按钮时,你想要做什么? – MadProgrammer