2

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, "Password is empty");
                            }

                         else if ((p.length() == 0) && (jt.getText().length()) == 0)
                            {
                                   JFrame jf1 = new JFrame("UserName and Password");
                                         jf1.setSize(401, 401);
                                         //jf1.setVisible(true);
                                         jf1.setDefaultCloseOperation(jf1.EXIT_ON_CLOSE);
                                         JOptionPane.showMessageDialog(jf1, "Username and Password is 
empty");
                            }

                            else
                            { //go to another method}
Can someone help
4

1 に答える 1

11

非常に単純な方法で、テキストJPasswordField#getPassword()のaを返すを使用してテキストを取得char[]し、次に配列の長さを取得して、それが0に等しいかどうかを確認します。

JPasswordField jpf...

if(jpf.getPassword().length == 0) {
    // it is empty
}else {
   // it is not empty
}
于 2012-11-19T19:53:47.707 に答える