【问题标题】:JOptionPane cancel buttonJOptionPane 取消按钮
【发布时间】:2013-06-03 11:33:49
【问题描述】:

伙计们,我正在尝试使用 JOptionPane,但取消按钮正在响应,就好像我输入了错误的输入值并且没有退出程序一样。任何非常有用的想法!

int n = 0, k = 0;

Students stu = new Students();      

while (n <= 0) { 

     try { 
       n = Integer.parseInt(JOptionPane.showInputDialog(stu, "Enter the number of people","Input", JOptionPane.INFORMATION_MESSAGE));

       if (n <= 0) {
        OptionPane.showMessageDialog(stu, "You have given a wrong input!", 
             "Warning", JOptionPane.WARNING_MESSAGE);
       }    
    }

    catch (Exception e) { 
          JOptionPane.showMessageDialog(stu, "You have given a wrong input!",
               "Warning",  JOptionPane.WARNING_MESSAGE);
          n = 0; 
    }
}

【问题讨论】:

  • 考虑发布完整的工作代码......
  • JOptionPane 将其作为 API 中实现的常量返回
  • 程序退出的条件是什么?
  • 当用户按下取消按钮退出时,我试图告诉程序
  • 看看下面的答案,由 A-SM 提供。如果用户按下取消按钮,输入对话框将返回 null,无论他们输入了什么。

标签: java swing joptionpane cancel-button


【解决方案1】:

这是你想要的吗:

int n;
String code = JOptionPane.showInputDialog(null, 
"Enter the size of the group", 
"Team Combination Finder", 
JOptionPane.INFORMATION_MESSAGE);

if (code == null) {
  System.out.println("This is cancel button");
  System.exit(0);
} else if (code.equalsIgnoreCase("")) {
  System.out.println("This is OK button without input");
} else {
  try {
    n = Integer.parseInt(code);
    if (n <= 0) {
      System.out.println("This is wrong input");
    } else {
      System.out.println("This is right input");
    }
  } catch (Exception e) {
    System.out.println("You must input numeric only");
  }
}

看看它是否适合你:)

【讨论】:

  • 这是最好的答案。如果用户按下取消按钮,无论在文本框中输入了什么,JOptionPane.showInputDialog 都会返回 null。
【解决方案2】:

作为旁注,我认为拼写错误 JOptionPane(第 11 行)应该会引发不必要的异常。

【讨论】:

  • 代码中没问题,我只是在转移到这里时错过了它
【解决方案3】:

显示 OptionDialog 后,您必须使用 break 语句来中断循环。 JOptionPane 不知道您的循环。

或者使用

System.exit(ErrorCode);

而不是break;

更新

所以我想你想要的是这样的:

String input = JOptionPane.showInputDialog(null, "Enter name :  ", "New Record!", 

while (n <= 0) { 
 try {
   String input = JOptionPane.showInputDialog(stu, "Enter the number of people","Input", JOptionPane.INFORMATION_MESSAGE);
   if(input == null || input.length() == 0)
   {
    OptionPane.showMessageDialog(stu, "You have given a wrong input!", 
         "Warning", JOptionPane.WARNING_MESSAGE);
      System.exit(0);
   }

   n = Integer.parseInt(input);

   if (n <= 0) {
    OptionPane.showMessageDialog(stu, "You have given a wrong input!", 
         "Warning", JOptionPane.WARNING_MESSAGE);
   }    
}

catch (Exception e) { 
      JOptionPane.showMessageDialog(stu, "You have given a wrong input!",
           "Warning",  JOptionPane.WARNING_MESSAGE);
      n = 0; 
}

}

【讨论】:

  • 我试过了,但没有任何反应! if (stu == null) { JOptionPane.showMessageDialog(comb, "你已经退出程序", "Exiting..", JOptionPane.PLAIN_MESSAGE); System.exit(0); }
  • System.exit(ErrorCode) 将退出进程。想给你一个否定但做不到。
  • 我只是在我的代码中添加一些东西以使它看起来更好。我唯一不能做的就是用取消按钮退出
  • @Mr.777,你认为“退出程序”是什么意思?
  • @user2441143,那么您是否要编写一个要求用户退出的对话框?你要求退出程序,可以通过System.exit(..)来实现
猜你喜欢
  • 2013-05-02
  • 1970-01-01
  • 1970-01-01
  • 2012-07-14
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-17
相关资源
最近更新 更多