java实验:完成一个电商购物平台的登录注册窗体

电商购物平台pro plus

(越来越像样了奥


文章目录

前言

一、实验内容

二、实验思路

三、实验代码

四、实验结果截图

总结


前言

继续对电商购物平台进行改进和升级,加上用户登录及注册页面

(虽然前几次升级好像没发

一个有点小复杂的东西,完成的不是很完善


一、实验内容

完成一个电商购物平台的登录注册窗体,点击“点我注册”,显示注册窗体,用户输入用户信息,点击提交后到达显示用户信息的窗体。窗体如图2所示。

在内容2的基础上,用户点击登录注册窗体的登录按钮,到达商品信息查询的窗体,窗体如图3所示

二、实验思路

和计算器的类似,按照所给的图(需求),写出需要的组件(也就是用户名,密码,登录,注册等按钮),添加窗体所需要的库,按照需求摆好按钮所需要的位置,用监听器将功能部署到按钮上去

让按钮可以实现需要的功能

三、实验代码

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();
    }
}

四、实验结果截图


总结

一个奇怪的东西,还没有完全完成(不知道为什么有的按钮不按照我想象的形式出现,让我再看看改改(×)

猜你喜欢

转载自blog.csdn.net/m0_72471315/article/details/127873129