如何处理JOptionPane中的取消按钮

我创建了一个showInputDialog类型的JOptionPane 。 当它打开时,它会显示两个按钮: 确定取消 。 当我按下取消按钮时,我想处理动作,但我不知道如何达到它。 我怎么才能得到它?

例如:

 int n = JOptionPane.showConfirmDialog( frame, "Would you like green eggs and ham?", "An Inane Question", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) { } else if (n == JOptionPane.NO_OPTION) { } else { } 

或者使用showOptionDialog

 Object[] options = {"Yes, please", "No way!"}; int n = JOptionPane.showOptionDialog(frame, "Would you like green eggs and ham?", "A Silly Question", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]); if (n == JOptionPane.YES_OPTION) { } else if (n == JOptionPane.NO_OPTION) { } else { } 

有关更多详细信息,请参见如何创建对话框 。

编辑: showInputDialog

 String response = JOptionPane.showInputDialog(owner, "Input:", ""); if ((response != null) && (response.length() > 0)) { } 

showMessageDialog不应该显示两个按钮,因此您的代码或对它的解释会有问题。 无论如何,如果您想让用户选择并想要检测该选择,请不要使用showMessageDialog而是使用showConfirmDialog,并返回返回的int并测试它以查看它是否为JOptoinPane.OK_OPTION。

这是一个老问题,我是一个Java新手所以可能有更好的解决方案,但我想知道相同,也许它可以帮助其他人,我做的是检查响应是否为空。

这对我有用:

 //inputdialog JOptionPane inpOption = new JOptionPane(); //Shows a inputdialog String strDialogResponse = inpOption.showInputDialog("Enter a number: "); //if OK is pushed then (if not strDialogResponse is null) if (strDialogResponse != null){ (Code to do something if the user push OK) } //If cancel button is pressed else{ (Code to do something if the user push Cancel) } 
 package Joptionpane; import javax.swing.JOptionPane; public class Cancle_on_JOptionPane { public static void main(String[] args) { String s; int i; for (i=0;i<100;i++){ s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?"); try { if (s.equals("")) { JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=2; } else { JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=100; } } catch (Exception e) { JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=100; } } } } 

你可以这样做:

 String val = JOptionPane.showInputDialog("Value: "); if(val == null){ // nothing goes here if yo don't want any action when canceled, or // redirect it to a cancel page if needed }else{ //insert your code if ok pressed // JOptionPane return an String, as you was talking about int int value = Integer.ParseInt(val); } 

这可能是你的答案:

 package Joptionpane; import javax.swing.JOptionPane; public class Cancle_on_JOptionPane { public static void main(String[] args) { String s; int i; for(i=0;i<100;i++){ s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?"); try{ if(s.equals("")) { JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=2; } else { JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=100; } } catch(Exception e) { JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE); i=100; } } } }