简体   繁体   English

如何在java中存储用户名和密码并在不同的类中调用它们

[英]How to store usernames and passwords in java and call them in a different class

I have 2 classes: one that creates a JPane login screen called ToolKit and another that is linked to the Toolkit called Register.我有 2 个类:一个创建名为 ToolKit 的 JPane 登录屏幕,另一个链接到名为 Register 的 Toolkit。 When someone opens my app they register their information and it is saved into values like username and password and confirm password.当有人打开我的应用程序时,他们会注册他们的信息,并将其保存为用户名和密码以及确认密码等值。 I want to figure out how to store passwords and username and match the username with the registered password of the user input.我想弄清楚如何存储密码和用户名,并将用户名与用户输入的注册密码相匹配。 Then I want to be able to use the stored username and password in my ToolKit class to allow access to login.然后我希望能够使用我的 ToolKit 类中存储的用户名和密码来允许访问登录。

I tried Array class storing my username and passwords in separate array fields and trying to link them but that seems not to work.我尝试使用 Array 类将我的用户名和密码存储在单独的数组字段中并尝试链接它们,但这似乎不起作用。 I am trying to see if I should use a for loop to create switch statements or something random.我想看看我是否应该使用 for 循环来创建 switch 语句或随机的东西。

Toolkit file:工具包文件:

This is the login button:这是登录按钮:

JButton btnLogin = new JButton("Login");
btnLogin.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {

When you press login it clears the text fields:当您按登录时,它会清除文本字段:

String password = txtPassword.getText();
String username = txtUsername.getText();
//Dont forget condtion if statements. Like breaking down capitalization

I want to set "King" to what ever the user stores as username in the Register class and do the same for password.我想将“King”设置为用户在 Register 类中存储为用户名的内容,并对密码执行相同操作。

        if(password.contains("King") && username.contains("one")) {
            txtPassword.setText(null);
            txtUsername.setText(null);
        }
        else
        {

Just an Invalid Login Message this will be changed later to have more invalid login conditions.只是一个无效的登录消息,稍后将更改为具有更多无效登录条件。

JOptionPane.showMessageDialog(null, "Invalid Login Details Have you Registered Yet?", "Login Error", JOptionPane.ERROR_MESSAGE);
            txtPassword.setText(null);
            txtUsername.setText(null);

Register Class: This is the second class and has a Jlabel that I turned into a button.注册类:这是第二类,有一个 Jlabel,我把它变成了一个按钮。

JLabel Register = new JLabel("Register");

Register.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
        Register.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

When Register is clicked a few things will happen.单击注册时,会发生一些事情。

As of right now when the Register Label is pushed it stores what ever the user input is into the variable name and password.截至目前,当注册标签被推送时,它会将用户输入的内容存储到变量名称和密码中。

How do i take those values and make them linked together and then store them and move them over to another class?我如何获取这些值并将它们链接在一起,然后存储它们并将它们移动到另一个类?

String name =  userName.getText();
String password = passwordField.getText();

I expect that while the app is open the user can create multiple account with different usernames and passwords that will stay linked together and will be able to go to the Toolkit class and Login with the password and username they created in the Register Class.我希望当应用程序打开时,用户可以使用不同的用户名和密码创建多个帐户,这些帐户将保持链接在一起,并且能够转到 Toolkit 类并使用他们在注册类中创建的密码和用户名登录。

You have a few options,你有几个选择,

  1. Make them static public variables.使它们成为static public变量。 ( Reference ) 参考
  2. Create a singleton for the 'user', which contains the name and password.为“用户”创建一个单例,其中包含名称和密码。 This way, the next time you access it, you get the old value.这样,下次访问它时,您将获得旧值。 ( Reference 1 , Reference 2 , Reference 3 ) 参考文献1参考文献2参考文献3
  3. Store the value in a file (or) local database and retrieve from it when required.将值存储在文件(或)本地数据库中,并在需要时从中检索。 ( Reference 1 ,Reference 2 ) 参考文献1参考文献2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Selenium 中打开不同的 URL(使用不同的用户名和密码) - How to open Different URLs(with different usernames and passwords) in Selenium Java在哪里存储记住的HTTP基本身份验证密码,以及如何删除它们? - Where does Java store remembered HTTP basic auth passwords and how do you delete them? 如何使用servlet / jsp / javabeans以非常简单的注册形式存储用户名和密码? - How to store usernames and passwords in a very simply registration form using servlet/jsp/javabeans? 如何使用Selenium WebDriver优雅地测试同一网站的不同用户名和密码? - How to test for different usernames and passwords for same website using Selenium WebDriver elegantly? 如何在属性文件中保存多个用户名和密码 - How to hold multiple usernames and passwords in properties file Java:我如何从不同的 class 调用数组,我要修改它们吗? - Java: How do I call the array from different class, and I do I modify them? 如何存储Java Web应用程序所需的密码? - How to store java web application needed passwords? 在android中存储不同的密码 - Store different passwords in android 如何在另一个Java类中将带有变量的字符串存储在它们中? - How can I store strings with variables in them in another Java class? Java-如何在不同的类中调用add()方法 - Java - how to call an add() method in a different class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM