打開其他彈出窗口,如果你確定出確定/取消按鈕,然後用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