Java experiment: complete the login registration form of an e-commerce shopping platform

E-commerce shopping platform pro plus

(It's getting more and more decent


Article directory

foreword

1. Experimental content

2. Experimental ideas

3. Experiment code

4. Screenshot of Experimental Results

Summarize


foreword

Continue to improve and upgrade the e-commerce shopping platform, plus user login and registration pages

(Although the previous upgrades seem to have not been released

A little complicated thing, the completion is not very perfect


1. Experimental content

Complete the login and registration form of an e-commerce shopping platform, click "Click me to register" to display the registration form, the user enters user information, clicks submit and arrives at the form displaying user information. The form is shown in Figure 2.

On the basis of content 2, the user clicks the login button of the login registration form to arrive at the product information query form, as shown in Figure 3

2. Experimental ideas

Similar to the calculator, according to the given diagram (requirements), write the required components (that is, user name, password, login, registration, etc. buttons), add the library required by the form, and arrange the buttons required according to the requirements position, deploy the function to the button with a listener

Let the button achieve the desired function

3. Experiment code

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

public class LoginRegFrame extends JFrame {
    JLabel l_id,l_role,l_password;
    JTextField t_id;
    JComboBox<String> c_roel;//泛型
    JPasswordField p_password;
    JButton b_login,b_reset,b_regist;
    public LoginRegFrame(){
        setTitle("登录注册页面");
        setSize(350,300);
        setLocation(200,150);
        init();

        setVisible(true);
    }
    public void init() {
        setLayout(null);
        l_id = new JLabel("用户名", JLabel.CENTER);
        l_role = new JLabel("用户类型", JLabel.CENTER);
        l_password = new JLabel("密码", JLabel.CENTER);
        t_id = new JTextField();
        c_roel = new JComboBox<String>();
        c_roel.addItem("管理员");
        c_roel.addItem("普通用户");
        p_password = new JPasswordField();
        JPanel p = new JPanel();//创建中间容器
        p.setLayout(new GridLayout(3, 2));//设置面板布局方式
        p.add(l_id);
        p.add(t_id);//将组件添加到面板上
        p.add(l_role);
        p.add(c_roel);
        p.add(l_password);
        p.add(p_password);
        p.setBounds(5, 5, 320, 190);//设置面板放在窗体的位置及大小
        add(p);

        p = new JPanel();
        b_login = new JButton("登录");
        b_login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new LoginerFrame();
            }
        });

        b_reset = new JButton("重置");
        b_regist = new JButton("点我注册");
        b_regist.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new RegesiterFrame();
            }
        });
        p.setLayout(new GridLayout(1, 3, 5, 5));
        p.add(b_login);
        p.add(b_reset);
        p.add(b_regist);
        p.setBounds(5, 200, 320, 60);
        add(p);
    }
}




public class User {
    private String id,name,password;
    private char sex;
    private String city;

    public User(){
        super();
    }

    public User(String id,String name,String password,char sex,String city){
        super();
        this.id=id;
        this.name=name;
        this.password=password;
        this.sex=sex;
        this.city=city;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    @Override
    public String toString(){
        return "User[id="+id+",name="+name+",psaaword="+password+",sex="+sex+",city="+city+"]";
    }
}




import entity.User;

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

public class RegesiterFrame extends JFrame {
    JLabel l_id,l_name,l_passworld1,l_passworld2,l_sex,l_city;
    JTextField t_id,t_name;
    JPasswordField p_passworld1,p_passworld2;
    //想要保证单选按钮实现单选,需要将这两个按钮放到一个组里
    ButtonGroup bg;
    JRadioButton r_boy,r_girl;
    JComboBox<String> c_city;//泛型化了
    JButton b_register,b_reset;
    public RegesiterFrame(){
        super("电商购物平台注册页面");
        setSize(300,450);
        setLocation(700,200);
        init();
        setVisible(true);
    }
    public void init(){
        l_id=new JLabel("账号",JLabel.CENTER);
        l_name=new JLabel("姓名",JLabel.CENTER);
        l_passworld1=new JLabel("密码",JLabel.CENTER);
        l_passworld2=new JLabel("确认密码",JLabel.CENTER);
        l_sex=new JLabel("性别",JLabel.CENTER);
        l_city=new JLabel("城市",JLabel.CENTER);

        t_id=new JTextField();
        t_name=new JTextField();

        p_passworld1=new JPasswordField();
        p_passworld2=new JPasswordField();

        bg=new ButtonGroup();
        r_boy=new JRadioButton("男");
        r_girl=new JRadioButton("女");
        bg.add(r_boy);
        bg.add(r_girl);

        c_city=new JComboBox<String>();
        c_city.addItem("北京");
        c_city.addItem("上海");
        c_city.addItem("广州");

        b_register=new JButton("注册");
        b_register.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String id=t_id.getText();
                String name=t_name.getText();
                String password=null;
                String password1=new String(p_passworld1.getPassword());
                String password2=new String(p_passworld2.getPassword());
                if(password1.equals(password2)){
                    password=password1;
                }else{
                    JOptionPane.showMessageDialog(p_passworld1,"两次输入的密码不一致");
                }
                char sex;
                if(r_boy.isSelected()){
                    sex = '男';
                }else{
                    sex = '女';
                }
                String city = (String)c_city.getSelectedItem();
                User user=new User(id,name,password,sex,city);
                user.toString();
                new UserInfoFrame(user);
            }
        });
        b_reset=new JButton("重置");
        b_reset.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

            }
        });

        setLayout(new GridLayout(7,2,5,5));
        add(l_id); add(t_id);
        add(l_name); add(t_name);
        add(l_passworld1); add(p_passworld1);
        add(l_passworld2); add(p_passworld2);
        add(l_sex);
        JPanel p=new JPanel();//中间容器
        p.add(r_boy); p.add(r_girl);
        add(p);
        add(l_city); add(c_city);
        add(b_register); add(b_reset);
    }
}




import entity.User;

import javax.swing.*;

    public class UserInfoFrame extends JFrame{
        public UserInfoFrame(User user) {
            super("电商购物平台—用户信息");
            setSize(400, 300);
            setLocation(600, 200);
            init(user);
            setVisible(true);
        }
        public void init(User u) {
            JTable t;
            Object[] title = {"用户名", "姓名", "密码", "性别", "城市"};
            Object[][] users = {
   
   {u.getId(), u.getName(), u.getPassword(), u.getSex(), u.getCity()}};
            t = new JTable(users, title);
            add(new JScrollPane(t));
        }
}




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

public class LoginerFrame extends JFrame {
    private JLabel l_name,l_lei,l_id,l_zuozhe,l_kucun,l_fenlei,l_chaxun,l_buy;
    private JTextField t_id,t_name,t_zuozhe,t_kucun,t_fenlei;
    private JComboBox<String> c_lei;
    private JButton b_chaxun,b_buy;
    private JTable t_bookmessage;

    public LoginerFrame(){
        super("电商购物平台,商品查询页面");
        setSize(400,100);
        setLocation(700,600);
        init();
        setVisible(true);
    }

    public void init(){
        l_name=new JLabel("您好");
        l_id=new JLabel("书籍编号",JLabel.CENTER);
        l_lei=new JLabel("分类:");
        t_name =new JTextField();
        t_id=new JTextField();
        c_lei=new JComboBox<String>();
        c_lei.addItem("教材");
        c_lei.addItem("课外书");
        l_chaxun=new JLabel("查询");
        b_chaxun=new JButton("查询");

        l_lei=new JLabel("分类",JLabel.CENTER);
        l_name=new JLabel("书籍名",JLabel.CENTER);
        Object[] bookTitle = {"书籍编号","书籍名称","书籍作者","库存","书籍分类"};
        Object[][] book = {new Object[]{"b00001", "数据结构与算法", "严蔚敏", 30,"软件开发", "算法设计"},{"b00002","java开发语言","李华玲",30,"软件开发","java编程"},{"b00003","数据库概论","蒋本珊",30,"软件开发","数据库设计"},{"b00004","线性代数","高玉斌",30,"基础学科","数学"}};
        t_bookmessage = new JTable(book,bookTitle);
        setLayout(null);

        l_name=new JLabel("购物车商品数:");
        l_buy=new JLabel("购买");
        b_buy=new JButton("购买");

        JPanel p=new JPanel();
        p.setLayout(new GridLayout(1, 2, 15, 25));

        p.add(l_name);

        p.setBounds(5, 30, 670, 20);
        add(p);

        JPanel p2=new JPanel();
        p2.setLayout(new GridLayout(1,2,10,25));
        p2.add(l_name);
        p2.add(t_name);
        p2.add(l_lei);
        p2.add(c_lei);
        p2.add(b_chaxun);

        p2.setBounds(5,50,670,20);
        add(p2);

        JPanel p3=new JPanel();
        p3.setLayout(new GridLayout(1,1,10,25));
        p3.add(new JScrollPane(t_bookmessage));

        p3.setBounds(5,100,670,300);
        add(p3);

        JPanel p4=new JPanel();
        p4.setLayout(new GridLayout(30,30,15,25));
        p4.add(l_name);
        p4.add(b_buy);
        p4.setBounds(5,130,670,20);
        add(p4);

    }
}



public class Test {
    public static void main(String[] args){
        LoginRegFrame loginRegFrame=new LoginRegFrame();
    }
}

4. Screenshot of Experimental Results


Summarize

A strange thing, not fully finished (I don’t know why some buttons don’t appear in the form I imagined, let me see and change again (×)

Guess you like

Origin blog.csdn.net/m0_72471315/article/details/127873129