我试图在Java Swing(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);
}
}
Run Code Online (Sandbox Code Playgroud)
一切正常,除非我退格.键入8个以上字符后退格时,颜色不会变回红色,直到字段中只剩下5个字符.
帮助将不胜感激,我是java编程和Netbeans的新手.
编辑:我改变了我的代码,
//If password is 8+ characters
if ((pstxtPassword.getPassword()).length >= 8) {
//Set password …Run Code Online (Sandbox Code Playgroud)