简体   繁体   English

如何使用swing清空java中的密码字段值(String)

[英]how to empty the password field value(String) in java using swing

I have Textfield and password field.I am able to get the string written in the textfield and password field but not able to setting the text of password field empty. 我有Textfield和密码字段。我能够在textfield和password字段中获取字符串,但无法将密码字段的文本设置为空。

The error comes "The method setText(String) is undefined for type string" 错误来了“方法setText(String)未定义类型字符串”

JTextField jtf=new JTextField(8);
JPasswordField jpwf=new JPasswordField(8);
String value= jtf.getText();
String jpwf= jpwfName.getText();
jtfName.setText("");
**Error on this line**//jpwf.setText("");

You are not calling the setText() method on the correct variable. 您没有在正确的变量上调用setText()方法。 Use 采用

jpwf.setText("");

instead of 代替

pwd.setText("");

There is also another problem before that, you declare twice the variable jpwf (once with JPasswordField and once with String ). 之前还有另一个问题,你将变量jpwf声明两次(一次使用JPasswordField ,一次使用String )。 Maybe it's only a typo. 也许这只是一个错字。

The error you get says that you have a variable jpwf of the type String on which there is no method named setText() . 你得到的错误说你有一个String类型的变量jpwf ,其上没有名为setText()方法。

u can user this code 你可以使用这段代码

    jpwf.setText("");

in the line of u typed like 在你打字的行中

  jtfName.setText("");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM