项目描述:一个简单的超市管理系统,能够实现用户登入和注册功能,共分为前台和后台两个主要界面,普通用户界面操作权限收到限制,只能对商品和销售记录进行简单查询操作,后台中可以进行商品的删除、修改、查询,可以查看用户信息,可以修改用户信息。具体的代码如下:
TooLUtil工具类:在该类中定义一些方法,用于判断字符串是否为空、获取当前时间、对时间进行格式转化,同时获取当前用户登入,具体代码如下所示:
package cn.qinlimei.goodmanager.utils;import javax.xml.crypto.Data;import java.text.Format;import java.text.SimpleDateFormat;import java.util.Date;public class ToolUtil { //判断字符串是否为空 public static boolean isEmpty(String str){ if(str!=null&& !"".equals(str.trim())){//字符串两端的空格去掉 return false;//不是空的返回false } return true;//空的话就返回真 } //获取当前系统时间所对应的毫秒值 public static Long getTime(){ long time=System.currentTimeMillis(); return time; } //将一个long类型的毫秒值转换成指定格式的日期字符串 public static String getDataByTime(long time){ SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd"); Date date=new Date(time); String value=format.format(date); return value; }}
LoginFrm类:实现用户登入的类
package cn.qinlimei.goodmanager.jframe;import cn.qinlimei.goodmanager.dao.UserDao;import cn.qinlimei.goodmanager.model.User;import cn.qinlimei.goodmanager.utils.Dbutil;import cn.qinlimei.goodmanager.utils.ToolUtil;import cn.qinlimei.goodmanager.model.Goods;import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.sql.Connection;//用户实现登入的界面public class LoginFrm extends JFrame { public static User currentUser;//当登入成功后,使用该变量存储登入的用户 private JFrame jf;//登入界面的窗体组件 private JTextField userNameText;//输入用户名的文本框 private JTextField passwordText;//输入密码的文本框 private JComboBox comboBox;//用户角色的下拉选择框 //创建一个UserDao对象 private UserDao userDao=new UserDao(); public LoginFrm(){ //初始化窗体组件 jf=new JFrame("超市管理系统"); jf.getContentPane().setFont(new Font("幼圆",Font.BOLD,14)); jf.setBounds(600,250,500,467); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.getContentPane().setLayout(null); //初始化展示用户名三个字所需要的标签 JLabel label; label=new JLabel("用户名:"); label.setForeground(Color.BLACK); label.setFont(new Font("幼圆",Font.BOLD,16)); label.setBounds(110,80,75,40); jf.getContentPane().add(label); //初始化初始化用户名文本框组件 userNameText=new JTextField(); userNameText.setFont(new Font("幼圆",Font.BOLD,14)); userNameText.setForeground(Color.BLACK); userNameText.setBounds(198,83,164,30); jf.getContentPane().add(userNameText); userNameText.setColumns(10); //初始化展示密码两个字所需要的标签 JLabel label_1; label_1=new JLabel("密码:"); label_1.setForeground(Color.BLACK); label_1.setFont(new Font("幼圆",Font.BOLD,16)); label_1.setBounds(120,150,65,40); jf.getContentPane().add(label_1); //初始化密码输入的文本框组件 passwordText=new JTextField(); passwordText.setFont(new Font("幼圆",Font.BOLD,14)); passwordText.setForeground(Color.BLACK); passwordText.setColumns(10); passwordText.setBounds(198,152,164,30); jf.getContentPane().add(passwordText); //初始化展示权限两个字的标签 JLabel label_2; label_2=new JLabel("权限"); label_2.setForeground(Color.BLACK); label_2.setFont(new Font("幼圆",Font.BOLD,15)); label_2.setBounds(144,250,45,29); jf.getContentPane().add(label_2); //初始化角色选择的下拉框组件 comboBox=new JComboBox(); comboBox.setBounds(199,254,127,25); comboBox.addItem("普通成员"); comboBox.addItem("管理员"); jf.getContentPane().add(comboBox); //初始化登入按钮 JButton button=new JButton("登入"); //给登入按钮添加一个动作监听器 button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //编写登入的业务逻辑 checkLogih(); } }); button.setBounds(153,320,65,29); jf.getContentPane().add(button); //初始化注册按钮 JButton button_1=new JButton("注册"); //给注册按钮添加一个动作监听器 button_1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //编写注册的业务逻辑 regUser();//去注册界面的方法 } }); button_1.setBounds(263,320,65,29); jf.getContentPane().add(button_1); //初始化展示 用户登入标题所需要的标签 JLabel lbNewLable_1; lbNewLable_1=new JLabel("用户登入"); lbNewLable_1.setFont(new Font("幼圆",Font.BOLD,25)); lbNewLable_1.setBounds(200,10,122,51); jf.getContentPane().add(lbNewLable_1); //让图形界面显示且不可改变大小 jf.setVisible(true); jf.setResizable(false); } //去注册页面所对应的方法 public void regUser(){ //把当前的jf页面隐藏掉 jf.setVisible(false); new RegFram(); } //完成用户登入的业务逻辑 public void checkLogih(){ //获取用户输入的用户名和密码 String userName= userNameText.getText(); String password= passwordText.getText(); int index=comboBox.getSelectedIndex(); if(ToolUtil.isEmpty(userName)||ToolUtil.isEmpty(password)){ JOptionPane.showMessageDialog(null,"用户名和密码不能为空!"); return; } //把数据封装到User中 User user=new User(); user.setUername(userName); user.setUerpassword(password); if(index==0){ user.setRole(1); }else{ user.setRole(2); } //获取一个链接对象 Connection connection=null; try { connection= Dbutil.getConnection(); User login=userDao.login(connection,user); if(login==null){ JOptionPane.showMessageDialog(null,"登入失败"); }else{ currentUser =login;//记录当前登入的用户 if(index==0){ //等于0说明选择的是普通用户 jf.dispose(); new UserMenuFrm(); //普通用户的登入界面 }else{ jf.dispose(); new AdminMenuFrm(); //管理员的登入界面 } } } catch (Exception e) { e.printStackTrace(); }finally { try { Dbutil.closeConnection(connection); } catch (Exception e) { e.printStackTrace(); } } } public static void main(String[] args) { LoginFrm loginFrm=new LoginFrm(); }}
AdminMenuFrm类:实现部门管理的类
package cn.qinlimei.goodmanager.jframe;import cn.qinlimei.goodmanager.dao.DepartmentDao;import cn.qinlimei.goodmanager.model.Department;import cn.qinlimei.goodmanager.utils.Dbutil;import cn.qinlimei.goodmanager.utils.ToolUtil;import javax.swing.*;import java.awt.*;import java.awt.event.*;import java.sql.Connection;public class AdminMenuFrm extends JFrame { private JFrame jf; private JTextField textField; private JButton btnNewButton; private JTextArea textArea; private DepartmentDao departmentDao=new DepartmentDao(); public AdminMenuFrm(){ //初始化界面所需要的窗体组件 jf=new JFrame("管理员"); jf.setBounds(400,100,600,429); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.getContentPane().setLayout(null); //初始化展示部门类别四个字所对应的标签 JLabel label_1=new JLabel(); label_1.setFont(new Font("幼圆",Font.BOLD,14)); label_1.setText("部门名称:"); label_1.setBounds(112,38,75,26); jf.getContentPane().add(label_1); //初始化输入部门名称的文本框 textField=new JTextField(); textField.setBounds(197,38,241,26); jf.getContentPane().add(textField); //初始化展示部门编号所对应的标签 JLabel label=new JLabel(); label.setFont(new Font("幼圆",Font.BOLD,14)); label.setText("部门编号:"); label.setBounds(112,120,75,26); jf.getContentPane().add(label); //初始化输入部门编号所需要的文本框 textArea=new JTextArea(); textArea.setColumns(10); textArea.setRows(5); textArea.setBackground(Color.WHITE); textArea.setBounds(197,114,241,25); jf.getContentPane().add(textArea); //初始化 添加 按钮 btnNewButton=new JButton("添加"); btnNewButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) {//添加按钮的业务逻辑 //获取部门名称和部门编号 String typeName= textField.getText(); int typeRemrke= Integer.parseInt(textArea.getText()); //判断是否为空 if(ToolUtil.isEmpty(typeName)||ToolUtil.isEmpty(String.valueOf(typeRemrke))){ JOptionPane.showMessageDialog(null,"请输入相关信息"); return; } //把数据封装到部门对象中 Department department=new Department(); department.setDepartmentname(typeName); department.setDepartmentid(typeRemrke); //获取链接对象 Connection connection=null; try { //把数据保存在数据库中 connection=Dbutil.getConnection(); //调用部门管理中的方法进行信息保存 int i=departmentDao.add(connection,department); if(i==1){ JOptionPane.showMessageDialog(null,"添加成功"); reset(); }else if(i==2){ JOptionPane.showMessageDialog(null,"添加失败,已存在!"); }else{ JOptionPane.showMessageDialog(null,"添加失败!"); } } catch (Exception ex) { ex.printStackTrace(); }finally { if(connection !=null){ try {Dbutil.closeConnection(connection); } catch (Exception ex) {ex.printStackTrace(); } } } } }); btnNewButton.setFont(new Font("幼圆",Font.BOLD,14)); btnNewButton.setBounds(182,281,80,26); jf.getContentPane().add(btnNewButton); //创建重置按钮 JButton button=new JButton("重置"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { reset(); } }); button.setFont(new Font("幼圆",Font.BOLD,14)); button.setBounds(360,281,80,26); jf.getContentPane().add(button); //创建菜单栏组件 JMenuBar menuBar=new JMenuBar(); jf.setJMenuBar(menuBar); //添加部门管理菜单 JMenu mnNewMeun=new JMenu("部门管理"); menuBar.add(mnNewMeun); //添加部门 选项 JMenuItem mntmNewMenuItem=new JMenuItem("部门添加"); mnNewMeun.add(mntmNewMenuItem); //添加部门修改选项 JMenuItem mntmNewMenuItem_1=new JMenuItem("部门修改"); mntmNewMenuItem_1.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AddminTypeEdit();//部门修改的界面 } }); mnNewMeun.add(mntmNewMenuItem_1); //添加商品管理菜单 JMenu mnNewMeun_2=new JMenu("商品管理"); menuBar.add(mnNewMeun_2); //添加商品添加选项 JMenuItem mntmNewMenuItem_2=new JMenuItem("添加商品"); mntmNewMenuItem_2.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AdmingoodAdd(); } }); mnNewMeun_2.add(mntmNewMenuItem_2); //添加商品修改 JMenuItem mntmNewMenuItem_3=new JMenuItem("商品修改"); mntmNewMenuItem_3.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AddmingoodEdit(); } }); mnNewMeun_2.add(mntmNewMenuItem_3); //添加用户管理菜单 JMenu menu1=new JMenu("员工管理"); menuBar.add(menu1); //添加用户选项 JMenuItem mntmNewMenuItem_4=new JMenuItem("添加员工"); mntmNewMenuItem_4.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AddminUserinfo(); } }); menu1.add(mntmNewMenuItem_4); //添加流水信息选项 JMenuItem mntmNewMenuItem_5=new JMenuItem("流水信息"); mntmNewMenuItem_5.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new Addminflowinfo(); } }); menu1.add(mntmNewMenuItem_5); //添加退出系统菜单 JMenu mnNewMeun_1=new JMenu("退出系统"); mnNewMeun_1.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { JOptionPane.showMessageDialog(null,"欢迎下次使用!"); jf.dispose(); } }); menuBar.add(mnNewMeun_1); jf.setVisible(true); jf.setResizable(false); } public void reset(){ this.textField.setText(""); this.textArea.setText(""); } public static void main(String[] args) { new AdminMenuFrm(); }}
AdmingoodAdd类:实现商品管理的类:
package cn.qinlimei.goodmanager.jframe;import cn.qinlimei.goodmanager.dao.GoodsDao;import cn.qinlimei.goodmanager.model.Goods;import cn.qinlimei.goodmanager.utils.Dbutil;import cn.qinlimei.goodmanager.utils.ToolUtil;import javax.swing.*;import javax.swing.border.TitledBorder;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.math.BigDecimal;import java.sql.Connection;public class AdmingoodAdd extends Exception { private JFrame jf;//展示添加图书组件的窗体组件 private JTextField textField_1;//输入商品名所需要的文本框 private JTextField textField_2;//输入编号文本框 private JTextField textField_3;//输入条形码文本框 private JTextField textField_4;//输入产地文本框 private JTextField textField_5;//输入产商文本框 private JTextField textField_6;//输入规文本框 private JTextField textField_7;//输入文本框 private JTextField textField_8;//输入售价文本框 private JTextField textField_9;//输入库存文本框 private JTextField textField_10;//库存下限文本框 //创建商品Dao对象 private GoodsDao goodsDao=new GoodsDao(); public AdmingoodAdd() { //初始化商品添加窗口组件 jf = new JFrame("管理员界面"); jf.setBounds(400, 100, 611, 450); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); jf.getContentPane().setLayout(null); //创建一个添加商品信息所对应的面板组件 JPanel panel = new JPanel(); panel.setBorder(new TitledBorder(null, "书籍添加", TitledBorder.LEADING, TitledBorder.TOP, null, Color.black)); panel.setBounds(23, 21, 554, 350); jf.getContentPane().add(panel); panel.setLayout(null); //创建展示商品名所需要的标签 JLabel lblNewLabel=new JLabel("名称:"); lblNewLabel.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel.setBounds(55,31,45,27); panel.add(lblNewLabel); //初始化输入商品名所需的文本框 textField_1=new JTextField(); textField_1.setBounds(101,31,129,27); panel.add(textField_1); textField_1.setColumns(10); //创建展示编号所需要的标签 JLabel lblNewLabel_2=new JLabel("编号:"); lblNewLabel_2.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_2.setBounds(294,31,45,27); panel.add(lblNewLabel_2); //初始化编号文本框 textField_2=new JTextField(); textField_2.setBounds(340,31,128,27); panel.add(textField_2); textField_2.setColumns(10); //创建展示条形码所需要的标签 JLabel lblNewLabel_3=new JLabel("条形码:"); lblNewLabel_3.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_3.setBounds(43,79,60,27); panel.add(lblNewLabel_3); //初始化输入条形码文本框 textField_3=new JTextField(); textField_3.setBounds(101,79,129,27); panel.add(textField_3); textField_3.setColumns(10); //创建展示产地的标签 JLabel lblNewLabel_4=new JLabel("产地:"); lblNewLabel_4.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_4.setBounds(294,79,60,27); panel.add(lblNewLabel_4); //初始化产地文本 textField_4=new JTextField(); textField_4.setBounds(340,79,129,27); panel.add(textField_4); textField_4.setColumns(10); //产商标签 JLabel lblNewLabel_5=new JLabel("产商:"); lblNewLabel_5.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_5.setBounds(55,127,60,27); panel.add(lblNewLabel_5); //初始化产商文本 textField_5=new JTextField(); textField_5.setBounds(101,127,129,27); panel.add(textField_5); textField_5.setColumns(10); //规格标签 JLabel lblNewLabel_6=new JLabel("规格:"); lblNewLabel_6.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_6.setBounds(294,127,60,27); panel.add(lblNewLabel_6); //初始化规格文本 textField_6=new JTextField(); textField_6.setBounds(340,127,129,27); panel.add(textField_6); textField_6.setColumns(10); //进价标签 JLabel lblNewLabel_7=new JLabel("进价:"); lblNewLabel_7.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_7.setBounds(45,175,60,27); panel.add(lblNewLabel_7); //进价文本 textField_7=new JTextField(); textField_7.setBounds(101,175,129,27); panel.add(textField_7); textField_7.setColumns(10); //售价标签 JLabel lblNewLabel_8=new JLabel("售价:"); lblNewLabel_8.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_8.setBounds(294,175,45,27); panel.add(lblNewLabel_8); //售价文本 textField_8=new JTextField(); textField_8.setBounds(340,175,129,27); panel.add(textField_8); textField_8.setColumns(10); //库存标签 JLabel lblNewLabel_9=new JLabel("库存:"); lblNewLabel_9.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_9.setBounds(45,223,45,27); panel.add(lblNewLabel_9); //库存文本 textField_9=new JTextField(); textField_9.setBounds(101,223,129,27); panel.add(textField_9); textField_9.setColumns(10); //库存下限标签 JLabel lblNewLabel_10=new JLabel("下限:"); lblNewLabel_10.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_10.setBounds(294,223,45,27); panel.add(lblNewLabel_10); //库存下限文本 textField_10=new JTextField(); textField_10.setBounds(340,223,129,27); panel.add(textField_10); textField_10.setColumns(10); //查询数据库获取类别数据 //创建添加按钮 JButton btnNewButton=new JButton("添加"); btnNewButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { //添加商品的业务逻辑代码 //从文本框中获取用户输入的数据 String goodname=textField_1.getText(); String goodid=textField_2.getText(); String goodtxm=textField_3.getText(); String goodcd=textField_4.getText(); String goodcs=textField_5.getText(); String goodgg=textField_6.getText(); String goodjj=textField_7.getText(); String goodsj=textField_8.getText(); String goodkc=textField_9.getText(); String goodxx=textField_10.getText(); //对数据进行校验 if(ToolUtil.isEmpty(goodname)|| ToolUtil.isEmpty(goodid)|| ToolUtil.isEmpty(goodtxm)|| ToolUtil.isEmpty(goodcd)|| ToolUtil.isEmpty(goodcs)|| ToolUtil.isEmpty(goodgg)|| ToolUtil.isEmpty(goodjj)|| ToolUtil.isEmpty(goodsj)|| ToolUtil.isEmpty(goodkc)|| ToolUtil.isEmpty(goodxx)){ JOptionPane.showMessageDialog(null,"请输入相关内容!"); return; } //因为从文本框中得到的是字符串类型的数据,无法将整形等数据赋给商品对象中的整形等数据,因此要进行数据解析 Integer bianhao;//编号 Double jinjia;//进价 Double shoujia;//售价 Integer kucun;//库存 Integer kcxiaxian;//库存下限 try { bianhao=Integer.parseInt(goodid); jinjia= new BigDecimal(goodjj).setScale(2).doubleValue();//保留两位小数点 shoujia= new BigDecimal(goodsj).setScale(2).doubleValue(); kucun=Integer.parseInt(goodkc); kcxiaxian=Integer.parseInt(goodxx); }catch (Exception e1){ JOptionPane.showMessageDialog(null,"参数错误!"); return; } //创建一个商品对象用来封装参数 Goods goods=new Goods(); goods.setGoodtxm(goodtxm); goods.setGoodid(bianhao); goods.setGoodname(goodname); goods.setGoodcd(goodcd); goods.setGoodgg(goodgg); goods.setGoodcj(goodcs); goods.setGoodj(jinjia); goods.setGoodsj(shoujia); goods.setGoodxx(kcxiaxian); goods.setGoodkc(kucun); //获取链接对象进行数据保存操作 Connection connection=null; try { connection=Dbutil.getConnection(); //调用商品dao中的方法完成数据保存 int i=goodsDao.add(connection,goods);//会返回一个整形 if(i==1){ JOptionPane.showMessageDialog(null,"添加成功!"); reset();//进行清空操作 }else { JOptionPane.showMessageDialog(null,"添加失败!"); } } catch (Exception ex) { ex.printStackTrace(); }finally { if(connection!=null){ try {Dbutil.closeConnection(connection); } catch (Exception ex) {ex.printStackTrace(); } } } } }); btnNewButton.setFont(new Font("幼圆",Font.BOLD,14)); btnNewButton.setBounds(124,280,77,27); panel.add(btnNewButton); //创建重置按钮 JButton button=new JButton("重置"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { reset(); } }); button.setFont(new Font("幼圆",Font.BOLD,14)); button.setBounds(329,280,77,27); panel.add(button); //创建菜单栏组件 JMenuBar menuBar=new JMenuBar(); jf.setJMenuBar(menuBar); //添加部门管理菜单 JMenu mnNewMenu=new JMenu("部门管理"); menuBar.add(mnNewMenu); //添加 添加部门选项 JMenuItem mntmNewMenuItem=new JMenuItem("部门添加"); mntmNewMenuItem.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AdminMenuFrm(); } }); mnNewMenu.add(mntmNewMenuItem); //添加 部门修改选项 JMenuItem mntmNewMenuItem_1=new JMenuItem("部门修改"); mntmNewMenuItem_1.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AddminTypeEdit(); } }); mnNewMenu.add(mntmNewMenuItem_1); //商品管理菜单 JMenu mnNewMenu_2=new JMenu("商品管理"); menuBar.add(mnNewMenu_2); //商品添加 选项 JMenuItem mntmNewMenuItem_2=new JMenuItem("商品添加"); mnNewMenu_2.add(mntmNewMenuItem_2); //商品修改 选项 JMenuItem mntmNewMenuItem_3=new JMenuItem("商品修改"); mntmNewMenuItem_3.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AddmingoodEdit();//商品修改的界面 } }); mnNewMenu_2.add(mntmNewMenuItem_3); //员工管理菜单 JMenu menu1=new JMenu("员工管理"); menuBar.add(menu1); //添加员工 选项 JMenuItem mntnNewMenuItem_4=new JMenuItem("员工信息"); mntnNewMenuItem_4.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AddminUserinfo();//进入员工管理的界面 } }); menu1.add(mntnNewMenuItem_4); //添加流水信息菜单选项 JMenuItem mntnNewMenuItem_5=new JMenuItem("流水信息"); mntnNewMenuItem_5.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new Addminflowinfo(); } }); menu1.add(mntnNewMenuItem_5); //添加退出系统菜单 JMenu mnNewMenu_1=new JMenu("退出系统"); mnNewMenu_1.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { JOptionPane.showMessageDialog(null,"欢迎下次使用!"); jf.dispose(); } }); menuBar.add(mnNewMenu_1); //展示jf,并且不可改变大小 jf.setVisible(true); jf.setResizable(false); } public void reset(){ //清空所有文本框中的内容 textField_1.setText(""); textField_2.setText(""); textField_3.setText(""); textField_4.setText(""); textField_5.setText(""); textField_6.setText(""); textField_7.setText(""); textField_8.setText(""); textField_9.setText(""); textField_10.setText(""); } public static void main(String[] args) { new AdmingoodAdd(); }}
AdminUserinfo类:实现员工管理的类:
package cn.qinlimei.goodmanager.jframe;import cn.qinlimei.goodmanager.dao.StaffDao;import cn.qinlimei.goodmanager.dao.UserDao;import cn.qinlimei.goodmanager.model.Goods;import cn.qinlimei.goodmanager.model.UserStaff;import cn.qinlimei.goodmanager.utils.Dbutil;import cn.qinlimei.goodmanager.utils.ToolUtil;import javax.swing.*;import javax.swing.border.TitledBorder;import javax.swing.table.DefaultTableModel;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.sql.Connection;import java.sql.ResultSet;import java.util.Date;import java.util.Vector;import static cn.qinlimei.goodmanager.utils.ToolUtil.getDataByTime;public class AddminUserinfo { private JFrame jf;//展示用户管理界面的窗体组件 private JTable table;//展示员工信息的表格组件 private DefaultTableModel model;//封装部门信息所需要的表格数据模型组件 private JTextField textField;//输入工号所需要的文本框组件 private JTextField textField_1;//输入ID所需要的文本框 private JTextField textField_2;//输入名文本框 private JTextField textField_3;//输入sex文本框 private JTextField textField_4;//输入身份证文本框 private JTextField textField_5;//输入birth文本框 private JTextField textField_6;//输入phone本框 private JTextField textField_7;//输入did文本框 private StaffDao staffDao=new StaffDao(); public AddminUserinfo(){ jf=new JFrame("管理员界面"); jf.setBounds(400,50,600,680); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JMenuBar menuBar=new JMenuBar(); jf.setJMenuBar(menuBar); //添加部门管理菜单 JMenu mnNewMenu=new JMenu("部门管理"); menuBar.add(mnNewMenu); //添加 添加部门选项 JMenuItem mntmNewMenuItem=new JMenuItem("部门添加"); mntmNewMenuItem.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AdminMenuFrm(); } }); mnNewMenu.add(mntmNewMenuItem); //添加 部门修改选项 JMenuItem mntmNewMenuItem_1=new JMenuItem("部门修改"); mntmNewMenuItem_1.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AddminTypeEdit(); } }); mnNewMenu.add(mntmNewMenuItem_1); //商品管理菜单 JMenu mnNewMenu_2=new JMenu("商品管理"); menuBar.add(mnNewMenu_2); //商品添加 选项 JMenuItem mntmNewMenuItem_2=new JMenuItem("商品添加"); mntmNewMenuItem_2.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new AdmingoodAdd();//商品添加的界面 } }); mnNewMenu_2.add(mntmNewMenuItem_2); //商品修改 选项 JMenuItem mntmNewMenuItem_3=new JMenuItem("商品修改"); mnNewMenu_2.add(mntmNewMenuItem_3); //员工管理菜单 JMenu menu1=new JMenu("员工管理"); menuBar.add(menu1); //添加员工 选项 JMenuItem mntnNewMenuItem_4=new JMenuItem("员工信息"); menu1.add(mntnNewMenuItem_4); //添加流水信息菜单选项 JMenuItem mntnNewMenuItem_5=new JMenuItem("流水信息"); mntnNewMenuItem_5.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { jf.dispose(); new Addminflowinfo(); } }); menu1.add(mntnNewMenuItem_5); //添加退出系统菜单 JMenu mnNewMenu_1=new JMenu("退出系统"); mnNewMenu_1.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { JOptionPane.showMessageDialog(null,"欢迎下次使用!"); jf.dispose(); } }); menuBar.add(mnNewMenu_1); //创建查询的操作面板 JPanel panel = new JPanel(); panel.setBorder(new TitledBorder(null, "员工查询", TitledBorder.LEADING, TitledBorder.TOP, null, Color.black)); panel.setBounds(20, 10, 541, 78); jf.getContentPane().add(panel); panel.setLayout(null); jf.getContentPane().setLayout(null); //初始化查询字段所对应的 JLabel lblNewLabel=new JLabel("工号:"); lblNewLabel.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel.setBounds(125,31,45,27); panel.add(lblNewLabel); //初始化输入查询关键字所对应的文本框 textField=new JTextField(); textField.setBounds(185,28,146,24); panel.add(textField); textField.setColumns(10); //创建查询按钮 JButton button=new JButton("查询"); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String staffid=textField.getText(); int gonghao=Integer.parseInt(staffid); UserStaff userStaff=new UserStaff(); userStaff.setStaffid(gonghao); putDtates(userStaff); } }); button.setFont(new Font("幼圆",Font.BOLD,14)); button.setBounds(352,28,81,25); panel.add(button); //创建员工查询的面板 JPanel panel_1 = new JPanel(); panel_1.setBorder(new TitledBorder(null, "员工信息", TitledBorder.LEADING, TitledBorder.TOP, null, Color.black)); panel_1.setBounds(20, 105, 541, 195); panel_1.setLayout(null); //展示商品信息所对应的表头数据 String[]title={"工号","姓名","性别","身份证","出生日期","电话","部门"}; //展示商品信息所需要的二维数组 String[][]dates={}; //初始化展示商品信息所对应的表格数据模型 model=new DefaultTableModel(dates,title); //初始化表格组件 table=new JTable(model); //查询数据库获取的所有员工信息 putDtates(new UserStaff()); table.addMouseListener(new MouseAdapter() { @Override public void mousePressed(MouseEvent evt) { typeTableMousePressed(); } }); //添加到面板中 panel_1.setLayout(null); JScrollPane jscrollPane=new JScrollPane(); jscrollPane.setBounds(20,22,496,154); jscrollPane.setViewportView(table); panel_1.add(jscrollPane); jf.getContentPane().add(panel_1); //创建编辑商品信息所对应的面板 JPanel panel_2 = new JPanel(); panel_2.setBorder(new TitledBorder(null, "员工编辑", TitledBorder.LEADING, TitledBorder.TOP, null, Color.black)); panel_2.setBounds(20, 310, 541, 292); jf.getContentPane().add(panel_2); panel_2.setLayout(null); //创建展示商品名所需要的标签 JLabel lblNewLabel_1=new JLabel("姓名:"); lblNewLabel_1.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_1.setBounds(55,31,45,27); panel_2.add(lblNewLabel_1); //初始化输入商品名所需的文本框 textField_1=new JTextField(); textField_1.setBounds(101,31,129,27); panel_2.add(textField_1); textField_1.setColumns(10); //创建展示编号所需要的标签 JLabel lblNewLabel_2=new JLabel("工号:"); lblNewLabel_2.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_2.setBounds(294,31,45,27); panel_2.add(lblNewLabel_2); //初始化编号文本框 textField_2=new JTextField(); textField_2.setBounds(340,31,128,27); panel_2.add(textField_2); textField_2.setColumns(10); //创建展示条形码所需要的标签 JLabel lblNewLabel_3=new JLabel("性别:"); lblNewLabel_3.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_3.setBounds(54,79,60,27); panel_2.add(lblNewLabel_3); //初始化输入条形码文本框 textField_3=new JTextField(); textField_3.setBounds(101,79,129,27); panel_2.add(textField_3); textField_3.setColumns(10); //创建展示产地的标签 JLabel lblNewLabel_4=new JLabel("身份证:"); lblNewLabel_4.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_4.setBounds(277,79,60,27); panel_2.add(lblNewLabel_4); //初始化产地文本 textField_4=new JTextField(); textField_4.setBounds(340,79,129,27); panel_2.add(textField_4); textField_4.setColumns(10); //产商标签 JLabel lblNewLabel_5=new JLabel("出生日期:"); lblNewLabel_5.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_5.setBounds(23,127,75,27); panel_2.add(lblNewLabel_5); //初始化产商文本 textField_5=new JTextField(); textField_5.setBounds(101,127,129,27); panel_2.add(textField_5); textField_5.setColumns(10); //规格标签 JLabel lblNewLabel_6=new JLabel("电话:"); lblNewLabel_6.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_6.setBounds(289,127,60,27); panel_2.add(lblNewLabel_6); //初始化规格文本 textField_6=new JTextField(); textField_6.setBounds(340,127,129,27); panel_2.add(textField_6); textField_6.setColumns(10); //进价标签 JLabel lblNewLabel_7=new JLabel("部门:"); lblNewLabel_7.setFont(new Font("幼圆",Font.BOLD,14)); lblNewLabel_7.setBounds(55,175,60,27); panel_2.add(lblNewLabel_7); //进价文本 textField_7=new JTextField(); textField_7.setBounds(101,175,129,27); panel_2.add(textField_7); textField_7.setColumns(10); JButton btnNewButton_1=new JButton("修改"); btnNewButton_1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String staffid=textField_1.getText(); String staffname=textField_2.getText(); String staffsex=textField_3.getText(); String staffsfz=textField_4.getText(); String staffbirth=textField_5.getText(); String staffphone=textField_6.getText(); String staffdid=textField_7.getText(); if(ToolUtil.isEmpty(staffid)|| ToolUtil.isEmpty(staffname)|| ToolUtil.isEmpty(staffsex)|| ToolUtil.isEmpty(staffsfz)|| ToolUtil.isEmpty(staffbirth)|| ToolUtil.isEmpty(staffphone)|| ToolUtil.isEmpty(staffdid)){ JOptionPane.showMessageDialog(null,"请输入相关内容!"); return; } //封装到userstaff UserStaff userStaff=new UserStaff(); userStaff.setStaffid(Integer.parseInt(staffid)); userStaff.setStaffname(staffname); userStaff.setStaffsex(staffsex); userStaff.setStaffsfz(staffsfz); userStaff.setStaffbirthday(new Date(staffbirth)); userStaff.setStaffphone(staffphone); userStaff.setStaffdid(Integer.parseInt(staffdid)); //获取链接 Connection connection=null; try { connection=Dbutil.getConnection(); //调用修改函数 int i=staffDao.update(connection,userStaff); if(i==1){ JOptionPane.showMessageDialog(null,"修改成功!"); putDtates(new UserStaff()); }else{ JOptionPane.showMessageDialog(null,"修改失败!"); } } catch (Exception ex) { ex.printStackTrace(); }finally { if(connection!=null){ try {Dbutil.closeConnection(connection); } catch (Exception ex) {ex.printStackTrace(); } } } } }); btnNewButton_1.setBounds(230,240,93,25); btnNewButton_1.setFont(new Font("幼圆",Font.BOLD,14)); panel_2.add(btnNewButton_1); //展示jf,并且不可改变大小 jf.setVisible(true); jf.setResizable(false); } public void typeTableMousePressed(){ //获取用户所选择的行索引 int row=table.getSelectedRow(); //拿到书的编号 //拿这行所对应的数据 index表示第几行 0表示第一列 1表示第二列 textField_1.setText(table.getValueAt(row,0).toString()); textField_2.setText(table.getValueAt(row,1).toString()); textField_3.setText(table.getValueAt(row,2).toString()); textField_4.setText(table.getValueAt(row,3).toString()); textField_5.setText(table.getValueAt(row,4).toString()); textField_6.setText(table.getValueAt(row,5).toString()); textField_7.setText(table.getValueAt(row,6).toString()); } public void putDtates(UserStaff staff){ //将获得的对应数据给数据模型对象 DefaultTableModel model=(DefaultTableModel) table.getModel(); model.setRowCount(0);//设置总的行数 //获取链接对象 Connection connection=null; try { connection= Dbutil.getConnection(); //调用UserDao中的方法进行查询 ResultSet resultSet=staffDao.list(connection,staff); //如果有数据的话 while (resultSet.next()){ Vector rowData=new Vector(); //拿数据 rowData.add(resultSet.getInt("staff_id"));//编码1 rowData.add(resultSet.getString("staff_name"));//商品名称2 rowData.add(resultSet.getString("staff_sex"));//条形码3 rowData.add(resultSet.getString("staff_sfz"));//产地4 rowData.add(resultSet.getDate("staff_birthday"));//产商5 rowData.add(resultSet.getString("staff_phone"));//规格型号6 rowData.add(resultSet.getInt("did"));//库存7 //将行数据添加到数据模型中 model.addRow(rowData); } } catch (Exception e) { e.printStackTrace(); }finally { if(connection!=null){ try { Dbutil.closeConnection(connection); } catch (Exception e) { e.printStackTrace(); } } } } public static void main(String[] args) { new AddminUserinfo(); }}
实现结果如下:
来源地址:https://blog.csdn.net/MayMarch/article/details/128046201