Java基础入门(awt&swing实现登录界面)

登录系统

功能分析

  • 登录界面组件:登录界面名、账号label、密码label、账号dimension、密码dimension、登录button、注册button
  • 注册界面组件:账号label、密码label、密码确认label、注册确认button、取消button
  • 登录button功能:获取登录界面dimension的输入与用户的账号密码进行匹配,匹配成功则进入主页,匹配失败输出错误信息
  • 注册button功能:跳转注册界面
  • 注册确认button功能:获取注册页面dimension的输入,比对两次的密码输入是否一致,若一致则新建用户
  • 取消button功能:关闭注册页面

功能实现

  • 生成界面
    窗口初始化使用javax.swing包的JFrame类实例化一个窗口对象,JFrame提供多种窗口初始化功能,本案例使用了setSize窗口设置方法setResizable窗口缩放setDefaultCloseOperation窗口关闭方式初始化窗口。布局初始化使用java.awt包,本案例使用流式布局FlowLayout通过setLayout方法实现。
JFrame jf = new JFrame("Login");
jf.setSize(600, 900);
jf.setResizable (false);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
FlowLayout fl = new FlowLayout();
  • 生成界面内容
    本案例里界面的内容组件主要有以下:label(标签)、dimension(输入框)、button(按钮)、JTextField(账号输入)、JPasswordField(密码输入)。这些组件的生成都可以使用javax.swing包实现,其实现方法分别是:JLabel()Dimension()JButton()JTextField()JPasswordField()。新建完组件后还需要把组件添加到界面中,此时需要用到**add()**添加组件。
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);
  • 按键功能
    按键的响应需要由按键监听器完成。按键监听器实则就是ActionListener接口的实现类。按键监听器只有一个actionPerformed方法需要实现,其参数ActionEvent e是按键对象,常用方法就是获取按键名称e.getActionCommand ()
public class 类名 implements ActionListener {

	public void actionPerformed(ActionEvent e) {
	
	}

}

配置完成按键响应后需要在往按键对象里添加监听器,用本案例的一个代码举例,往登录按钮添加登录监听器:

SigninListener SigninListener = new SigninListener();
btn_sign_in.addActionListener(SigninListener);
  • 账号和密码输入
    账号密码输入是通过dimension模块实现的,其中有两种输入类型,一种是Text,另一种时Password。其区别在于输入时Password会进行遮挡,但是获取数据的类型是一样的均为String字符串
JTextField namein = new JTextField();
JPasswordField pwdin = new JPasswordField();
Dimension dim = new Dimension(550, 50);
namein.setPreferredSize(dim);
pwdin.setPreferredSize(dim);
  • 用户系统
    真实的登录系统的后台数据应该存储在服务器的数据库里,通过指令进行数据库的增、删、改、查。但是死马当活马医,我们来分析一下这个用户类的功能:
  1. 初始化一个管理员(需要使用单例模式,在执行登录和注册这两个功能时需要使用同一个对象
  2. 多次添加新的账号和密码(账号和密码需要数组进行存储
  3. 搜索匹配账号和密码(对账号和密码数组进行同时遍历
  4. 修改每个用户的登录状态(建立用户状态数组
  5. 记录用户数量(使用静态变量记录用户数量
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;
    }
}

整体项目展示

界面初始化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();

    }
}

登录监听

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+"按钮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 ("账号正确!");
                if (pwdinText.equals(user.userpwd[i])){
                    System.out.println ("登录成功!");
                    login = 1;
                    break;
                }
            }
        }
        if (login == 1){
            UIshow();
        }
        else{
            System.err.println ("账号或密码错误!");
        }
    }
    private void UIshow(){
        JFrame jf = new JFrame("主页");
        jf.setSize(600, 600);
        jf.setResizable (false);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jf.setVisible(true);
    }
}

注册监听

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+"按钮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);

    }
}

注册确认

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("用户人数:"+ 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 ("注册成功!");
            //System.out.println("用户人数:"+ Users.getUsers_num());

        }
        else{
            System.out.println ("两次密码不一致!");
        }
    }
}

注册取消(无功能)

package login;

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

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

上一篇:用浏览器打开网址


下一篇:Selenium的Actions类不能模拟键盘操作浏览器,要用java.awt的Robot类