简体   繁体   English

验证GUI中的文本字段输入

[英]Validate textfield input in GUI

I have two text fields and they must add up to 100 when submit is clicked, if I put in correct values into text field there's no error but if I leave the text field blank or put a letter in it the error handling isn't right. 我有两个文本字段,单击提交时它们必须加起来最多为100,如果我在文本字段中输入正确的值就没有错误,但是如果我将文本字段留空或在其中输入字母,则错误处理不正确。 Any ideas? 有任何想法吗?

submit.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

        if (validation == true) {
            int numA = Integer.parseInt(aTextField.getText());
            int numB = Integer.parseInt(aTextField.getText());
            int sum = numA + numB;

            if (sum == 100) {
                validation = true;
                System.out.println("success");
            } else {
                JOptionPane.showMessageDialog(createFrame, "A and B must add up to 100");
                validation = false;

            }


        }

this is the error 这是错误

int numA = Integer.parseInt(aTextField.getText());

Integer.parseInt(str); throws a NumberFormatException (the error you are seeing) if the String parsed into the method can not be converted into a number. 如果解析到方法中的String无法转换为数字,则抛出NumberFormatException (您正在看到的错误)。

You could use some exception handling in this particular situation. 在这种特定情况下,您可以使用一些异常处理。 I would suggest a try and catch block. 我建议一个trycatch块。 For example; 例如;

   try{
       if(validation == true){
            int numA = Integer.parseInt(aTextField.getText());
            int numB = Integer.parseInt(aTextField.getText());
            int sum = numA + numB;

            if(sum == 100){
                validation = true;
                System.out.println("success");
            } else{
                JOptionPane.showMessageDialog(createFrame, "A and B must add up to 100");
                validation = false;
            }
        }
    } catch (NumberFormatException n){
            JOptionPane.showMessageDialog(createFrame, "Only numbers should be entered into A and B");
            validation = false;
    }

If the exception is now thrown from within the try block, it will be 'caught' and then validation will be set to false in the catch block. 如果现在从try块中引发了异常,它将被“捕获”,然后在catch块中将验证设置为false You could also use the catch block to display a message saying that only numbers should be entered into the fields. 您还可以使用catch块显示一条消息,指出仅数字应输入到字段中。

Another way to achieve this using the Swing API is if you don't want them to ever put texts into those fields you could use a JFormattedTextField to allow them to only enter numbers. 另一种方式来实现这一目标使用Swing的API是,如果你不希望他们没有把文本到这些领域,你可以使用JFormattedTextField ,让他们只输入数字。

I hope this helps. 我希望这有帮助。 Let me know how you get on. 让我知道你是怎么办的。

Check the javadoc of Integer.parseInt ! 检查Integer.parseIntjavadoc The function cannot handle an empty string. 该函数无法处理空字符串。 What number should that be anyways? 反正应该是多少?

Possible solutions: 可能的解决方案:

  • set the default text of your textFields to "0" 将您的textFields的默认文本设置为“ 0”

  • add exception handling for the parseInt calls (you could fall back to 0 or prompt the user to correct) 为parseInt调用添加异常处理(您可以退回到0或提示用户进行更正)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM