【问题标题】:JTextfield Listener for Integers整数的 JTextfield 侦听器
【发布时间】:2013-03-23 02:01:15
【问题描述】:

我很难找到为JTextField 编写侦听器的解决方案,专门只允许整数值(不允许Strings)。我在 Document Listener 上尝试过 this recommended link,但我不知道调用什么方法等。

我以前从未使用过这种类型的侦听器,所以谁能解释我如何在 JTextField 上编写侦听器以只允许可以接受的整数值?

基本上在我单击JButton 之后,并且在将数据提取到变量上之前,Listener 将不允许处理它,直到输入一个整数。

非常感谢。

【问题讨论】:

    标签: java swing jtextfield documentlistener


    【解决方案1】:

    您不需要监听器,您想从JTextField 获取文本并测试它是否为int

    if (!input.getText().trim().equals(""))
    {
        try 
        {
            Integer.parseInt(myString);
            System.out.println("An integer"):
        }
        catch (NumberFormatException) 
        {
            // Not an integer, print to console:
            System.out.println("This is not an integer, please only input an integer.");
            // If you want a pop-up instead:
            JOptionPane.showMessageDialog(frame, "Invalid input. Enter an integer.", "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    

    您也可以使用正则表达式(有点矫枉过正,但它有效):

    boolean isInteger = Pattern.matches("^\d*$", myString);
    

    【讨论】:

    • @DouglasGrealis 你会在你的catch 中加入类似的东西。我将更新我的示例。
    • 好的,谢谢。我可以在 catch 子句中使用一个对话框。再次感谢
    【解决方案2】:

    您不需要文档侦听器。您希望在提交/确定按钮上有一个 ActionListener。

    确保使用 JTextField 的句柄创建侦听器,然后将此代码放入 actionPerformed 调用中:

    int numberInField;
    try {
      numberInField = Integer.parseInt(myTextField.getText());
    } catch (NumberFormatException ex) {
      //maybe display an error message;
      JOptionPane.showMessageDialog(null, "Bad Input", "Field 'whatever' requires an integer value", JOptionPane.ERROR_MESSAGE);
      return;
    }
    // you have a proper integer, insert code for what you want to do with it here
    

    【讨论】:

    • 好的,谢谢。我实际上一直在使用 parseInt。但是有没有一种方法,如果你尝试输入一个字符串,那么会弹出一个提示,并说“只需要整数(JTextField 不允许输入)?还是用java写太复杂了?再次感谢
    • 你可以在上面的catch子句中弹出错误信息,或者在你的对话框中放置一个JLabel来显示错误信息。哪个看起来更好取决于您那里还有什么,但一般来说弹出窗口具有破坏性。 [编辑以包含错误对话框]
    • 好的,感谢您的帮助。对话框是最好的解决方案。再次感谢
    • 您想使用 JFormattedTextField 或 DocumentFilter 以便只能输入有效数字。然后你甚至不需要进行编辑,除非你有某个国王的范围条件。对于不使用 JDialog 的错误消息,可以使用 JOptionPane。
    • @camickr 我认为你把这个评论放错了地方。我已经在我的回答中使用了 JOptionPane 来制作错误对话框。
    【解决方案3】:

    如何在 JTextField 上编写一个侦听器以只允许可以接受的整数值?

    您应该使用JFormattedTextFieldDocument Filter

    【讨论】:

      【解决方案4】:

      JFormattedTextField 示例:

      public static void main(String[] args) {
          NumberFormat format = NumberFormat.getInstance();
          format.setGroupingUsed(false);
          NumberFormatter formatter = new NumberFormatter(format);
          formatter.setValueClass(Integer.class);
          formatter.setMinimum(0);
          formatter.setMaximum(Integer.MAX_VALUE);
          JFormattedTextField field = new JFormattedTextField(formatter);
          JOptionPane.showMessageDialog(null, field);
      }
      

      JFormattedTextField 非常适合限制输入。除了将输入限制为数字之外,它还具有更高级的用途,例如电话号码格式。这提供了即时验证,无需等待表单提交或类似事件。

      【讨论】:

        猜你喜欢
        • 2021-05-23
        • 2011-04-26
        • 2015-05-10
        • 1970-01-01
        • 2014-03-09
        • 2014-03-21
        • 2011-11-27
        相关资源
        最近更新 更多