当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Replace string and encode it again

问题描述:

I'm pretty new to Java & just made a login, but I want to hide the password with stars. But if I use this snippet it will use the stars. How can I 'encode' it after replacing?

EDIT: The input is from a GuiTextBox, its a method of GuiScreen of Minecraft.

String input = passwd.getText();

char[] ca = new char[input.length()];

Arrays.fill(ca, '*');

String output = new String(ca);

passwd.setText(output);

网友答案:

You are trying to recreate something already existing.

Java provide the JPasswordField that will do exactly what you want directly.

This is used easily like a JTextField but hidding the password on keystroke. And you have this solution to get the value (can't remember if getText() return the value too)

char[] input = passwordField.getPassword();

See this guide for more information


EDIT

Since this is not a Swing component and I don't know it, I will give you an ugly solution ;)

You need to keep the input variable in memory. So instead of declaring input in the methods where you replace the value, declare it outside the methods (to be an instance variable)

分享给朋友:
您可能感兴趣的文章:
随机阅读: