Introduction to Java Basics (awt&swing implementation login interface)

Keywords: Java Back-end

functional analysis

  • Login interface components: login interface name, account label, password label, account dimension, password dimension, login button and registration button
  • Registration interface components: account label, password label, password confirmation label, registration confirmation button and cancel button
  • Login button function: get the input of the login interface dimension to match with the user's account password. If the matching is successful, enter the home page, and if the matching fails, output error information
  • Registration button function: jump to the registration interface
  • Registration confirmation button function: obtain the input of the dimension on the registration page, compare whether the two password inputs are consistent, and if they are consistent, create a new user
  • Cancel button function: close the registration page

Function realization

  • Generate interface
    Window initialization uses the JFrame class of javax.swing package to instantiate a window object. JFrame provides a variety of window initialization functions. This case uses the setSize window setting method, setResizable window scaling, and setDefaultCloseOperation window closing method to initialize the window. The java.awt package is used for layout initialization. In this case, the flow layout is used, and the FlowLayout is implemented through the setLayout method.
JFrame jf = new JFrame("Login");
jf.setSize(600, 900);
jf.setResizable (false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout fl = new FlowLayout();
  • Generate interface content
    The content components of the interface in this case mainly include the following: label, dimension, button, JTextField and JPasswordField. The generation of these components can be implemented using javax.swing package. The implementation methods are: JLabel(), Dimension(), JButton(), JTextField(), JPasswordField(). After creating a new component, you also need to add the component to the interface. In this case, you need to use * * add() * * to add the component.
JButton btn_sign_in = new JButton("Sign in");
JButton btn_sign_up = new JButton("Sign up");
JTextField namein = new JTextField();
JPasswordField pwdin = new JPasswordField();
JLabel nameJla = new JLabel("Account num:");
JLabel pwdJla = new JLabel ("password: ");
Dimension dim = new Dimension(550, 50);
namein.setPreferredSize(dim);
pwdin.setPreferredSize(dim);
jf.add(nameJla);
jf.add(namein);
jf.add(pwdJla);
jf.add(pwdin);
jf.add(checkBox);
jf.add(btn_sign_in);
jf.add(btn_sign_up);
jf.setVisible(true);
  • Key function
    The response of the key needs to be completed by the key listener. The key listener is actually the implementation class of ActionListener interface. The key listener has only one actionPerformed method to implement. Its parameter ActionEvent e is the key object. The common method is to obtain the key name e.getActionCommand().
public class Class name implements ActionListener {

	public void actionPerformed(ActionEvent e) {
	
	}

}

After configuring the key response, you need to add a listener to the key object. Take a code example of this case to add a login listener to the login button:

SigninListener SigninListener = new SigninListener();
btn_sign_in.addActionListener(SigninListener);
  • Account and password input
    The input of account and Password is realized through the dimension module. There are two input types, one is Text and the other is Password. The difference is that Password will be blocked when entering, but the type of data obtained is the same, all of which are String strings.
JTextField namein = new JTextField();
JPasswordField pwdin = new JPasswordField();
Dimension dim = new Dimension(550, 50);
namein.setPreferredSize(dim);
pwdin.setPreferredSize(dim);
  • User system
    The background data of the real login system should be stored in the database of the server, and the database can be added, deleted, modified and queried through instructions. But a dead horse is a living horse doctor. Let's analyze the functions of this user class:
  1. Initialize an administrator (you need to use singleton mode, and you need to use the same object when performing login and registration functions)
  2. Add new accounts and passwords for many times (accounts and passwords need to be stored in an array)
  3. Search for matching account and password (traverse the account and password array at the same time)
  4. Modify the login status of each user (establish a user status array)
  5. Record the number of users (use static variables to record the number of users)
public class Users {
    String[] username = new String[10];
    String[] userpwd = new String[10];
    int[] online = new int[10];
    static int users_num = 1;

    private Users(String username, String userpwd, int online){
        this.username[0] = username;
        this.userpwd[0] = userpwd;
        this.online[0] = online;
    }

    private static final Users admin = new Users("admin", "123", 0);

    public static Users getUser() {
        return admin;
    }

    public static int getUsers_num() {
        return users_num;
    }

    public static void setUsers_num(int users_num) {
        Users.users_num = users_num + 1;
    }
}

Overall project display

Interface initialization LoginUI

package login;

import java.awt.*;
import javax.swing.*;

public class LoginUI {
    public void initUI(){
        JFrame jf = new JFrame("Login");
        jf.setSize(600, 900);
        jf.setResizable (false);
        jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        FlowLayout fl = new FlowLayout();
        jf.setLayout(fl);

        JButton btn_sign_in = new JButton("Sign in");
        JButton btn_sign_up = new JButton("Sign up");

        JTextField namein = new JTextField();
        JPasswordField pwdin = new JPasswordField();

        JLabel nameJla = new JLabel("Account num:");
        JLabel pwdJla = new JLabel ("password: ");

        JCheckBox checkBox = new JCheckBox("remember password");

        Dimension dim = new Dimension(550, 50);
        namein.setPreferredSize(dim);
        pwdin.setPreferredSize(dim);

        jf.add(nameJla);
        jf.add(namein);
        jf.add(pwdJla);
        jf.add(pwdin);
        jf.add(checkBox);
        jf.add(btn_sign_in);
        jf.add(btn_sign_up);

        jf.setVisible(true);

        SigninListener SigninListener = new SigninListener();
        SignupListener SignupListener = new SignupListener();

        btn_sign_in.addActionListener(SigninListener);
        btn_sign_up.addActionListener(SignupListener);

        SigninListener.setNamein_pwdin(namein, pwdin);


    }

    public static void main(String[] args) {
        new LoginUI().initUI();

    }
}

Login monitoring

package login;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;

public class SigninListener implements ActionListener {
    private JTextField namein;
    private JPasswordField pwdin;

    public void setNamein_pwdin(JTextField namein, JPasswordField pwdin){
        this.namein = namein;
        this.pwdin = pwdin;
    }
    @Override
    public void actionPerformed(ActionEvent e) {
        //String btnstr =  e.getActionCommand ();
        //System.out.println(btnstr + "button click");
        String nameinText = namein.getText();
        String pwdinText = new String(pwdin.getPassword());
        //System.out.println("Acount:"+ nameinText);
        //System.out.println("Password:"+ pwdinText);
        Users user = Users.getUser();
        int login = 0;
        for (int i = 0; i < Users.getUsers_num(); i++){
            if (nameinText.equals(user.username[i])){
                System.out.println ("The account number is correct!");
                if (pwdinText.equals(user.userpwd[i])){
                    System.out.println ("Login succeeded!");
                    login = 1;
                    break;
                }
            }
        }
        if (login == 1){
            UIshow();
        }
        else{
            System.err.println ("Wrong account or password!");
        }
    }
    private void UIshow(){
        JFrame jf = new JFrame("homepage");
        jf.setSize(600, 600);
        jf.setResizable (false);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jf.setVisible(true);
    }
}

Register listening

package login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SignupListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        //String btnstr =  e.getActionCommand ();
        //System.out.println(btnstr + "button click");
        JFrame jf = new JFrame("Sign up");
        jf.setSize(600, 900);
        jf.setResizable (false);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        FlowLayout fl = new FlowLayout();
        jf.setLayout(fl);
        JButton btn_determine = new JButton("determine");
        JButton btn_cancle = new JButton("cancle");
        JTextField namein = new JTextField();
        JPasswordField pwdin = new JPasswordField();
        JPasswordField pwdin2 = new JPasswordField();
        JLabel nameJla = new JLabel("Account num:");
        JLabel pwdJla = new JLabel ("password: ");
        JLabel pwdJla2 = new JLabel ("password again: ");
        Dimension dim = new Dimension(550, 50);
        namein.setPreferredSize(dim);
        pwdin.setPreferredSize(dim);
        pwdin2.setPreferredSize(dim);
        jf.add(nameJla);
        jf.add(namein);
        jf.add(pwdJla);
        jf.add(pwdin);
        jf.add(pwdJla2);
        jf.add(pwdin2);
        jf.add(btn_determine);
        jf.add(btn_cancle);
        jf.setVisible(true);

        SignupdetListener SignupdetListener = new SignupdetListener();
        SignupcancelListener SignupcancelListener = new SignupcancelListener();

        btn_determine.addActionListener(SignupdetListener);
        btn_cancle.addActionListener(SignupcancelListener);

        SignupdetListener.setNamein_pwdin(namein, pwdin, pwdin2);

    }
}

Registration confirmation

package login;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SignupdetListener implements ActionListener{
    private JTextField namein;
    private JPasswordField pwdin;
    private JPasswordField pwdin2;

    public void setNamein_pwdin(JTextField namein, JPasswordField pwdin, JPasswordField pwdin2){
        this.namein = namein;
        this.pwdin = pwdin;
        this.pwdin2 = pwdin2;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String nameinText = namein.getText ();
        String pwdinText = new String(pwdin.getPassword());
        String pwdinText2 = new String(pwdin2.getPassword());
        Users user = Users.getUser();
        if (pwdinText.equals(pwdinText2)){
            //System.out.println("number of users:" + Users.getUsers_num());
            user.username[Users.getUsers_num()] = nameinText;
            user.userpwd[Users.getUsers_num()] = pwdinText;
            //System.out.println("Acount:"+ user.username[Users.getUsers_num()]);
            //System.out.println("Password:"+ user.userpwd[Users.getUsers_num()]);
            Users.setUsers_num(Users.getUsers_num());
            System.out.println ("Registration succeeded!");
            //System.out.println("number of users:" + Users.getUsers_num());

        }
        else{
            System.out.println ("The two passwords are inconsistent!");
        }
    }
}

Registration cancellation (no function)

package login;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SignupcancelListener implements ActionListener {
    @Override
    public void actionPerformed(ActionEvent e) {
        
    }
}

Posted by koray on Sun, 07 Nov 2021 16:38:24 -0800