相关疑难解决方法(0)

JPasswordField KeyPress字符串长度错误?

我试图在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)

java swing netbeans string-length jpasswordfield

6
推荐指数
3
解决办法
1898
查看次数

如何检查JPassword字段是否为空

我想检查Swing中的用户名和密码.

该检查适用于用户名,但它不适用于JPaswordfield.我发布相关代码:

//Here I get the password
Char[] pwd = jt1.getPassword(); 
String s = new String(pwd);  //converting char to string
String p = s;

//In the JButton checking username and password(Username check works fine but not passwordfield)

jb.addActionListener(new ActionListener() {
                         public void actionPerformed(ActionEvent e)
                           {
                            if ((jt.getText().length()) == 0)
                            {
                                   JFrame jf1 = new JFrame("UserName");
                                         jf1.setSize(401, 401);
                                         //jf1.setVisible(true);
                                         jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
                                         JOptionPane.showMessageDialog(jf1, "User Name is empty");
                            }

                         else if((p.length()) == 0)
                            {

                                   JFrame jf1 = new JFrame("Password");
                                         jf1.setSize(401, 401);
                                         //jf1.setVisible(true);
                                         jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
                                         JOptionPane.showMessageDialog(jf1, …
Run Code Online (Sandbox Code Playgroud)

java swing jpasswordfield

2
推荐指数
1
解决办法
1万
查看次数

标签 统计

java ×2

jpasswordfield ×2

swing ×2

netbeans ×1

string-length ×1