简体   繁体   English

如何在按键中验证 jTextfield

[英]How to validate jTextfield within keypress

im trying to validate a jtextfield from the keypress and would like it to throw an error message in a jLabel .我试图从keypress验证jtextfield并希望它在jLabel抛出错误消息。 I am trying to do this with in the code below.我试图在下面的代码中做到这一点。

private void jTextField2KeyPressed(java.awt.event.KeyEvent evt) {   


    }                                   

Can't comment, so i'll answer instead.无法评论,所以我会回答。 If you are looking to validate the input of the user as he/she type, you might wanna have a look at javax.swing.text.DocumentFilter .如果您希望在用户输入时验证他/她的输入,您可能需要查看javax.swing.text.DocumentFilter

If you are simply looking to update a JLabel when the user types something, you should add a KeyboardListener to the JTextField.如果您只是想在用户键入内容时更新 JLabel,您应该向 JTextField 添加一个 KeyboardListener。 You have to be a bit careful with that tho, as swing doesn't guarantee the order in which listeners are called, meaning that the text may not have updated when the KeyboardListener is invoked.您必须小心一点,因为 Swing 不能保证调用侦听器的顺序,这意味着在调用 KeyboardListener 时文本可能没有更新。

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

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