在JFrame类中,我有一个JPasswordField对象,如下所示:
pswdTextField = new JPasswordField(20); externalPanel.add(pswdTextField, "w 90%, wrap");
我尝试通过以下代码行访问其插入的内容(用户插入的密码):
char[] pswd = pswdTextField.getPassword(); System.out.println("Password: " + pswd.toString());
问题是,当我要打印此内容时,将获得以下输出:密码:[C @ d5c0f9,而不是插入的密码
为什么?什么意思 如何获得插入的密码?
特纳克斯
安德里亚
为什么?什么意思
如果您浏览文档比得到的原因多。
为了提高安全性,建议在使用后通过将每个字符设置为零来清除返回的字符数组。
如何获得插入的密码?
您可以通过以下方式获取密码:
char[] pswd = pswdTextField.getPassword(); String password=new String(pswd);
或者,您可以直接在 System.out.print
System.out.print(pswd); // It override ...print(char[]) method // without concat with another String.
编辑
请注意,如果char[]
与之连接String
,它将继承Object.toString()
方法。
System.out.print("Password: " +pswd);// It will print like Password: [C@d5c0f9