gpt4 book ai didi

java - JOptionPane 获取密码

转载 作者:IT老高 更新时间:2023-10-28 20:34:11 25 4
gpt4 key购买 nike

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

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

最佳答案

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

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));
}

关于java - JOptionPane 获取密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8881213/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com