Java编译错误:找不到符号(EraserThread)

时间:2013-11-21 18:10:29

标签: java compilation symbols

Java新手,使用模板工作。知道什么是错的吗?我正在使用Netbeans IDE并将java文件上传到linux机器,我正在那里编译。

PasswordField.java:42: cannot find symbol
symbol  : class EraserThread
location: class PasswordField
  EraserThread et = new EraserThread(prompt);
  ^
PasswordField.java:42: cannot find symbol
symbol  : class EraserThread
location: class PasswordField
  EraserThread et = new EraserThread(prompt);
                        ^
2 errors

public 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();
  }
  // stop masking
  et.stopMasking();
  // return the password entered by the user
  return password;
} 
}

1 个答案:

答案 0 :(得分:0)

您需要先创建(或复制)EraserThread类。这是代码。您将要转到项目资源管理器,并在创建上述类的同一个程序包中创建一个名为“EraserThread”的新类。然后将其粘贴到类中:

import java.awt.event.KeyListener;
import java.io.*;

public class EraserThread implements Runnable {
    private boolean stop;

    /**
    *@param The prompt displayed to the user
    */
    public EraserThread(String prompt) {
        System.out.print(prompt);
    }

    /**
    * Begin masking...display asterisks (*)
    */
    public void run () {
        stop = true;
        while (stop) {
            System.out.print("/010*");
            try {
                Thread.currentThread().sleep(1);
            } catch(InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    }

    /**
    * Instruct the thread to stop masking
    */
    public void stopMasking() {
        this.stop = false;
    }
}

创建类后,您应该能够运行代码。

代码来源:http://code.google.com/p/testsome/source/browse/trunk/test/src/EraserThread.java?r=3

修改 我不确定这个类是否已存在于其他地方。您可以在不创建它的情况下导入它,但我不确定。只需自己创建这个类,你应该没问题,而不必通过代码导入它。