简体   繁体   English

如何在Java中验证JTextField上的输入

[英]How to validate inputs on JTextField in Java

everyone, I am writing an interactive graphical user interface program. 大家,我正在编写一个交互式图形用户界面程序。 basically it accepts all inputs and display information on TextArea below. 基本上,它接受所有输入并在下面的TextArea上显示信息。 but it will validate all inputs if it is empty or null. 但是它将验证所有输入是否为空或为空。 My question is that, for example, first name is empty, how can it loop back at textField (first name field)? 我的问题是,例如,名字为空,它如何循环回到textField(名字字段)? and retype again without further going ? 并再次输入而无需进一步操作?

I was using a do while loop, but it only works when I use JOptionPane windows to pop out. 我使用了do while循环,但是仅当我使用JOptionPane窗口弹出时才起作用。 let me clarify my question: What I want is that when the first name field is null or empty, the user needs to retype again from textfield instead of a window, I don't want "JOPtionPane.showmessageDialog " window coming out Thanks 让我澄清一下我的问题:我想要的是,当名字字段为null或空时,用户需要从文本字段而不是窗口中再次输入,我不希望出现“ JOPtionPane.showmessageDialog”窗口。

image is here 图片在这里

graphic interface 图形界面

do while loop 做while循环

do{
        whetherContinue=true;

        FName=TxtFname.getText();
        if(TxtFname.getText().isEmpty()){
            FName = JOptionPane.showInputDialog("Please type your First Name");
            TxtFname.setText(FName);
        }
        else{

        }
        if (FName==null)
            System.exit(1);
    try{
        if (FName.isEmpty())
            throw new ArithmeticException("First name can not be empty!");
        whetherContinue = false;
    }catch (Exception ex) 
    {
        JOptionPane.showMessageDialog(null,"The first name can not be empty! Please type again");
    }
    }while(whetherContinue); //First Name while

try the focus listner for the empty input problem when the focus is lost from your component check it's text i the text is empty than go back to the same component by requestFocus method 当焦点从组件中丢失时,尝试使用焦点列表器来解决空输入问题。检查文本是否为空,然后通过requestFocus方法返回相同的组件

 FocusListener focus_event = new FocusListener() {

            @Override
            public void focusLost(FocusEvent arg0) {
                if(textField.getText().isEmpty())
                {
                    JOptionPane.showConfirmDialog(null, "Please enter","erruer",JOptionPane.ERROR_MESSAGE);
                    textField.requestFocus();
                }
            }

            @Override
            public void focusGained(FocusEvent arg0) {
                // TODO Auto-generated method stub

            }
        };
        textField.addFocusListener(focus_event);

Try this method, it prevents the user from entering non-numerical characters as they type. 尝试使用此方法,它可以防止用户在键入时输入非数字字符。 You capture user's input from keyboard, like this if you are taking numbers: 您可以从键盘捕获用户的输入,如果您要输入数字,则如下所示:

JTextField textField = new JTextField(10);
textField.addKeyListener(new KeyAdapter() {
   public void keyTyped(KeyEvent e) {
      char c = e.getKeyChar();
      if ( ((c < '0') || (c > '9')) && (c != KeyEvent.VK_BACK_SPACE)) {
         e.consume();  // ignore event
      }
   }
});

Based on this approach, you can do for characters that you want. 基于这种方法,您可以对所需字符进行处理。

OR change your JTextField with a JFormattedTextField, like this: 或使用JFormattedTextField更改您的JTextField,如下所示:

try {
   MaskFormatter mascara = new MaskFormatter("##.##");
   JFormattedTextField textField = new JFormattedTextField(mascara);
   textField.setValue(new Float("12.34"));
} catch (Exception e) {
   ...
}

Here is a reference with other approaches as well. 这也是其他方法的参考

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

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