我的密码程序可以隐藏键盘输入。当我输入密码时它是不可见的。
但我的问题是如何将密码输入显示为星号( * )
任何人都能帮帮我吗? :P
我的代码在这里:
public void login()
{
Scanner input = new Scanner(System.in);//create Scanner object
System.out.println("\n");
Console console = System.console();
char[] ad_password = "admin".toCharArray();
char[] sm_password = "salesman".toCharArray();
char[] passwordEntered = console.readPassword("Enter Password To Access The Project ( As Admin / Sales Man ): ");
if (Arrays.equals(ad_password, passwordEntered))
{
System.out.println("\n Congratulation!!! Access granted \n");
System.out.println("\n Welcome Admin. :-) \n\n");
Shoping_mall obj_ad_dis=new Shoping_mall();
obj_ad_dis.ad_dis();
}
else if(Arrays.equals(sm_password, passwordEntered))
{
System.out.println("\n Congratulation!!! Access granted \n");
System.out.println("\n Welcome Sales Man. :-) \n\n");
Shoping_mall obj1_sm_dis=new Shoping_mall();
obj1_sm_dis.sm_dis();
}
else
{
System.out.println("\n Error: Your Password Doesn't Meet. Access Denied !!! :-( \n\n");
System.out.println("\n Enter Correct Password.\n\n");
Shoping_mall obj_login=new Shoping_mall();//creat object for calling Admin_works() method
obj_login.login();
}
}
答案 0 :(得分:0)
你可以使用JPasswordField来做到这一点;以下是一个例子:
private JPasswordField password;
private String typedPassword;
private final String defaultPassword = "yourDesiredPassword";
public void createPasswordField(){
password = new JPasswordField(30);
password.setBounds(280, 240, 90, 20);
password.setEchoChar('*');
password.setBackground(Color.white);
password.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
password = (JPasswordField) e.getSource();
char [] tempPass = password.getPassword();
typedPassword = new String(tempPass);
if (!typedPassword.equals(defaultPassword)){
JOptionPane.showMessageDialog(null,
"Your password is not correct",
"Stack Over Flow example",
JOptionPane.ERROR_MESSAGE);
}
}
});
}