JavaSwing(GUI窗口)+Mysql实现简单的库存管理系统(角色:普通用户/管理员 商品的增删查等)

JavaSwing库存管理系统

本系统简单实现了库存管理和角色权限管理,管理员可以管理和发布商品,普通用户浏览商品需求

实现功能截图

登录页面:
在这里插入图片描述
不同角色校验:
在这里插入图片描述
管理员页面:
在这里插入图片描述
普通用户页面:
在这里插入图片描述

系统功能

本库存管理系统实现了以下功能:
管理员/普通用户权限管理
不同角色登录
商品的增删查

使用技术

数据库:mysql
开发工具:netbeans(Eclipse、Myeclispe、Idea也可以)
知识点:JavaSwing

由于功能简单,代码都放在了一个包下面:
在这里插入图片描述

代码

登录页面:

package com.stock.java;

import javax.swing.*;
import javax.swing.plaf.InsetsUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;
import java.text.SimpleDateFormat;

/**
 * @Author: wen
 * @Date: 2020/11/20 22:28
 * @Description: 登陆模块
 * @Version: 1.0
 */
public class Login extends JFrame {

    private static final long serialVersionUID = 1L;
    private JPanel jContentPane = null;
    private JButton jButton21 = null;
    private JButton jButton22 = null;
    private JTextField jTextField = null;
    private JPasswordField jPasswordField = null;
    private JLabel jLabel = null;
    public static String storeUserName = null;// 登录用户名
    public static String storeUserPassword = null;// 登录密码
    static boolean RELOAD = true;// 重新登陆标记
    static int login_user_type;// 1表示管理员,0销售
    private JLabel jLabel_User = null;
    private JLabel jLabel_userName = null;
    private JLabel jLabel_password = null;
    private JLabel jLabel_privilege = null;
    private URL imgURL = null;

    private BtnListener btl = null;
    private JComboBox jComboBox = null;
    private JLabel jLabel_tips = null;

    private void initialize() {

        jLabel_tips = new JLabel();
        jLabel_tips.setBounds(new Rectangle(15, 247, 277, 24));
        this.setResizable(false);
        this.setSize(296, 356);
        // this.setSize(350, 450);
        this.setTitle("Welcome Login");
        imgURL = this.getClass().getResource(
                "icon.png");
        this.setIconImage(Toolkit.getDefaultToolkit().getImage(imgURL));
        this.setLocationRelativeTo(null);
        // this.setUndecorated(true);//设置无边框
        try {
            UIManager
                    .setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");// 使用windows外观
        } catch (Exception e) {
            e.printStackTrace();
        }
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        jButton21 = new JButton();
        jButton21.setBounds(new Rectangle(15, 295, 78, 26));

        imgURL = this.getClass().getResource("/com/code2life/user/images/icon.png");

        jButton21.setText("login");
        // 允许回车登录
        getRootPane().setDefaultButton(jButton21);

        jButton22 = new JButton();
        jButton22.setBounds(new Rectangle(110, 296, 78, 26));
        jButton22.setText("exit");

        jTextField = new JTextField(20);
        jTextField.setBounds(new Rectangle(120, 180, 124, 23));

        jPasswordField = new JPasswordField();
        jPasswordField.setBounds(new Rectangle(120, 210, 124, 23));

        jLabel = new JLabel();
        jLabel.setText("");
        jLabel.setBounds(new Rectangle(0, -1, 291, 142));
        imgURL = this.getClass()
                .getResource("login.gif");
        jLabel.setIcon(new ImageIcon(imgURL));
        jLabel_password = new JLabel();
        jLabel_password.setBounds(new Rectangle(29, 210, 71, 19));
        jLabel_password.setText("password:");
        jLabel_userName = new JLabel();
        jLabel_userName.setBounds(new Rectangle(29, 181, 71, 19));
        jLabel_userName.setText("username:");
        jLabel_User = new JLabel();
        jLabel_User.setBounds(new Rectangle(10, 147, 275, 98));

        jLabel_privilege = new JLabel();
        jLabel_privilege.setBounds(new Rectangle(18, 272, 71, 19));
        jLabel_privilege.setText("type:");

        jComboBox = new JComboBox();
        jComboBox.setBounds(new Rectangle(109, 272, 123, 23));
        jComboBox.addItem("sale");
        jComboBox.addItem("admin");

        imgURL = this.getClass().getResource("user.gif");
        jLabel_User.setIcon(new ImageIcon(imgURL));
        jLabel_User.setText("User");

        jContentPane = new JPanel();// 新建jPanel面板
        jContentPane.setLayout(null);
        jContentPane.add(jLabel_userName, null);
        jContentPane.add(jLabel_password, null);
        jContentPane.add(jButton21, null);
        jContentPane.add(jButton22, null);
        jContentPane.add(jTextField, null);
        jContentPane.add(jPasswordField, null);
        jContentPane.add(jLabel, null);
        jContentPane.add(jLabel_User, null);

        jContentPane.add(jComboBox, null);
        jContentPane.add(jLabel_privilege, null);
        jContentPane.add(jLabel_tips, null);
        setContentPane(jContentPane);

        btl = new BtnListener();
        jButton21.addActionListener(btl);
        jButton22.addActionListener(btl);

    }

