Displaying the password in a JPasswordField rather than setting 0 as an echo char

ぐ巨炮叔叔 提交于 2019-12-01 22:01:51

问题


How do I display the text of a JPasswordField rather than set 0 as an echo char?

Java Docs says:

Setting a value of 0 indicates that you wish to see the text as it is typed, similar to the behavior of a standard JTextField.

The following results in an Incompatible types error

outField.setEchoChar(0);

wheras

outField.setEchoChar('0');

simply sets 0 as the echo char (displays 0s for every digit).

The reason for using JPasswordField over JTextField is that I want to change the echo char and hide the password with another method.

Thanks!


回答1:


Do this:

outField.setEchoChar((char)0);

If you do this:

outField.setEchoChar(0);

then 0 is an integer, not a char, and the method requires a char.

If you do this:

outField.setEchoChar('0');

then '0' is equivalent to (char)48.




回答2:


When you do this:

outField.setEchoChar(0);

you're trying to pass an int into the method, and this isn't allowed since it expects a char type. Instead try

outField.setEchoChar((char)0);



回答3:


Use the char with value 0, not the number 0, or the char '0':

    outField.setEchoChar((char)0);



回答4:


Try it out like this......

outField.setEchoChar((char)0);



回答5:


try outField.setEchoChar('\0');



来源:https://stackoverflow.com/questions/11637424/displaying-the-password-in-a-jpasswordfield-rather-than-setting-0-as-an-echo-cha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!