如何在JOptionPane中处理取消按钮

14

我创建了一个类型为showInputDialogJOptionPane。当它打开时,会显示两个按钮:OKCancel。我想在按下Cancel按钮时处理动作,但我不知道如何处理。怎样才能获取它?


https://dev59.com/OmTWa4cB1Zd3GeqPG8sp#10966330 - assylias
7个回答

26
例如:
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)) {

}

1
因为这个答案并没有回答问题,我把它踩了 - 不确定为什么会被接受作为答案。Mazzy 的问题是关于 showInputDialog 的。 - Amber
@Amber,2012年的编辑显示了如何在答案末尾使用showInputDialog - tenorsax

9

这是一个老问题,我是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)

    }

1
但是你的代码没有处理一种情况,如果你的输入为空并且你点击了OK按钮。你会得到一个错误。 - frank17
这是正确的答案。如果用户没有输入任何内容并按下确定,响应将是空字符串。如果他们点击取消(即使已经输入文本),响应将为null。 - Amber

6

showMessageDialog不应该显示两个按钮,因此你的代码或解释可能有问题。无论如何,如果你想要给用户一个选择并想要检测这个选择,不要使用showMessageDialog,而是使用showConfirmDialog,并获取返回的int值,然后测试它是否为JOptionPane.OK_OPTION。


1
我犯了一个错误...我的意思是showInputDialog。我的返回值是一个字符串对象。 - Mazzy

0
你可以这样做:
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);
}

0

showInputDialog 如果点击取消则返回 NULL,即使字符串为空也会返回 String 对象。 所以你只需要测试它是否为 Null,以及它是否不为空。


-1
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;
            }
        }
    }
}

-2

这可能是你的答案:

package Joptionpane;

import javax.swing.JOptionPane;
    
public class Cancle_on_JOptionPane {

    public static void main(String[] args) {
        String s;
        int i;
        // Set the process limit to any number, that use when you need to use for comparability and limited to get out of the program when the answer is correct.(in this case is 100)
        for(i=0;i<100;i++){
        s = JOptionPane.showInputDialog(null,"What is your favorite fruit ?");
                        // Resolve process by compare answer with character to got the right answer, If it's no character it would show question box until user type answer or click cancel.
                        try{      
                                 if(s.equals(""))  {
                                                            JOptionPane.showMessageDialog(null," Enter your answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
                                                            i=2; // Set i=2 to repeating question box loop until user give answer.
                                                       }
                                 else  {
                                           JOptionPane.showMessageDialog(null," s = "+s," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
                                           i=100; // Get out from question box when the variable is not empty.
                                         }
                            }
        catch(Exception e)
              {
                 JOptionPane.showMessageDialog(null,"Cancle answer !!!"," ^-^ Information^-^ ",JOptionPane.INFORMATION_MESSAGE);
                 i=100; // Get out from question box when user click cancel and showing "cancel message", You can link to another action here.
              }
     }
   }
}

虽然这个代码块可能回答了问题,但最好能够提供一些解释为什么会这样。请编辑您的答案以包括这样的描述。 - Artjom B.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接