JOptionPane 获取密码 [英] JOptionPane to get password

查看:22
本文介绍了JOptionPane 获取密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JOptionPane 可用于从用户获取字符串输入,但就我而言,我想在 showInputDialog 中显示密码字段.

JOptionPane can be used to get string inputs from user, but in my case, I want to display a password field in showInputDialog.

我需要的方式是用户给出的输入应该被屏蔽并且返回值必须在char[]中.我需要一个带有消息、密码字段和两个按钮的对话框.可以做到吗?谢谢.

The way I need is the input given by the user should be masked and the return value must be in char[]. I need a dialog box with a message, password field, and two buttons. Can that be done? Thanks.

推荐答案

是的,可以使用 JOptionPane.showOptionDialog().像这样:

Yes, it is possible using JOptionPane.showOptionDialog(). Something like this:

JPanel panel = new JPanel();
JLabel label = new JLabel("Enter a password:");
JPasswordField pass = new JPasswordField(10);
panel.add(label);
panel.add(pass);
String[] options = new String[]{"OK", "Cancel"};
int option = JOptionPane.showOptionDialog(null, panel, "The title",
                         JOptionPane.NO_OPTION, JOptionPane.PLAIN_MESSAGE,
                         null, options, options[1]);
if(option == 0) // pressing OK button
{
    char[] password = pass.getPassword();
    System.out.println("Your password is: " + new String(password));
}

这篇关于JOptionPane 获取密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