[英]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,你有几个选择,
static public
variables.static public
变量。 ( Reference )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.