    /**
     * @author Administrator
     * @监听类
     */
    public class BtnListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == jButton21) {
                GoodsService ud = new GoodsService();
                String user = jTextField.getText().trim();
                String password = new String(jPasswordField.getPassword())
                        .trim();// char to String

                storeUserName = user;
                storeUserPassword = password;
                login_user_type = jComboBox.getSelectedIndex();

                if ("".equals(user)) {
                    JOptionPane.showMessageDialog(null, "用户名不能为空");
                    return;
                }
                if ("".equals(password)) {
                    JOptionPane.showMessageDialog(null, "密码不能为空");
                    return;
                }
                SimpleDateFormat sdf = new SimpleDateFormat(
                        "yyyy-MM-dd HH:mm:ss");
                String dt = sdf.format(new java.util.Date());
                // 登陆
                    if (ud.userLogin(login_user_type, storeUserName,
                            storeUserPassword)) {

                        dispose();
                        try {

                            UIManager.put("RootPane.setupButtonVisible", false);
                            UIManager.put("ToolBar.isPaintPlainBackground",
                                    Boolean.FALSE);
                            UIManager.put("TabbedPane.tabAreaInsets",
                                    new InsetsUIResource(0, 0, 0, 0));
                            UIManager.put("TabbedPane.contentBorderInsets",
                                    new InsetsUIResource(0, 0, 2, 0));
                            UIManager.put("TabbedPane.tabInsets",
                                    new InsetsUIResource(3, 10, 9, 10));
                            Font frameTitleFont = (Font) UIManager
                                    .get("InternalFrame.titleFont");
                            frameTitleFont = frameTitleFont
                                    .deriveFont(Font.PLAIN);
                            UIManager.put("InternalFrame.titleFont",
                                    frameTitleFont);
                        } catch (Exception e1) {
                            // TODO exception
                        }
                        GoodsFrame mf = new GoodsFrame(user);
                        mf.setVisible(true);
                    } else {
                        JOptionPane.showMessageDialog(null, "login fail");
                        return;
                    }
            } else if (e.getSource() == jButton22) {
                System.exit(0);
            }
        }
    }

    /**
     * @param args
     * @throws Exception
     * @主函数
     */
    public static void main(String[] args) throws Exception {
        try {
            UIManager.put("RootPane.setupButtonVisible", false);
            UIManager.put("ToolBar.isPaintPlainBackground", Boolean.FALSE);

        } catch (Exception e) {
            // TODO exception
        }
        UIManager.put("RootPane.setupButtonVisible", false);
        UIManager.put("TabbedPane.tabAreaInsets", new InsetsUIResource(0, 0, 0,
                0));
        UIManager.put("TabbedPane.contentBorderInsets", new InsetsUIResource(0,
                0, 2, 0));
        UIManager.put("TabbedPane.tabInsets",
                new InsetsUIResource(3, 10, 9, 10));
        Font frameTitleFont = (Font) UIManager.get("InternalFrame.titleFont");
        frameTitleFont = frameTitleFont.deriveFont(Font.PLAIN);
        UIManager.put("InternalFrame.titleFont", frameTitleFont);

        Login login = new Login(RELOAD);
        login.setVisible(true);
    }
    public Login() {
        super();
        initialize();
    }
    public Login(boolean reload) {
        super();
        initialize();
    }
}

