首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java和JPasswordField问题?

Java和JPasswordField问题?
EN

Stack Overflow用户
提问于 2013-07-26 17:42:52
回答 1查看 319关注 0票数 0

在我的代码中,我尝试设置Preferences。我有两个输入字段:JTextFieldJPasswordFieldJPasswordField工作正常,但是JTextField不会将偏好信息保存在内存中,相反,它会复制密码信息。

代码语言:javascript
复制
import java.util.prefs.Preferences;
import javax.swing.*;


public class TestJP {

    public static Preferences userPreferences = Preferences.userRoot();
    public final static String LOGIN_KEY = "";
    public final static String PASSWORD_KEY = "";


    public static void main(String[] args) {


              JTextField login = new JTextField(20);
              login.setText(userPreferences.get(LOGIN_KEY, ""));
              JPasswordField password = new JPasswordField(20);
              password.setText(userPreferences.get(PASSWORD_KEY, ""));

              JPanel myPanel = new JPanel();
              myPanel.add(new JLabel("login:"));
              myPanel.add(login);
              myPanel.add(Box.createHorizontalStrut(15)); 
              myPanel.add(new JLabel("password:"));
              myPanel.add(password);

              int result = JOptionPane.showConfirmDialog(null, myPanel, 
                       "Please Login", JOptionPane.OK_CANCEL_OPTION);
              if (result == JOptionPane.OK_OPTION) {

                 userPreferences.put(LOGIN_KEY,login.getText());
                 userPreferences.put(PASSWORD_KEY, password.getText());

              }

           }

    }

JPasswordField是否以某种方式覆盖JTextField

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-26 17:52:55

你们的钥匙都是空字符串。它们必须是唯一的字符串。

在此之前:

代码语言:javascript
复制
public final static String LOGIN_KEY = "";
public final static String PASSWORD_KEY = "";

新的:

代码语言:javascript
复制
public final static String LOGIN_KEY = "login_key";
public final static String PASSWORD_KEY = "password_key";
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17887694

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档