JPasswordField KeyPress 字符串长度错误? [英] JPasswordField KeyPress string length error?

查看:12
本文介绍了JPasswordField KeyPress 字符串长度错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改 Java Swing (Netbeans) 中 JPasswordField 的背景颜色.

I am trying to change background colors of a JPasswordField in Java Swing (Netbeans).

这是我所拥有的:

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

    //Get string from password box
    userPassword = new String(pstxtPassword.getPassword());

    //If password is 8+ characters
    //(one less because string counting begins at 0)
    if (userPassword.length() >= 7) {

        //Set password input box background color to green
        pstxtPassword.setBackground(Color.green);
    }

    else { //If password is less than 8 characters

        //Set password input box background color to red
        pstxtPassword.setBackground(Color.red);
    }

}

一切正常,除非我退格.当我在输入 8 个以上的字符后退格时,颜色不会变回红色,直到字段中只剩下 5 个字符.

Everything works, except when I backspace. When I backspace after typing 8+ characters, the color doesn't change back to red until there is only 5 characters left in the field.

将不胜感激,我对 Java 编程和 Netbeans 非常陌生.

Help would be appreciated, I am very new to java programming and Netbeans.

我已经更改了我的代码,

I have changed my code,

    //If password is 8+ characters
    if ((pstxtPassword.getPassword()).length >= 8) {

        //Set password input box background color to green
        pstxtPassword.setBackground(Color.green);
    }

    else { //If password is less than 8 characters

        //Set password input box background color to red
        pstxtPassword.setBackground(Color.red);
    }

这段代码对我来说似乎很有意义,但在测试中,第 9 个字符的颜色变为绿色;退格时,它在 6 处变回红色.这似乎与我在代码为 >= 7 时遇到的问题相同,其中颜色在第 8 个字符处变为绿色,但又变回红色5 个字符.

This code seems to make sense to me, but in testing, the color changes green at the 9th character; when backspacing, it changes back to red at 6. This seems to be the same problem I had when the code was >= 7 where the color changed green at the 8th character, but changed back to red at 5 characters.

输入9个字符后,组件变绿

After typing 9 characters, the component turns green

退格后(从9开始),组件保持绿色直到有6个字符

After backspacing (starting from 9), component remains green until there are 6 characters

这很奇怪,因为我在这个程序的一个按钮中有类似的代码,它显示了一条错误消息.该代码工作正常.这是 KeyPress 的问题,也许与退格键有关?

This is strange, because I have similar code in a button in this program which displays an error message. That code works fine. Is this a KeyPress problem, perhaps something to do with the backspace key?

推荐答案

顺便说一句,检查 getPassword() 返回的数组的长度,而不是 String 的长度 从该数组构造.String 存在安全风险,因为它将以容易找到的名称 userPassword 无限期存储.

As an aside, examine the length of the array returned by getPassword(), rather than the length of a String constructed from that array. The String is a security risk, as it will be stored for an indefinite time with the easily found name userPassword.

附录:这是 Robin 示例12968493/230513">建议 使用 DocumentListener.我猜您的关键侦听器在 JPasswordField 处理它之前看到了 KeyEvent.

Addendum: Here's a related example of Robin's suggestion to use DocumentListener. I'm guessing that your key listener is seeing the KeyEvent before the JPasswordField has processed it.

这篇关于JPasswordField KeyPress 字符串长度错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