商品管理页面:

package com.stock.java;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;

public class GoodsFrame extends JFrame {

	private JPanel contentPane;
	private JTextField textField_1;
	private JTable table;
	private String[] columnCount= {"ID","goods_name","quantity","price","time"};
	private List<Goods> list;
	public static Goods goo;
	public static GoodsFrame frame;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new GoodsFrame("admin");
					//窗口居中
					frame.setLocationRelativeTo(null);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public GoodsFrame(String type) {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 764, 469);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(29, 58, 692, 332);
		contentPane.add(scrollPane);
		
		table = new JTable();
		scrollPane.setViewportView(table);
		
		textField_1 = new JTextField();
		textField_1.setBounds(30, 22, 40, 23);
		contentPane.add(textField_1);
		
		JButton button = new JButton("query");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				quaryAll();
			}
		});
		
		button.setBounds(80, 22, 70, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("add");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new FromFjame().setVisible(true);
			}
		});
		button_1.setBounds(400, 22, 70, 23);
		if (type.equals("admin")){
			contentPane.add(button_1);
		}
		
		
		//全屏
//		setExtendedState(JFrame.MAXIMIZED_BOTH);
		
		
		JButton button_3 = new JButton("delete");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				remove();
				quaryAll();
			}
		});
		button_3.setBounds(510, 22, 70, 23);
//		contentPane.add(button_3);
		if (type.equals("admin")){
			contentPane.add(button_3);
		}
		
		
		JButton button_4 = new JButton("close");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);
			}
		});
		button_4.setBounds(650, 22, 70, 23);
		contentPane.add(button_4);
		
	}
	//查询
	public void quaryAll() {
		String select_val = textField_1.getText();
		System.out.println(select_val);
		GoodsService ss=new GoodsService();
		list = ss.queryAll(select_val);
		if(list==null) {
			JOptionPane.showMessageDialog(null, "Server is busy!");
			return;
		}
		Object[][] data = Util.listToArray(list);
		table.setModel(new DefaultTableModel(data, columnCount));
	}
	
	//删除
	private void remove() {
		int i = table.getSelectedRow();
		Goods s = list.get(i);
		int code = new GoodsService().delete(s.getId());
		if(code==0) {
			JOptionPane.showMessageDialog(null, "delete success");
			return;
		}else {
			JOptionPane.showMessageDialog(null,Util.errors.get(code) );
		}
		quaryAll();
	}
	
}

提交表单页面:

