我尝试过以下代码,它们都有不同的方法。
方法1:它在最新版本的Eclipse IDE中执行时会中断,比如2020-03,而在MarsIDE中运行良好。
这个问题已经在"如何在Java中显示输入星号?"中提出了?。
方法1:
Package test;
import java.io.*;
public class Test {
public static void main(final String[] args) {
String password = PasswordField.readPassword("Enter password:");
System.out.println("Password entered was:" + password);
}
}
class PasswordField {
public static String readPassword(String prompt) {
EraserThread et = new EraserThread(prompt);
Thread mask = new Thread(et);
mask.start();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String password = "";
try {
password = in.readLine();
} catch (IOException ioe) {
ioe.printStackTrace();
}
et.stopMasking();
return password;
}
}
class EraserThread implements Runnable {
private boolean stop;
public EraserThread(String prompt) {
System.out.print(prompt);
}
@SuppressWarnings("static-access")
public void run() {
while (!stop) {
System.out.print("