【问题标题】:How do I Prompt the user to enter a password before entering the main program?如何在进入主程序前提示用户输入密码?
【发布时间】:2012-02-02 05:08:24
【问题描述】:

我需要做的是提示用户输入用户名和密码(身份验证在本地完成),如果签出,用户就可以访问主程序主体。

public static void main(String[] args){

  //String input = JOptionPane.showInputDialog("Enter password to continue: ");
  //input2 = Integer.parseInt(input);


  // followed by the creation of the main frame
  new Cashier3();
  Cashier3 frame = new Cashier3();
  frame.setTitle("CASHIER 2");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.setVisible(true);

有什么快速的方法可以做到这一点吗?

【问题讨论】:

    标签: java swing user-interface joptionpane


    【解决方案1】:

    你可以使用showInputDialog来获取用户名

    和下面的代码来获取密码

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

    并写一个if条件来检查用户名和密码

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

    // 如果登录验证通过,则用户程序将继续进行

    【讨论】:

      【解决方案2】:

      您可以简单地在您的程序中添加一个静态块来进行身份验证,这个静态块总是在 main 方法之前执行。如果用户无效,请使用

      System.exit(0);
      

      退出程序。否则程序将照常开始执行。

      这里有一个示例程序可以给你一些想法:

      import java.awt.Color;
      import javax.swing.*;
      
      public class Validation extends JFrame
      {
          private static Validation valid = new Validation();
          static
          {
              String choice = JOptionPane.showInputDialog(valid, "Enter Password", "Password", JOptionPane.PLAIN_MESSAGE);
              if ((choice == null) || ((choice != null) && !(choice.equals("password"))))
                  System.exit(0);
          }
      
          private static void createAndDisplayGUI()
          {
              valid.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              valid.setLocationRelativeTo(null);
      
              valid.getContentPane().setBackground(Color.YELLOW);
      
              valid.setSize(200, 200);
              valid.setVisible(true);
          }
          public static void main(String... args)
          {
              SwingUtilities.invokeLater(new Runnable()
              {
                  public void run()
                  {
                      createAndDisplayGUI();
                  }
              });
          }
      }
      

      【讨论】:

      • 这很有意义。我还没有真正接触过静态块,直到现在才真正看到需要。谢谢,这让它更清楚了。
      • @user976123:欢迎您,很高兴我能帮上忙。问候
      【解决方案3】:
            String userName = userNameTF.getText();
            String userPassword = userPasswordPF.getText();
              if(userName.equals("xian") && userPassword.equals("1234"))
              {
                JOptionPane.showMessageDialog(null,"Login successful!","Message",JOptionPane.INFORMATION_MESSAGE); 
                // place your main class here... example: new L7();
              }
              else 
              {
                 JOptionPane.showMessageDialog(null,"Invalid username and password","Message",JOptionPane.ERROR_MESSAGE); 
                 userNameTF.setText("");
                 userPasswordPF.setText("");                       
              }  
      

      【讨论】:

      • 请参阅formatting help 了解如何格式化代码块 :) 或者下次只需使用 {} 按钮。
      猜你喜欢
      • 2016-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-05
      • 2011-07-28
      • 1970-01-01
      • 2014-04-12
      • 2012-04-03
      相关资源
      最近更新 更多