package com.stock.java;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class FromFjame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_3;
	private JComboBox comboBox;
	private JComboBox comboBox_1;
	private JComboBox comboBox_2;

	public FromFjame() {
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 314, 436);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel label = new JLabel("add");
		label.setFont(new Font("宋体", Font.PLAIN, 17));
		label.setBounds(118, 20, 78, 39);
		contentPane.add(label);
		
		JLabel label_1 = new JLabel("name");
		label_1.setBounds(23, 71, 40, 15);
		contentPane.add(label_1);
		
		textField = new JTextField();
		textField.setBounds(87, 68, 155, 21);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel label_2 = new JLabel("quantity");
		label_2.setBounds(23, 128, 40, 15);
		contentPane.add(label_2);
		
		textField_1 = new JTextField();
		textField_1.setBounds(87, 125, 155, 21);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("time");
		lblNewLabel.setBounds(23, 191, 32, 15);
		contentPane.add(lblNewLabel);
		
		JLabel label_3 = new JLabel("price");
		label_3.setBounds(23, 251, 32, 15);
		contentPane.add(label_3);
		
		textField_3 = new JTextField();
		textField_3.setBounds(87, 248, 155, 21);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		comboBox = new JComboBox();
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				addDay();
			}
		});
		comboBox.setBounds(87, 188, 54, 21);
		contentPane.add(comboBox);
		
		comboBox_1 = new JComboBox();
		comboBox_1.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				addDay();
			}
		});
		comboBox_1.setBounds(151, 188, 47, 21);
		contentPane.add(comboBox_1);
		
		comboBox_2 = new JComboBox();
		comboBox_2.setBounds(202, 188, 40, 21);
		contentPane.add(comboBox_2);
		
		//选择框添加内容
		addBirth();
		
		JButton button = new JButton("add");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if(GoodsFrame.goo==null) {
					add();
				}
			}
		});
		button.setBounds(37, 325, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("back");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//每次返回清空信息
				GoodsFrame.goo=null;
				//退出
				dispose();
			}
		});
		button_1.setBounds(169, 325, 93, 23);
		contentPane.add(button_1);
		
		//当点击的行数的信息不为空时,进行下面操作
		if(GoodsFrame.goo!=null) {
			textField.setText(GoodsFrame.goo.getGood_name());
			textField_1.setText(GoodsFrame.goo.getQuantity()+"");
			textField_3.setText(GoodsFrame.goo.getPrice()+"");
			LocalDate birth =GoodsFrame.goo.getTime();
			comboBox.setSelectedItem(birth.getYear());
			comboBox_1.setSelectedItem(birth.getMonthValue());
			comboBox_2.setSelectedItem(birth.getDayOfMonth());
			button.setText("update");
		}
	}

	//选择框填充内容
	private void addBirth() {
		int year=LocalDate.now().getYear();
		for(int i=1970;i<=year;i++) {
			comboBox.addItem(i);
		}
		for(int i=1;i<=12;i++) {
			comboBox_1.addItem(i);
		}
	}
	
	private void addDay() {
		int year=1970;
		int month=1;
		int day=0;
	    Object oYear = comboBox.getSelectedItem();
		Object oMonth =comboBox_1.getSelectedItem();
		if(oYear==null||oMonth==null) {
			return;
		}else {
			year=(int) oYear;
			month=(int) oMonth;
		}
		boolean flag=(year%4==0&&year%100!=0)||year%400==0;
		switch(month) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				day=31;
				break;
			case 2:
				day=flag?28:29;
				break;
			default:
				day=30;
				break;
		}
		comboBox_2.removeAllItems();
		for(int i=1;i<=day;i++) {
			comboBox_2.addItem(i);
		}
	}
	
	//增加
	private void add() {
		String goods_name=textField.getText();
		String quantity=textField_1.getText();
		String price=textField_3.getText();
		int year=(int)comboBox.getSelectedItem();
		int month=(int) comboBox_1.getSelectedItem();
		int day=(int) comboBox_2.getSelectedItem();
		Goods s=new Goods(goods_name,quantity,Double.parseDouble(price),LocalDate.of(year, month, day));
		int insert = new GoodsService().insert(s);		
		if(insert==0) {
			JOptionPane.showMessageDialog(null, "add success");
			textField.setText("");
			textField_1.setText("");
			textField_3.setText("");
			return;
		}else {
			JOptionPane.showMessageDialog(null, Util.errors.get(insert));
		}
	}
	
}

写在最后

如果运行代码中遇到问题,或者需要完整源码和报告,可以加博主V交流:Code2Life2

  • 7
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

anmu4200

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值