java 密码为星号 (*)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15575929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 20:07:41  来源:igfitidea点击:

Password as asterisk (*)

javapasswords

提问by Sojib

my password program can hide keyboard input . its invisible when i type password.

我的密码程序可以隐藏键盘输入。当我输入密码时它是不可见的。

But my qus is how can show password input as asterisk (*)

但我的问题是如何将密码输入显示为星号(*

can any one help me please? :p

有人可以帮我吗?:p

my code is here:

我的代码在这里:

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

回答by Survivor

You can use JPasswordField to do that; Below is an example:

您可以使用 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);
            }
        }
    });
}