我无法从我的JPasswordField获取输入。我得到它,因为它将作为输出显示在JOptionPane中(仅用于练习目的)。无法从PasswordField获取文本,甚至转换为字符串
Eclipse不给我任何错误,但程序不能正常工作
好了,所以这里的程序流程。如果JPasswordField等于“admin”。然后会出现一个对话框并显示我的密码(即“admin”)。而已。
我已经将我的JPasswordField转换为字符串。 但仍然。没有要显示的字符串。 Eclipse中也不会显示任何错误。
代码:
public class GUI2 extends JFrame {
private JButton shut;
private JPasswordField field;
public GUI2(){
super("Display The Password");
setLayout(new FlowLayout());
field = new JPasswordField(10);
add(field);
display = new JButton("Display");
add(display);
thehandler handler = new thehandler();
display.addActionListener(handler);
}
class thehandler implements ActionListener{
String password = new String(field.getPassword());
public void actionPerformed(ActionEvent event){
if (password.equals("admin")){
JOptionPane.showMessageDialog(null, password);
}
}
}
}
在''actionPerformed''方法内移动''String password = new String(field.getPassword());''。 – f1sh