简体   繁体   English

setInputVerifier(InputVerifier inputVerifier)如何工作?

[英]How does setInputVerifier(InputVerifier inputVerifier) work?

I've got code: 我有代码:

public class SerialNumberInputVerifier extends InputVerifier {
@Override
public boolean verify(JComponent input) {
    String text = ((JTextField) input).getText();
    if(text.length()!=6)
        return false;
    try {
        Integer value = Integer.parseInt(text);
        return true;
    } catch (NumberFormatException e) {
        return false;
    }
}

It seems to work fine, do all the checkings. 看来一切正常,请进行所有检查。 But I'm not sure about setInputVerifier(InputVerifier inputVerifier). 但是我不确定setInputVerifier(InputVerifier inputVerifier)。 It just doesn't allow me to input to another field, but I simply can push a button and a wrong data have been sended. 它只是不允许我输入其他字段,而我只需要按一下按钮,就可以发送错误的数据。 So how could I possibly handle that? 那我怎么可能处理呢?

Thanks. 谢谢。

Take a look at Validating Input from How to Use the Focus Subsystem . 看一下如何使用Focus子系统 验证输入

Basically, what it is allowing you to do is determine if focus should be taken away from the field or not depending on whether you think the input is valid or not 基本上,允许您执行的操作是确定输入是否应从该字段移开,具体取决于您认为输入是否有效

Try overriding shouldYieldFocus to stop the field from losing focus when it's invalid 尝试覆盖shouldYieldFocus以阻止该字段在无效时失去焦点

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

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