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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:13:31  来源:igfitidea点击:

JPasswordField how to set password

javaswingpasswordsjpasswordfield

提问by alexj

Hello I am using a JPasswordFieldwhen I want to read it it is no problem with getPasswordbut what I am doing is when the Password is not set it shows a InputDialogwhere you can type in the password and then it should set the the Password in to the JPasswordFieldbut when I use setTextit 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 setTextis not workingfor a JPasswordFieldis 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 JPasswordFieldin your UI. When you add/remove components from a Containeryou need to invalidate the layout, as documented in the Container#addand Container#removemethods.

让我想知道您是否JPasswordField在 UI 中看到了您的新内容。当您从 a 添加/删除组件时,Container您需要使布局无效,如Container#addContainer#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 :) 谢谢你的帮助。