Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPasswordField按键字符串长度错误?_Java_Swing_Netbeans_String Length_Jpasswordfield - Fatal编程技术网

Java JPasswordField按键字符串长度错误?

Java JPasswordField按键字符串长度错误?,java,swing,netbeans,string-length,jpasswordfield,Java,Swing,Netbeans,String Length,Jpasswordfield,我试图在JavaSwing(Netbeans)中更改JPasswordField的背景色 以下是我所拥有的: private void pstxtPasswordKeyPressed(java.awt.event.KeyEvent evt) { //Get string from password box userPassword = new String(pstxtPassword.getPas

我试图在JavaSwing(Netbeans)中更改JPasswordField的背景色

以下是我所拥有的:

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个字符

如果您能提供帮助,我将不胜感激。我对java编程和Netbeans非常陌生

编辑: 我更改了密码

    //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个字符处变回红色

键入9个字符后,组件将变为绿色

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

这很奇怪,因为我在这个程序的一个按钮中有类似的代码,它显示了一条错误消息。这个代码很好用。 这是一个按键问题,可能与退格键有关

if (userPassword.length() >= 7)
此if语句与您的评论不匹配:

//如果密码为8个以上字符

实际代码显示如果有7个以上的字符,则将背景变成绿色。因此,当你退格时,当你减到剩下6个字符时,背景应该变成红色

我认为你的困惑可以从以下评论中看出:

//(one less because string counting begins at 0)

您试图描述的是,对
字符串中的字符进行索引从
0
开始,例如,当您使用
charAt()
子字符串()
时。这意味着第一个字符位于索引
0
,第二个字符位于索引
1
,等等。另一方面,
length()
返回
字符串中的字符数。这与索引无关,因此不需要减去1。

另外,检查
getPassword()
返回的数组的长度,而不是从该数组构造的
字符串的长度。
字符串
存在安全风险,因为它将使用易于找到的名称
userPassword
无限期存储


附录:下面是Robin的一个相关文档,可以使用
DocumentListener
。我猜您的密钥侦听器在
JPasswordField
处理它之前看到了
KeyEvent

因为
JPasswordField
扩展自
JTextComponent
,您可以在其上附加一个
DocumentListener
,这是一种更安全的方式,可以在每次内容更改时更新背景颜色。

我用按键释放而不是按键解决了这个问题,请试用我的朋友使用

private void pstxtPasswordKeyReleased(java.awt.event.KeyEvent evt) 
而不是

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

好的,因此
length()
返回字符串中的字符数。我最初有
if(userPassword.length()>=8
。但是,经过测试,当我输入8个字符时,背景仍然是红色的,当我输入第9个字符时,背景将变为绿色。我将其更改为
=7
,由于某种原因,它起了作用。我仍然无法理解为什么它会保持绿色,直到我退格到5个字符。不过,谢谢您的帮助。@Jaybob要调试这段代码,我首先要打印出
userPassword
的值,以确保它符合您的期望。@Jaybob:以下是Robin建议使用
DocumentListener
的相关内容。我猜您的密钥侦听器在
JPasswordField
处理它之前看到了
KeyEvent