我是 JApplet 的新手。我不知道如何从方法 actionPerformed 中的内部类访问 JPasswordField 对象。我想将 PasswordField 添加到我的 JFrame BioReader,然后我想将 truePassword 与 JPasswordField 中的输入进行比较。我收到错误作为“密码无法解析为变量”。
import javax.swing.JFrame;
import javax.swing.JPasswordField;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class BioReader extends JFrame{
public BioReader(){
super("BioTech Inc.");
setLayout(new FlowLayout());
JPasswordField Password = new JPasswordField(10);
add(Password);
BioReader.theHandler eventHandler = new BioReader.theHandler();
Password.addActionListener(eventHandler);
}
private class theHandler implements ActionListener {
public void actionPerformed(ActionEvent event){
if(event.getSource()==Password) //error
String.format("You typed: %s",event.getActionCommand());
}//actionPerformed ended
}//class theHandler ended
}//class BioReader ended