java JPasswordField 如何设置密码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13306689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JPasswordField how to set password
提问by alexj
Hello I am using a JPasswordField
when I want to read it it is no problem with getPassword
but what I am doing is when the Password is not set it shows a InputDialog
where you can type in the password and then it should set the the Password in to the JPasswordField
but when I use setText
it does not set it and there is not method setPassword()
. So my question is how can i set a password to a JPasswordField
?
你好我使用的是JPasswordField
当我想读它,它是没有问题的getPassword
,但我做的是当没有设置口令也显示了InputDialog
在那里你可以键入密码,然后将它应该设置密码到JPasswordField
,但当我使用setText
它时没有设置它并且没有方法setPassword()
。所以我的问题是如何将密码设置为 a JPasswordField
?
String password = "";
JPasswordField passwordField = new JPasswordField();
passwordField.setEchoChar('*');
Object[] obj = {"Bitte ihr PAsswort eingeben:\n\n", passwordField};
Object stringArray[] = {"OK","Cancel"};
if (JOptionPane.showOptionDialog(null, obj, "Passwort", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, stringArray, obj) == JOptionPane.WARNING_MESSAGE)
{
password = new String(passwordField.getPassword());
}
txtFtpUser.setText(username);
panel_1.remove(txtFtpPassword);
txtFtpPassword = new JPasswordField(password);
txtFtpPassword.setBounds(10, 113, 206, 23);
panel_1.add(txtFtpPassword);
回答by Robin
You claim that setText
is not workingfor a JPasswordField
is incorrect. See the following piece of code which just works as expected:
你声称setText
是不工作的JPasswordField
不正确。请参阅以下代码,它按预期工作:
public static void main( String[] args ) {
EventQueue.invokeLater( new Runnable() {
@Override
public void run() {
JFrame testFrame = new JFrame( "Test" );
JPasswordField field = new JPasswordField( );
field.setColumns( 20 );
field.setText( "Password" );
testFrame.add( field );
testFrame.pack();
testFrame.setVisible( true );
testFrame.setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );
}
} );
}
The variant where you pass the text in the constructor (as you did in your code) also works as expected.
您在构造函数中传递文本的变体(就像您在代码中所做的那样)也按预期工作。
So I would search in another direction. The following part
所以我会寻找另一个方向。以下部分
txtFtpUser.setText(username);
panel_1.remove(txtFtpPassword);
txtFtpPassword = new JPasswordField(password);
txtFtpPassword.setBounds(10, 113, 206, 23);
panel_1.add(txtFtpPassword);
makes me wonder whether you see your new JPasswordField
in your UI. When you add/remove components from a Container
you need to invalidate the layout, as documented in the Container#add
and Container#remove
methods.
让我想知道您是否JPasswordField
在 UI 中看到了您的新内容。当您从 a 添加/删除组件时,Container
您需要使布局无效,如Container#add
和Container#remove
方法中所述。
Note: be aware of the security issues when passing the password around as a String
. But according to your comments you are already aware of this.
注意:将密码作为String
. 但根据您的评论,您已经意识到这一点。
回答by alexj
I found the Error here it was:
我在这里发现错误是:
if (JOptionPane.showOptionDialog(null, obj, "Passwort", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, stringArray, obj) == JOptionPane.WARNING_MESSAGE)
{
password = new String(passwordField.getPassword());
}
It checked it was a Warning Message but it was a YES_OPTION :) Thank you for all your help.
它检查了它是一个警告消息,但它是一个 YES_OPTION :) 谢谢你的帮助。