如何在进入主程序之前提示用户输入密码? [英] How do I Prompt the user to enter a password before entering the main program?

查看:85
本文介绍了如何在进入主程序之前提示用户输入密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 program body.

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

有没有快速的方法可以做到这一点?

is there any quick way to go about doing this?

推荐答案

您只需在程序中添加静态块,您将在其中进行身份验证,此静态块始终在main方法之前执行。如果用户无效,请使用

You can simply add a static block to your program where you will do your authentication, this static block is always executed before the main method. If the user is not valid go with

System.exit(0);

退出程序。此外,该程序将照常开始执行。

to exit the program. Else the program will start execution as usual.

这是一个示例程序,可以给你一些想法:

Here is one sample program to give you some idea :

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

这篇关于如何在进入主程序之前提示用户输入密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