【问题标题】:How to handle cancel button in JOptionPane如何处理 JOptionPane 中的取消按钮
【发布时间】:2012-07-14 16:55:40
【问题描述】:

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

【问题讨论】:

标签: java swing jbutton joptionpane


【解决方案1】:

例如:

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 {

}

请参阅How to Make Dialogs 了解更多详情。

编辑:showInputDialog

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

}

【讨论】:

  • 投反对票,因为这不能回答问题 - 不知道为什么它是公认的答案。 Mazzy 专门询问 showInputDialog。
  • @Amber 有一个 2012 年的编辑显示如何使用showInputDialog,接近答案的结尾。
【解决方案2】:

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

    }

【讨论】:

  • 但是如果您的输入为空并且您点击了确定按钮,您的代码无法处理这种情况。你得到错误。
  • 这是正确答案。如果用户没有输入任何内容并按下确定,则响应将是空字符串。如果他们点击取消(即使他们输入了文本),响应将为空。
【解决方案3】:

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

【讨论】:

  • 我犯了一个错误...我的意思是showInputDialog。我有作为返回一个字符串对象
【解决方案4】:
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;
            }
        }
    }
}

【讨论】:

    【解决方案5】:

    你可以这样做:

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

    【讨论】:

      【解决方案6】:

      这可能是你的答案:

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

      【讨论】:

      • 虽然这个代码块可能会回答这个问题,但最好能对它这样做的原因提供一点解释。请edit您的回答包含这样的描述。
      猜你喜欢
      • 2014-01-21
      • 1970-01-01
      • 2013-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多