How do I Prompt the user to enter a password before entering the main program?

前端 未结 3 1379
庸人自扰
庸人自扰 2020-12-21 02:16

What I need to do is to prompt the user with a username and password (authentication is done locally) and if it checks out, the user would then be able to access the main pr

3条回答
  •  星月不相逢
    2020-12-21 03:17

    You can use showInputDialog to get the user name

    and the below code to get the password

    JLabel label = new JLabel("Please enter your password:");
    JPasswordField jpf = new JPasswordField();
    JOptionPane.showConfirmDialog(null,
      new Object[]{label, jpf}, "Password:",
      JOptionPane.OK_CANCEL_OPTION);
    

    and write a if condition to check the user name and password

    if (!isValidLogin()){
    //You can give some message here for the user
    System.exit(0);
    }
    

    // If login is validated, then the user program will proceed further

提交回复
热议问题