Java-在JPasswordField中隐藏字符串
我正在检查密码输入。密码已设置,为“123”。一切正常,但当我将密码写入输入框时,我可以看到我写的内容。我想像****一样隐藏密码。我怎样才能解决这个问题Java-在JPasswordField中隐藏字符串,java,user-interface,Java,User Interface,我正在检查密码输入。密码已设置,为“123”。一切正常,但当我将密码写入输入框时,我可以看到我写的内容。我想像****一样隐藏密码。我怎样才能解决这个问题 public void actionPerformed(ActionEvent e) { String password= "123"; JPasswordField pf = new JPasswordField(); String pw = JOptionPane.showInputDialog(null,"B
public void actionPerformed(ActionEvent e) {
String password= "123";
JPasswordField pf = new JPasswordField();
String pw = JOptionPane.showInputDialog(null,"Bitte geben Sie das Passwort ein");
if (pw.equals((password))) {
JOptionPane.showMessageDialog(null,"correct!");
} else
JOptionPane.showMessageDialog(null,"False!");
}
密码应始终通过哈希加密,以获得最佳安全性。将要存储的是散列。当用户提供密码时,应使用相同的salt自动对其进行散列,并与相同的salt存储散列进行比较,以确保其有效性。除了用户之外,任何人都不应该知道真正的密码可能是什么 看起来您要做的是创建自己的定制JOptionPane输入框,JOptionPane的设计目的也是为了实现这一点。您可能希望创建一个JPanel,添加希望显示的组件和一些布局信息,然后应用JPanel以显示在一个确认对话框中。以下是您可能希望执行此操作的方式:
private void customLoginInputBox() {
// Create a dummy JFrame. This is done so that
// the Dialog boxes that are used in the application
// do not hide behind the IDE. Not a problem for
// some. If you can't use 'this' as the component
// source for the JOptionPane then use the two code
// lines below (otherwise remove them).
final JFrame iFRAME = new JFrame();
iFRAME.setAlwaysOnTop(true);
String uName;
String passWrd = "";
// Get User input as to User Name and Password.
// using a custom Input Dialog...
// Build the Login dialog Panel...
BorderLayout layout = new BorderLayout();
JPanel panel = new JPanel(layout);
JLabel label = new JLabel("<html>Please supply your Login information. If "
+ "you are<br>a New User then you will be automatically added<br>"
+ "to the database.<br><br><br></html>");
panel.add(label, BorderLayout.NORTH);
JPanel p = new JPanel(new BorderLayout(5, 5));
JPanel labels = new JPanel(new GridLayout(0, 1, 2, 2));
labels.add(new JLabel("User Name:", SwingConstants.RIGHT));
labels.add(new JLabel("Password:", SwingConstants.RIGHT));
p.add(labels, BorderLayout.WEST);
JPanel controls = new JPanel(new GridLayout(0, 1, 2, 2));
JTextField userName = new JTextField();
controls.add(userName);
JPasswordField password = new JPasswordField();
controls.add(password);
p.add(controls, BorderLayout.CENTER);
panel.add(p);
JLabel baseLabel = new JLabel("<html><pre><font size=2> "
+ "Select <font color=red>Cancel</font> to quit</font></pre><br></html>");
panel.add(baseLabel, BorderLayout.SOUTH);
// Get Input from User. This will display
// your custom Input Dialog Box.
int res = JOptionPane.showConfirmDialog(this, panel, "User Login ...",
JOptionPane.OK_CANCEL_OPTION);
// Process the result from our custom Input Dialog Box
if (res == JOptionPane.OK_OPTION && !userName.getText().equals("")
&& !Arrays.toString(password.getPassword()).equals("")) {
uName = userName.getText();
char[] pass = password.getPassword();
for (int i = 0; i < pass.length; i++) {
passWrd += Character.toString(pass[i]);
}
// Do whatever you want with User Name and Password
System.out.println("User Name Supplied:\t" + uName);
System.out.println("Password Supplied :\t" + passWrd);
}
else {
// Do whatever you want when nothing is supplied
// or User selectes the Cancel button or simply
// closes the Input Box Dialog.
System.out.println("Input Canceled!");
}
}
private void customLoginInputBox(){
//创建一个虚拟JFrame
//应用程序中使用的对话框
//不要躲在IDE后面,这对你来说不是问题
//有些。如果不能将“this”用作组件
//JOptionPane的源代码,然后使用这两个代码
//下面的行(否则将其删除)。
最终JFrame iFRAME=新JFrame();
iFRAME.setAlwaysOnTop(真);
字符串uName;
字符串passWrd=“”;
//获取有关用户名和密码的用户输入。
//使用自定义输入对话框。。。
//生成登录对话框面板。。。
BorderLayout=新的BorderLayout();
JPanel面板=新JPanel(布局);
JLabel label=新的JLabel(“请提供您的登录信息。如果”
+“如果您是新用户,则系统将自动添加您
”
+“到数据库。
”;
面板。添加(标签,边框布局。北);
JPanel p=新JPanel(新边界布局(5,5));
JPanel标签=新的JPanel(新的GridLayout(0,1,2,2));
添加(新的JLabel(“用户名:”,SwingConstants.RIGHT));
添加(新的JLabel(“密码:”,SwingConstants.RIGHT));
p、 添加(标签,边框布局。西);
JPanel控件=新的JPanel(新的GridLayout(0,1,2,2));
JTextField userName=新的JTextField();
控件。添加(用户名);
JPasswordField password=新的JPasswordField();
控件。添加(密码);
p、 添加(控件、BorderLayout.CENTER);
小组委员会.添加(p);
JLabel baseLabel=新的JLabel(“”)
+“选择“取消”退出
”;
面板。添加(baseLabel,BorderLayout.SOUTH);
//从用户处获取输入。这将显示
//自定义输入对话框。
int res=JOptionPane.showConfirmDialog(此面板为“用户登录…”),
JOptionPane.OK\u CANCEL\u选项);
//处理自定义输入对话框的结果
如果(res==JOptionPane.OK_选项&&!userName.getText().equals(“”)
&&!Arrays.toString(password.getPassword()).equals(“”){
uName=userName.getText();
char[]pass=password.getPassword();
for(int i=0;i
为什么您要打开一个新的输入对话框而不是实际使用JPasswordField
?我试图使用它,但它不起作用…@UnholySheep“它不起作用”不是问题描述。尽管您似乎试图使用文本字段作为弹出窗口(这显然不起作用)。但问题是,我看到的所有教程都没有一个已经设置了密码的示例。我设置了一个像“123”这样的密码,以及如何隐藏这个没有一个已经设置了密码的示例-是的,设置密码没有任何意义,所以我怀疑你会找到一个示例。在任何情况下,JPasswordField与任何其他JTextField都没有区别。使用setText()方法,