diff --git a/Supermarket/src/com/lingnan/supermarket/view/InView.java b/Supermarket/src/com/lingnan/supermarket/view/InView.java index 3a2e30d..5fadc90 100644 --- a/Supermarket/src/com/lingnan/supermarket/view/InView.java +++ b/Supermarket/src/com/lingnan/supermarket/view/InView.java @@ -43,193 +43,298 @@ import com.lingnan.supermarket.utils.SendQQMailUtil; import com.lingnan.supermarket.utils.TimeAndOrder; import com.lingnan.supermarket.view.ProdCatalogView.MyItemListener; +// 定义一个名为InView的类,继承自JPanel,并实现ActionListener接口 public class InView extends JPanel implements ActionListener{ - - - //上面 + + + // 定义顶部工具栏面板 private JPanel toolBarPanel; - + + // 定义搜索面板 private JPanel searchPanel; + // 定义标签 private JLabel nameLabel,locationLabel; + // 定义文本框,用于商品名称搜索 private JTextField nameSearchTF; + // 定义按钮,包括搜索、库存、退出等 private JButton searchBtn,StockBtn,exitBtn; - + + // 定义操作面板 private JPanel opePanel; + // 定义操作按钮,包括添加、更新、删除、历史、返回、详情等 private JButton addBtn,updateBtn,deleteBtn,historyBtn,backBtn,detailBtn; - - //中间 + + // 定义中间部分的滚动面板和表格 private JScrollPane tableScrollPane; private JTable inTable; - - //下面 + + // 定义底部面板及其左右部分 private JPanel bottomPanel,bottomPanelLeft,bottomPanelRight; + // 定义显示记录数的标签 private JLabel countInfoLabel,countInfoLabel2; - + + // 定义缓冲区对象和其实现类 private Buffer Buffer; private BufferImpl BufferImpl; - + + + + // 定义一个静态的向量,用于存储Production对象 private static Vector v = new Vector(); - + + // 定义一个下拉框,用于选择状态 private JComboBox combo; + // 定义状态数组,包含不同的货物状态 private String[] status ={"全部","已入库","待入库","已取消"}; + // 定义商品目录ID private int catalog; - + + // 定义一个JFrame对象,用于引用主窗口 private JFrame jFrame; + // 定义一个User对象,用于存储当前用户信息 private User user; - + + // 定义一个表格模型,用于进货表格 private InTableModel inTableModel ; - + + // 实例化缓冲区实现类 private BufferImpl bufferImpl = new BufferImpl(); - + + // 定义一个标记,用于区分是从进货表还是提醒过来的表 private int mark;/*标记从提醒那里来1是进货表,0是提醒过来的表*/ - + + // 定义一个进货订单服务实现类 private inOrderServiceImpl inOrderImpl; - + + // 定义一个浮点数,用于存储总价 private Float allPrice; + // 定义一个整数,用于存储行号 private int row; + // 定义一个字符串,用于存储用户名 private String uname; - + + // InView类的构造方法,接收主窗口、用户、货物向量、标记作为参数 public InView(JFrame jFrame,User user,Vector v,int mark) { - this.setLayout(new BorderLayout()); + // 设置布局管理器为边界布局 + this.setLayout(new BorderLayout()); + // 保存传入的JFrame引用 this.jFrame = jFrame; + // 保存传入的用户对象 this.user = user; - //获得进货缓冲区的保存的货物并删除缓冲区 + // 获取进货缓冲区保存的货物并删除缓冲区中的数据 this.v =bufferImpl.allInBuffer(); bufferImpl.DelAllInBuffer(); - + + // 保存传入的标记 this.mark=mark; + // 打印标记值 System.out.println("mark="+mark); + // 获取用户名 uname = user.getUsername(); + // 初始化视图 initView(); - - + + } - + + // 初始化视图的方法 private void initView() { - - toolBarPanel = new JPanel(new BorderLayout()); - - searchPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); + + // 创建工具栏面板,并设置布局管理器为边界布局 + toolBarPanel = new JPanel(new BorderLayout()); + + // 创建搜索面板,并设置布局管理器为流布局,对齐方式为右对齐 + searchPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); + // 创建并初始化订单号标签 nameLabel = new JLabel("订单号"); + // 创建文本框,用于输入订单号,宽度为20 nameSearchTF = new JTextField(20); + // 创建搜索按钮,并设置图标 searchBtn = new JButton(new ImageIcon("static\\icon\\search.png")); + // 为搜索按钮添加动作监听器 searchBtn.addActionListener(this); + // 创建并初始化位置标签 locationLabel=new JLabel("当前位置>进货系统"); + // 设置位置标签的字体 locationLabel.setFont(new FontUtil().userFont); + // 设置位置标签的前景色 locationLabel.setForeground(new Color(18, 150, 219)); - - - + + + + + + // 创建下拉框,并使用状态数组初始化 combo = new JComboBox(status); +// 为下拉框添加项目监听器 combo.addItemListener(new MyItemListener()); - - - - opePanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); + + + +// 创建操作面板,并设置布局管理器为流布局,对齐方式为左对齐 + opePanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); +// 创建添加按钮,并设置图标 addBtn =new JButton(new ImageIcon("static\\icon\\add.png")); +// 创建更新按钮,并设置图标 updateBtn =new JButton(new ImageIcon("static\\icon\\change.png")); +// 创建删除按钮,并设置图标 deleteBtn =new JButton(new ImageIcon("static\\icon\\delete.png")); +// 创建历史按钮,并设置图标 historyBtn =new JButton(new ImageIcon("static\\icon\\history.png")); +// 创建返回按钮,并设置图标 backBtn =new JButton(new ImageIcon("static\\icon\\back.png")); +// 创建详情按钮,并设置图标 detailBtn = new JButton(new ImageIcon("static\\icon\\detail.png")); - + +// 为添加按钮添加动作监听器 addBtn.addActionListener(this); +// 为更新按钮添加动作监听器 updateBtn.addActionListener(this); +// 为删除按钮添加动作监听器 deleteBtn.addActionListener(this); +// 为历史按钮添加动作监听器 historyBtn.addActionListener(this); +// 为返回按钮添加动作监听器 backBtn.addActionListener(this); +// 为详情按钮添加动作监听器 detailBtn.addActionListener(this); - - backBtn.setVisible(false);/*在记录页面显示出来*/ - detailBtn.setVisible(false);/*在订单详情页显示出来*/ - + +// 设置返回按钮为不可见,它在记录页面会显示出来 + backBtn.setVisible(false); +// 设置详情按钮为不可见,它在订单详情页会显示出来 + detailBtn.setVisible(false); + +// 将添加按钮添加到操作面板 opePanel.add(addBtn); +// 将返回按钮添加到操作面板 opePanel.add(backBtn); +// 将详情按钮添加到操作面板 opePanel.add(detailBtn); - opePanel.add(updateBtn); +// 将更新按钮添加到操作面板 + opePanel.add(updateBtn); +// 将删除按钮添加到操作面板 opePanel.add(deleteBtn); +// 将历史按钮添加到操作面板 opePanel.add(historyBtn); - - + +// 将位置标签添加到搜索面板 searchPanel.add(locationLabel); +// 将订单号标签添加到搜索面板 searchPanel.add(nameLabel); +// 将订单号文本框添加到搜索面板 searchPanel.add(nameSearchTF); +// 将搜索按钮添加到搜索面板 searchPanel.add(searchBtn); +// 将下拉框添加到搜索面板 searchPanel.add(combo); - - - + + + +// 将搜索面板添加到工具栏面板的西边 toolBarPanel.add(searchPanel,"West"); +// 将操作面板添加到工具栏面板的东边 toolBarPanel.add(opePanel,"East"); - - - //中间表 + + +// 初始化中间表格模型,使用货物向量v inTableModel = new InTableModel(v); + + // 创建表格,使用中间表格模型 inTable = new JTable(inTableModel); +// 设置表格字体 inTable.setFont(FontUtil.tableFont); +// 设置表格行高 inTable.setRowHeight(50); +// 创建滚动面板,并将表格添加到其中 tableScrollPane = new JScrollPane(inTable); - + +// 获取表格模型中的总价格 allPrice = inTableModel.getAllPrice(); +// 获取表格模型中的行数 row = inTableModel.getRowCount(); - - //下面 +// 创建底部左侧面板,并设置布局管理器为流布局,对齐方式为右对齐 bottomPanelLeft = new JPanel(new FlowLayout(FlowLayout.RIGHT)); - countInfoLabel = new JLabel("商品种类:"+row+",总价:"+allPrice); +// 创建并初始化显示商品种类和总价的标签 + countInfoLabel = new JLabel("商品种类:"+row+",总价:"+allPrice); +// 将标签添加到左侧面板,指定对齐方式为左对齐 bottomPanelLeft.add(countInfoLabel,"Left"); - + +// 创建底部右侧面板,并设置布局管理器为流布局,对齐方式为左对齐 bottomPanelRight = new JPanel(new FlowLayout(FlowLayout.LEFT)); +// 创建结账按钮,并设置图标 StockBtn =new JButton(new ImageIcon("static\\icon\\stock.png"));/*结账按钮*/ +// 创建退出按钮,并设置图标 exitBtn =new JButton(new ImageIcon("static\\icon\\exit.png"));/*退出按钮*/ +// 为结账按钮添加动作监听器 StockBtn.addActionListener(this); +// 为退出按钮添加动作监听器 exitBtn.addActionListener(this); +// 将结账按钮添加到右侧面板 bottomPanelRight.add(StockBtn); +// 将退出按钮添加到右侧面板 bottomPanelRight.add(exitBtn); - + +// 创建底部面板,并设置布局管理器为边界布局 bottomPanel = new JPanel(new BorderLayout()); +// 将右侧面板添加到底部面板的东边 bottomPanel.add(bottomPanelRight,"East"); +// 将左侧面板添加到底部面板的西边 bottomPanel.add(bottomPanelLeft,"West"); - - + + +// 将工具栏面板添加到窗口的北边 this.add(toolBarPanel,"North"); +// 将滚动面板(含表格)添加到窗口的中间 this.add(tableScrollPane,"Center");/*将表格放到中间*/ +// 将底部面板添加到窗口的南边 this.add(bottomPanel,"South"); - + +// 如果标记为1,表示从提醒页面过来,刷新缓冲区 if(mark==1) /*判断是不是从提醒那里过来的*/{ refreshBuffer(v); } - +// 如果标记为0,执行进货订单记录操作 else if(mark==0) { InOrderRecord(); } - - - + + + + + + +// 设置窗口为可见 setVisible(true); - - - - - + + + + + + } - + + // 提供一个静态方法,用于获取存储Production对象的向量 public static Vector getVector(){ return v; } - - + + // 定义一个内部类,实现ItemListener接口,用于监听下拉框的项目状态改变事件 public class MyItemListener implements ItemListener { + // 重写itemStateChanged方法,当下拉框中的项目状态改变时被调用 @Override public void itemStateChanged(ItemEvent e) { + // 将事件源强制转换为JComboBox JComboBox cb = (JComboBox) e.getSource(); + // 获取下拉框中选中的项目,并转换为字符串 String catalog1 = (String) cb.getSelectedItem(); + // 根据选中的项目设置商品目录ID if(catalog1.equals("全部")) catalog=0; else if(catalog1.equals("已入库")) @@ -238,276 +343,424 @@ public class InView extends JPanel implements ActionListener{ catalog=2; else if(catalog1.equals("已取消")) catalog=3; - + + // 根据商品目录ID执行查找状态的结果处理 resultOfFindStatus(catalog); - } + + + } - - - //按钮组件隐藏 + } + + } + + + + + // 设置按钮组件的可见性,用于订单视图 public void OrderView() { + // 显示返回按钮 backBtn.setVisible(true); + // 显示详情按钮 detailBtn.setVisible(true); + // 显示更新按钮 updateBtn.setVisible(true); + // 显示删除按钮 deleteBtn.setVisible(true); + // 隐藏添加按钮 addBtn.setVisible(false); + // 隐藏历史按钮 historyBtn.setVisible(false); } - - + + // 根据订单编号处理结果 public void resultOfNumber(String iNumber) { + // 重置标记 this.mark=0; + // 创建订单表格模型 InOrderTM inOrderTM = new InOrderTM(); + // 根据订单编号查询结果 inOrderTM.resultOfNumber(iNumber); + // 更新表格模型 inTable.setModel(inOrderTM); + // 清空底部左侧面板 bottomPanelLeft.removeAll(); + // 创建并初始化显示记录数的标签 countInfoLabel = new JLabel("共"+inOrderTM.getRowCount()+"条记录"); + // 将标签添加到底部左侧面板 bottomPanelLeft.add(countInfoLabel); + // 调用OrderView设置按钮可见性 OrderView(); - } - + } + + // 根据状态处理查询结果 public void resultOfFindStatus(int catalog) { + // 重置标记 this.mark=0; + // 创建订单表格模型 InOrderTM inOrderTM = new InOrderTM(); + // 根据状态查询结果 inOrderTM.resultOfFind(catalog); + // 更新表格模型 inTable.setModel(inOrderTM); + // 清空底部左侧面板 bottomPanelLeft.removeAll(); + // 创建并初始化显示记录数的标签 countInfoLabel = new JLabel("共"+inOrderTM.getRowCount()+"条记录"); + // 将标签添加到底部左侧面板 bottomPanelLeft.add(countInfoLabel); + // 调用OrderView设置按钮可见性 OrderView(); } - - /*刷新*/ - public void refreshBuffer(Vector v) { - this.mark=1; - + // 刷新缓冲区,更新表格数据 + public void refreshBuffer(Vector v) { + // 设置标记 + this.mark=1; + // 创建商品表格模型 InTableModel inTableModel = new InTableModel(v); + // 更新表格模型 inTable.setModel(inTableModel); + // 清空底部左侧面板 bottomPanelLeft.removeAll(); + // 创建并初始化显示商品种类和总价的标签 countInfoLabel = new JLabel("商品种类:"+inTableModel.getRowCount()+",总价:"+inTableModel.getAllPrice()); + // 将标签添加到底部左侧面板 bottomPanelLeft.add(countInfoLabel); + // 隐藏返回按钮 backBtn.setVisible(false); + // 隐藏详情按钮 detailBtn.setVisible(false); + // 显示历史按钮 historyBtn.setVisible(true); + // 显示更新按钮 updateBtn.setVisible(true); + // 显示添加按钮 addBtn.setVisible(true); + // 显示删除按钮 deleteBtn.setVisible(true); - - allPrice = inTableModel.getAllPrice(); + + // 更新总价和行数 + allPrice = inTableModel.getAllPrice(); row = inTableModel.getRowCount(); } - - /*调出进货订单表*/ + + + + + + /* 调出进货订单表 */ public void InOrderRecord() { + // 设置标记为0 this.mark=0; + // 创建进货订单表格模型 InOrderTM inOrderTM = new InOrderTM(); + // 获取所有进货订单记录 inOrderTM.allInOrderRecord(); + // 更新表格模型 inTable.setModel(inOrderTM); + // 清空底部左侧面板 bottomPanelLeft.removeAll(); + // 创建并初始化显示记录数的标签 countInfoLabel = new JLabel("共"+inOrderTM.getRowCount()+"条记录"); + // 将标签添加到底部左侧面板 bottomPanelLeft.add(countInfoLabel); + // 调用OrderView设置按钮可见性 OrderView(); - } - - /*调出进货订单表*/ + + /* 根据订单号调出进货订单记录 */ public void InRecord(String iNumber) { + // 设置标记为2 this.mark=2; + // 创建进货记录表格模型,并传入订单号 InRecordTM inRecordTM = new InRecordTM(iNumber); + // 根据订单号查询进货记录 inRecordTM.findInRecordByINumber(); + // 更新表格模型 inTable.setModel(inRecordTM); + // 清空底部左侧面板 bottomPanelLeft.removeAll(); + // 创建并初始化显示订单号和记录数的标签 countInfoLabel = new JLabel("订单号@"+iNumber+"共有"+inRecordTM.getRowCount()+"条记录"); + // 将标签添加到底部左侧面板 bottomPanelLeft.add(countInfoLabel); + // 设置按钮可见性 backBtn.setVisible(true); detailBtn.setVisible(false); updateBtn.setVisible(false); addBtn.setVisible(false); historyBtn.setVisible(false); deleteBtn.setVisible(false); - + + + } - - - - - - /*按钮监听时间*/ - @Override - public void actionPerformed(ActionEvent e) { - BufferImpl = new BufferImpl();/*获得购物车*/ + + +} + + + + + +/* 按钮监听事件 */ +@Override +public void actionPerformed(ActionEvent e) { + // 实例化购物车 + BufferImpl = new BufferImpl();/* 获得购物车 */ + // 获取事件源 Object source = e.getSource(); - + + // 如果事件源是搜索按钮 if(searchBtn==source) { - String number = nameSearchTF.getText(); - System.out.println("搜索后的订单:"+number); - resultOfNumber(number); + // 获取搜索框中的文本 + String number = nameSearchTF.getText(); + // 打印搜索后的订单号 + System.out.println("搜索后的订单:"+number); + // 处理搜索结果 + resultOfNumber(number); } - else if(addBtn==source) {/*添加*/ - InDialog outDialog = new InDialog(jFrame,v,user); - outDialog.setVisible(true); - v=outDialog.getVector(); - refreshBuffer(v); - + // 如果事件源是添加按钮 + else if(addBtn==source) {/* 添加 */ + // 创建并显示添加对话框 + InDialog outDialog = new InDialog(jFrame,v,user); + outDialog.setVisible(true); + // 获取对话框中的向量并更新 + v=outDialog.getVector(); + // 刷新购物车 + refreshBuffer(v); + + } + + + // 如果事件源是更新按钮 + else if(updateBtn==source) {/* 更新 */ + // 打印当前标记 + System.out.println("mark="+mark); + // 获取选中行的索引 + int rowIndex = inTable.getSelectedRow(); + // 如果没有选中行,弹出提示 + if(rowIndex==-1) { + JOptionPane.showMessageDialog(this,"请选中一条进行更改数量"); + return; } - - - else if(updateBtn==source) {/*更新*/ - System.out.println("mark="+mark); - int rowIndex = inTable.getSelectedRow(); - if(rowIndex==-1) { - JOptionPane.showMessageDialog(this,"请选中一条进行更改数量"); - return; - } - //进货表修改 - if(mark==1) { - String id =(String) inTable.getValueAt(rowIndex,0); - ChangeSumDialog changesumDialog = new ChangeSumDialog(jFrame,id,"In",v); - changesumDialog.setVisible(true); - v = changesumDialog.getVector(); - System.out.println("更改状态后v.size="+v.size()); - refreshBuffer(v); - } - - //inOrder修改,修改状态 - else if(mark==0) { - String iNumber =(String) inTable.getValueAt(rowIndex,0); - String status =(String) inTable.getValueAt(rowIndex,4); - if(status.equals("已入库")) { - JOptionPane.showMessageDialog(this,"订单上的货物已入库无法修改状态","提示",JOptionPane.INFORMATION_MESSAGE); - return; - } - - ChangeStatusDialog changeStatusDialog = new ChangeStatusDialog(jFrame,iNumber,status); - changeStatusDialog.setVisible(true); - MainView.refreshRemind(); - HomeView.refreshHome(); - InOrderRecord(); - } - + // 如果标记为1,表示进货表修改 + if(mark==1) { + // 获取选中行的ID + String id =(String) inTable.getValueAt(rowIndex,0); + // 创建并显示更改数量对话框 + ChangeSumDialog changesumDialog = new ChangeSumDialog(jFrame,id,"In",v); + changesumDialog.setVisible(true); + // 获取对话框中的向量并更新 + v = changesumDialog.getVector(); + // 打印更改状态后的向量大小 + System.out.println("更改状态后v.size="+v.size()); + // 刷新购物车 + refreshBuffer(v); } - - + + // 如果标记为0,表示inOrder修改,修改状态 + else if(mark==0) { + // 获取选中行的订单号和状态 + String iNumber =(String) inTable.getValueAt(rowIndex,0); + String status =(String) inTable.getValueAt(rowIndex,4); + // 如果状态为已入库,弹出提示 + if(status.equals("已入库")) { + JOptionPane.showMessageDialog(this,"订单上的货物已入库无法修改状态","提示",JOptionPane.INFORMATION_MESSAGE); + return; + } + + // 创建并显示更改状态对话框 + ChangeStatusDialog changeStatusDialog = new ChangeStatusDialog(jFrame,iNumber,status); + changeStatusDialog.setVisible(true); + // 刷新提醒和首页视图 + MainView.refreshRemind(); + HomeView.refreshHome(); + // 重新加载进货订单记录 + InOrderRecord(); + } + + } + + // 如果事件源是删除按钮 else if(deleteBtn==source) { - int rowIndex = inTable.getSelectedRow(); - if(rowIndex==-1) { - JOptionPane.showMessageDialog(this,"请选中一条"); - return; - } - - /*删除进货表的*/ - if(mark==1) { - System.out.println("删除进货表"); - String id =(String) inTable.getValueAt(rowIndex,0); - int select = JOptionPane.showConfirmDialog(this,"是否删除id为"+id+"的记录","提示",JOptionPane.YES_NO_OPTION); - if(select==JOptionPane.YES_OPTION) {/*选择是*/ - for(int i =0;i(); - refreshBuffer(v); - MainView.refreshRemind(); - JOptionPane.showConfirmDialog(null,"发送邮件成功\r\n订单号:"+s[0]+"\r\n负责人:"+uname, "提示", JOptionPane.YES_OPTION); - - } - - - } - + // 刷新购物车 + refreshBuffer(v); + + + + + + + // 如果购物车为空 + if(v.size()==0)/*购物车为空*/{ + // 显示提示信息 + JOptionPane.showMessageDialog(null,"您的进货页面为空", "提示", JOptionPane.YES_OPTION); + } +// 如果购物车不为空 + else {/*购物车不为空*/ + // 显示确认对话框,包含进价总金额、负责人等信息 + int res = JOptionPane.showConfirmDialog(null,"进价总金额:"+allPrice+"元\r\n负责人:"+uname+"\r\n发送邮件至 re@qq.com", "提交订单", JOptionPane.YES_NO_OPTION); + // 如果用户选择是,即已经结账 + if(res==JOptionPane.YES_OPTION)/*如果已经结账*/{ + + // 获取时间和订单号,s[0]为订单号,s[1]为时间 + String[] s =TimeAndOrder.TimeAndOrder(uname); + + // 往订单表插入一条记录 + inOrderServiceImpl inOrderImpl = new inOrderServiceImpl(); + inOrderImpl.InsertInOrder(s[0], allPrice, s[1],uname ,2); + + // 往inRecord表添加数据 + inRecordServiceImpl inRecordImpl = new inRecordServiceImpl(); + for(Production p:v) {/*往inRecord表添加数据*/ + inRecordImpl.insertInRecord(s[0], p); + } + + + // 生成订单文本 + CreateOrder createOrder = new CreateOrder(); + String OrderText = createOrder.CreateOrder(v, s[0], s[1], allPrice,uname); + try {/*发送邮件*/ + SendQQMailUtil QQEmail = new SendQQMailUtil("cwfeng5@qq.com","wlcinslohrgpdiac","1912638153@qq.com","@新民超市进货需求申请",OrderText); + } catch (MessagingException e1) { + // 打印异常堆栈信息 + e1.printStackTrace(); + } + // 清空购物车 + v=new Vector(); + // 刷新购物车显示 + refreshBuffer(v); + // 刷新主视图提醒 + MainView.refreshRemind(); + // 显示发送邮件成功提示 + JOptionPane.showConfirmDialog(null,"发送邮件成功\r\n订单号:"+s[0]+"\r\n负责人:"+uname, "提示", JOptionPane.YES_OPTION); + + + } + +// 如果事件源是退出按钮 }else if(exitBtn==source) { - int res = JOptionPane.showConfirmDialog(null,"确定退出并清空购物车吗", "结账", JOptionPane.YES_NO_OPTION); - if(res==JOptionPane.YES_OPTION)/*如果退出*/{ - v=new Vector();/*将数组置空*/ - refreshBuffer(v); - JOptionPane.showConfirmDialog(null,"退出成功", "提示", JOptionPane.PLAIN_MESSAGE); - } - } + // 显示确认对话框,询问是否退出并清空购物车 + int res = JOptionPane.showConfirmDialog(null,"确定退出并清空购物车吗", "结账", JOptionPane.YES_NO_OPTION); + // 如果用户选择是,即退出 + if(res==JOptionPane.YES_OPTION)/*如果退出*/{ + // 将购物车数组置空 + v=new Vector();/*将数组置空*/ + // 刷新购物车显示 + refreshBuffer(v); + // 显示退出成功提示 + JOptionPane.showConfirmDialog(null,"退出成功", "提示", JOptionPane.PLAIN_MESSAGE); + + + } + + } + + } + + } diff --git a/Supermarket/src/com/lingnan/supermarket/view/ProdCatalogView.java b/Supermarket/src/com/lingnan/supermarket/view/ProdCatalogView.java index 5eef1f0..42871db 100644 --- a/Supermarket/src/com/lingnan/supermarket/view/ProdCatalogView.java +++ b/Supermarket/src/com/lingnan/supermarket/view/ProdCatalogView.java @@ -1,146 +1,185 @@ -package com.lingnan.supermarket.view; - -import java.awt.BorderLayout; -import java.awt.Color; -import java.awt.FlowLayout; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.ItemEvent; -import java.awt.event.ItemListener; -import java.util.ArrayList; - -import javax.swing.ImageIcon; -import javax.swing.JButton; -import javax.swing.JComboBox; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JTable; -import javax.swing.JTextField; - -import com.lingnan.supermarket.dao.impl.prodCatalogImpl; -import com.lingnan.supermarket.dialog.UserDialog; -import com.lingnan.supermarket.dto.ProdCatalog; -import com.lingnan.supermarket.dto.Production; -import com.lingnan.supermarket.dto.User; -import com.lingnan.supermarket.table.ProdCatalogTM; -import com.lingnan.supermarket.table.StorageTableModel; -import com.lingnan.supermarket.table.UserTableModel; -import com.lingnan.supermarket.utils.FontUtil; - -public class ProdCatalogView extends JPanel { +package com.lingnan.supermarket.view; // 定义包名,包含视图相关的类 + +import java.awt.BorderLayout; // 导入用于窗口布局的BorderLayout类 +import java.awt.Color; // 导入用于颜色处理的Color类 +import java.awt.FlowLayout; // 导入用于面板布局的FlowLayout类 +import java.awt.event.ActionEvent; // 导入用于处理动作事件的ActionEvent类 +import java.awt.event.ActionListener; // 导入用于监听动作事件的ActionListener接口 +import java.awt.event.ItemEvent; // 导入用于处理项目事件的ItemEvent类 +import java.awt.event.ItemListener; // 导入用于监听项目事件的ItemListener接口 +import java.util.ArrayList; // 导入用于数组列表的ArrayList类 + +import javax.swing.ImageIcon; // 导入用于处理图像图标的ImageIcon类 +import javax.swing.JButton; // 导入用于创建按钮的JButton类 +import javax.swing.JComboBox; // 导入用于创建组合框的JComboBox类 +import javax.swing.JFrame; // 导入用于创建窗口的JFrame类 +import javax.swing.JLabel; // 导入用于创建标签的JLabel类 +import javax.swing.JPanel; // 导入用于创建面板的JPanel类 +import javax.swing.JScrollPane; // 导入用于创建滚动窗格的JScrollPane类 +import javax.swing.JTable; // 导入用于创建表格的JTable类 +import javax.swing.JTextField; // 导入用于创建文本字段的JTextField类 + +import com.lingnan.supermarket.dao.impl.prodCatalogImpl; // 导入产品目录数据访问层的实现类 +import com.lingnan.supermarket.dialog.UserDialog; // 导入用户对话框类 +import com.lingnan.supermarket.dto.ProdCatalog; // 导入产品目录数据传输对象 +import com.lingnan.supermarket.dto.Production; // 导入产品数据传输对象 +import com.lingnan.supermarket.dto.User; // 导入用户数据传输对象 +import com.lingnan.supermarket.table.ProdCatalogTM; // 导入产品目录表格模型 +import com.lingnan.supermarket.table.StorageTableModel; // 导入存储表格模型 +import com.lingnan.supermarket.table.UserTableModel; // 导入用户表格模型 +import com.lingnan.supermarket.utils.FontUtil; // 导入字体工具类 + +public class ProdCatalogView extends JPanel { // 创建一个公共类ProdCatalogView,继承自JPanel // 上面 - private JPanel toolBarPanel; + private JPanel toolBarPanel; // 工具栏面板 - private JPanel searchPanel; - private JLabel logLabel, locationLabel; - private JTextField nameSearchTF; - private JButton searchBtn; + private JPanel searchPanel; // 搜索面板 + private JLabel logLabel, locationLabel; // 日志标签和位置标签 + private JTextField nameSearchTF; // 名称搜索文本字段 + private JButton searchBtn; // 搜索按钮 - private JPanel opePanel; - private JButton addBtn, updateBtn, deleteBtn; + private JPanel opePanel; // 操作面板 + private JButton addBtn, updateBtn, deleteBtn; // 添加、更新、删除按钮 - private String catalog = "0"; - private JComboBox combo; - private String log[]=null; - private ArrayListalog=null; - private ProdCatalogTM prodCatalogTM; - private ProdCatalog pc; - private prodCatalogImpl pci; + private String catalog = "0"; // 目录字符串,默认为"0" + private JComboBox combo; // 字符串组合框 + private String log[]=null; // 日志数组 + private ArrayListalog=null; // 日志数组列表 + private ProdCatalogTM prodCatalogTM; // 产品目录表格模型 + private ProdCatalog pc; // 产品目录对象 + private prodCatalogImpl pci; // 产品目录数据访问层实现对象 // 中间 - private JScrollPane tableScrollPane; - private JTable prodCatalogTable; + private JScrollPane tableScrollPane; // 表格滚动窗格 + private JTable prodCatalogTable; // 产品目录表格 // 下面 - private JPanel bottomPanel; - private JLabel countInfoLabel; + private JPanel bottomPanel; // 底部面板 + private JLabel countInfoLabel; // 记录数信息标签 - private JFrame jFrame; + private JFrame jFrame; // 窗口对象 - public ProdCatalogView(JFrame jFrame) { - this.setLayout(new BorderLayout()); - initView(); - this.jFrame = jFrame; + public ProdCatalogView(JFrame jFrame) { // ProdCatalogView构造方法,接收一个JFrame参数 + this.setLayout(new BorderLayout()); // 设置面板布局为BorderLayout + initView(); // 初始化视图 + this.jFrame = jFrame; // 将传入的JFrame对象赋值给成员变量jFrame } - + private void initView() { + // 创建一个面板,使用边界布局管理器 toolBarPanel = new JPanel(new BorderLayout()); + // 创建搜索面板,使用流布局管理器,对齐方式为左对齐 searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); + // 创建并初始化商品目录标签 locationLabel = new JLabel("商品目录"); + // 设置标签字体 locationLabel.setFont(new FontUtil().userFont); + // 设置标签前景色 locationLabel.setForeground(new Color(18, 150, 219)); + // 创建分类标签 logLabel = new JLabel("分类"); + // 创建文本框,用于输入搜索内容,指定宽度为10 nameSearchTF = new JTextField(10); + // 创建搜索按钮,并指定图标 searchBtn = new JButton("搜索", new ImageIcon("static\\icon\\search.png")); + // 创建操作面板,使用流布局管理器,对齐方式为右对齐 // opePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); +// // 创建并初始化按钮,指定图标 // addBtn = new JButton(new ImageIcon("static\\icon\\add.png")); // updateBtn = new JButton(new ImageIcon("static\\icon\\update.png")); // deleteBtn = new JButton(new ImageIcon("static\\icon\\delete.png")); - +// +// // 为按钮添加事件监听器 // addBtn.addActionListener(this); // updateBtn.addActionListener(this); // deleteBtn.addActionListener(this); - +// +// // 将按钮添加到操作面板 // opePanel.add(addBtn); // opePanel.add(updateBtn); // opePanel.add(deleteBtn); - + + // 实例化商品目录实现类 pci=new prodCatalogImpl(); + // 查找所有商品目录名称 this.alog=pci.findNameProdCatalog(); + // 创建字符串数组,用于存储商品目录名称 this.log=new String[alog.size()]; + // 将商品目录名称填充到数组 for(int i=0;i(log); + // 为下拉列表添加项目监听器 combo.addItemListener(new MyItemListener()); + // 将标签、下拉列表添加到搜索面板 searchPanel.add(locationLabel); searchPanel.add(logLabel); searchPanel.add(combo); /* - * searchPanel.add(nameSearchTF); searchPanel.add(searchBtn); + * 将文本框和搜索按钮添加到搜索面板,当前被注释 + * searchPanel.add(nameSearchTF); + * searchPanel.add(searchBtn); */ + // 将搜索面板添加到工具栏面板的西边 toolBarPanel.add(searchPanel, "West"); + // 将操作面板添加到工具栏面板的东边,当前被注释 // toolBarPanel.add(opePanel, "East"); - // 中间表格 + // 创建表格模型,并获取所有数据 prodCatalogTM = new ProdCatalogTM(); prodCatalogTM.all(); + // 创建表格,使用表格模型 prodCatalogTable = new JTable(prodCatalogTM); + // 设置表格字体 prodCatalogTable.setFont(FontUtil.tableFont); + // 设置表格行高 prodCatalogTable.setRowHeight(50); + // 创建滚动面板,并将表格添加到其中 tableScrollPane = new JScrollPane(prodCatalogTable); + } - // 下面 - bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); - countInfoLabel = new JLabel("总共" + prodCatalogTable.getRowCount() + "条"); - bottomPanel.add(countInfoLabel); - - this.add(toolBarPanel, "North"); - this.add(tableScrollPane, "Center");/* 将表格放到中间 */ - this.add(bottomPanel, "South"); - setVisible(true); - } + // 下面 + // 创建底部面板,使用流布局管理器,对齐方式为左对齐 + bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); +// 创建并初始化显示总记录数的标签 + countInfoLabel = new JLabel("总共" + prodCatalogTable.getRowCount() + "条"); +// 将记录数标签添加到底部面板 +bottomPanel.add(countInfoLabel); + +// 将工具栏面板添加到当前窗口的北部 +this.add(toolBarPanel, "North"); +// 将表格滚动面板添加到当前窗口的中间 +this.add(tableScrollPane, "Center");/* 将表格放到中间 */ +// 将底部面板添加到当前窗口的南部 +this.add(bottomPanel, "South"); + + // 设置当前窗口为可见 + setVisible(true); +} + +// 以下是注释掉的代码,实现按钮的事件监听 // @Override // public void actionPerformed(ActionEvent e) { // Object source = e.getSource(); @@ -148,47 +187,73 @@ public class ProdCatalogView extends JPanel { // UserDialog userDialog = new UserDialog(jFrame); // userDialog.setVisible(true); // } else if (updateBtn == source) { -// +// // 更新按钮的逻辑 // } else if (deleteBtn == source) { -// +// // 删除按钮的逻辑 // } // } - public class MyItemListener implements ItemListener { - - @Override - public void itemStateChanged(ItemEvent e) { - JComboBox cb = (JComboBox) e.getSource(); - String catalog1 = (String) cb.getSelectedItem(); - pci =new prodCatalogImpl(); - for(int i=0;i商品库存"); - locationLabel.setFont(new FontUtil().userFont); - locationLabel.setForeground(new Color(18, 150, 219)); - nameLabel = new JLabel("商品名"); - nameSearchTF = new JTextField(10); - - searchBtn = new JButton(new ImageIcon("static\\icon\\search.png")); - - opePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); - addBtn =new JButton(new ImageIcon("static\\icon\\add.png")); - updateBtn =new JButton(new ImageIcon("static\\icon\\update.png")); - deleteBtn =new JButton(new ImageIcon("static\\icon\\delete.png")); - backBtn =new JButton(new ImageIcon("static\\icon\\back.png")); - historyBtn =new JButton(new ImageIcon("static\\icon\\history.png")); - backBtn.setVisible(false); - - addBtn.addActionListener(this); - updateBtn.addActionListener(this); - deleteBtn.addActionListener(this); - searchBtn.addActionListener(this); - backBtn.addActionListener(this); - historyBtn.addActionListener(this); - - opePanel.add(addBtn); - opePanel.add(updateBtn); - opePanel.add(deleteBtn); - opePanel.add(backBtn); - opePanel.add(historyBtn); - - searchPanel.add(locationLabel); - searchPanel.add(nameLabel); - searchPanel.add(nameSearchTF); - searchPanel.add(searchBtn); - - - toolBarPanel.add(searchPanel,"West"); - toolBarPanel.add(opePanel,"East"); - - + + private void initView() { // 初始化视图方法 + + toolBarPanel = new JPanel(new BorderLayout()); // 创建工具栏面板并设置布局为BorderLayout + + searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 创建搜索面板并设置布局为左对齐的FlowLayout + locationLabel=new JLabel("当前位置>商品库存"); // 创建当前位置标签并设置文本 + locationLabel.setFont(new FontUtil().userFont); // 设置标签字体 + locationLabel.setForeground(new Color(18, 150, 219)); // 设置标签前景色 + nameLabel = new JLabel("商品名"); // 创建商品名标签 + nameSearchTF = new JTextField(10); // 创建文本框用于输入商品名,大小为10 + + + + + searchBtn = new JButton(new ImageIcon("static\\icon\\search.png")); // 创建搜索按钮并设置图标 + + opePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); // 创建操作面板并设置布局为右对齐的FlowLayout + addBtn =new JButton(new ImageIcon("static\\icon\\add.png")); // 创建添加按钮并设置图标 + updateBtn =new JButton(new ImageIcon("static\\icon\\update.png")); // 创建更新按钮并设置图标 + deleteBtn =new JButton(new ImageIcon("static\\icon\\delete.png")); // 创建删除按钮并设置图标 + backBtn =new JButton(new ImageIcon("static\\icon\\back.png")); // 创建返回按钮并设置图标 + historyBtn =new JButton(new ImageIcon("static\\icon\\history.png")); // 创建历史按钮并设置图标 + backBtn.setVisible(false); // 设置返回按钮不可见 + + addBtn.addActionListener(this); // 为添加按钮添加事件监听器 + updateBtn.addActionListener(this); // 为更新按钮添加事件监听器 + deleteBtn.addActionListener(this); // 为删除按钮添加事件监听器 + searchBtn.addActionListener(this); // 为搜索按钮添加事件监听器 + backBtn.addActionListener(this); // 为返回按钮添加事件监听器 + historyBtn.addActionListener(this); // 为历史按钮添加事件监听器 + + opePanel.add(addBtn); // 将添加按钮添加到操作面板 + opePanel.add(updateBtn); // 将更新按钮添加到操作面板 + opePanel.add(deleteBtn); // 将删除按钮添加到操作面板 + opePanel.add(backBtn); // 将返回按钮添加到操作面板 + opePanel.add(historyBtn); // 将历史按钮添加到操作面板 + + searchPanel.add(locationLabel); // 将位置标签添加到搜索面板 + searchPanel.add(nameLabel); // 将商品名标签添加到搜索面板 + searchPanel.add(nameSearchTF); // 将商品名文本框添加到搜索面板 + searchPanel.add(searchBtn); // 将搜索按钮添加到搜索面板 + + + toolBarPanel.add(searchPanel,"West"); // 将搜索面板添加到工具栏面板的西边 + toolBarPanel.add(opePanel,"East"); // 将操作面板添加到工具栏面板的东边 + + //中间表格 - storageTableModel = new StorageTableModel(); - storageTableModel.all(); - storageTable = new JTable(storageTableModel); - storageTable.setFont(FontUtil.tableFont); - storageTable.setRowHeight(50); - - tableScrollPane = new JScrollPane(storageTable); - + storageTableModel = new StorageTableModel(); // 创建存储表格模型 + storageTableModel.all(); // 初始化表格模型数据 + storageTable = new JTable(storageTableModel); // 创建表格并设置模型 + storageTable.setFont(FontUtil.tableFont); // 设置表格字体 + storageTable.setRowHeight(50); // 设置表格行高 + + tableScrollPane = new JScrollPane(storageTable); // 创建滚动面板并包含表格 + //下面 - bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); - countInfoLabel = new JLabel("总共"+storageTableModel.getRowCount()+"条"); - bottomPanel.add(countInfoLabel); - - - this.add(toolBarPanel,"North"); - this.add(tableScrollPane,"Center");/*将表格放到中间*/ - this.add(bottomPanel,"South"); - - setVisible(true); + bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 创建底部面板并设置布局为左对齐的FlowLayout + countInfoLabel = new JLabel("总共"+storageTableModel.getRowCount()+"条"); // 创建记录数标签并设置文本 + bottomPanel.add(countInfoLabel); // 将记录数标签添加到底部面板 + + + this.add(toolBarPanel,"North"); // 将工具栏面板添加到当前面板的北边 + this.add(tableScrollPane,"Center"); /*将表格放到中间*/ + this.add(bottomPanel,"South"); // 将底部面板添加到当前面板的南边 + + setVisible(true); // 设置当前面板可见 } + + + + @Override - public void actionPerformed(ActionEvent e) { - Object source = e.getSource(); - if(addBtn==source) { + public void actionPerformed(ActionEvent e) { // 重写actionPerformed方法以处理按钮点击事件 + Object source = e.getSource(); // 获取事件源 + if(addBtn==source) { // 如果事件源是添加按钮 //添加数据的对话框 - ProductionDialog productionDialog = new ProductionDialog(jFrame); - productionDialog.setVisible(true); - refreshProduction(); - }else if(updateBtn==source) { - refreshProduction(); - }else if(historyBtn==source) { - storageRecord(); - }else if(backBtn==source) { - refreshProduction(); + ProductionDialog productionDialog = new ProductionDialog(jFrame); // 创建添加数据的对话框 + productionDialog.setVisible(true); // 显示对话框 + refreshProduction(); // 刷新数据 + }else if(updateBtn==source) { // 如果事件源是更新按钮 + refreshProduction(); // 刷新数据 + }else if(historyBtn==source) { // 如果事件源是历史按钮 + storageRecord(); // 处理存储记录 + }else if(backBtn==source) { // 如果事件源是返回按钮 + refreshProduction(); // 刷新数据 } - - else if(deleteBtn==source) { + + else if(deleteBtn==source) { // 如果事件源是删除按钮 //获取选中记录,删除 - int[] rowIndexs = storageTable.getSelectedRows(); - if(rowIndexs.length==0) { - JOptionPane.showMessageDialog(this,"请至少选中一条"); - return; + int[] rowIndexs = storageTable.getSelectedRows(); // 获取表格中选中的行索引 + if(rowIndexs.length==0) { // 如果没有选中任何行 + JOptionPane.showMessageDialog(this,"请至少选中一条"); // 显示提示信息 + return; // 结束方法执行 } - int select=JOptionPane.showConfirmDialog(this,"是否删除选中的"+rowIndexs.length+"条记录","提示",JOptionPane.YES_NO_OPTION); - if(select==JOptionPane.YES_OPTION){ - - for(int i=0;i供应商"); - locationLabel.setFont(new FontUtil().userFont); - locationLabel.setForeground(new Color(18, 150, 219)); - nameLabel = new JLabel("公司名称"); - nameSearchTF = new JTextField(10); - - searchBtn = new JButton(new ImageIcon("static\\icon\\search.png")); - - opePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); - addBtn =new JButton(new ImageIcon("static\\icon\\add.png")); - updateBtn =new JButton(new ImageIcon("static\\icon\\update.png")); - deleteBtn =new JButton(new ImageIcon("static\\icon\\delete.png")); - - addBtn.addActionListener(this); - updateBtn.addActionListener(this); - deleteBtn.addActionListener(this); - searchBtn.addActionListener(this); - - opePanel.add(addBtn); - opePanel.add(updateBtn); - opePanel.add(deleteBtn); - - searchPanel.add(locationLabel); - searchPanel.add(nameLabel); - searchPanel.add(nameSearchTF); - searchPanel.add(searchBtn); - - - toolBarPanel.add(searchPanel,"West"); - toolBarPanel.add(opePanel,"East"); - - + + private void initView() { // 初始化视图方法 + + toolBarPanel = new JPanel(new BorderLayout()); // 创建工具栏面板并设置布局为BorderLayout + + searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 创建搜索面板并设置布局为左对齐的FlowLayout + locationLabel=new JLabel("当前位置>供应商"); // 创建当前位置标签 + locationLabel.setFont(new FontUtil().userFont); // 设置标签字体 + locationLabel.setForeground(new Color(18, 150, 219)); // 设置标签前景色 + nameLabel = new JLabel("公司名称"); // 创建公司名称标签 + nameSearchTF = new JTextField(10); // 创建文本框用于输入公司名称,大小为10 + + + + + + searchBtn = new JButton(new ImageIcon("static\\icon\\search.png")); // 创建搜索按钮并设置图标 + + opePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); // 创建操作面板并设置布局为右对齐的FlowLayout + addBtn =new JButton(new ImageIcon("static\\icon\\add.png")); // 创建添加按钮并设置图标 + updateBtn =new JButton(new ImageIcon("static\\icon\\update.png")); // 创建更新按钮并设置图标 + deleteBtn =new JButton(new ImageIcon("static\\icon\\delete.png")); // 创建删除按钮并设置图标 + + addBtn.addActionListener(this); // 为添加按钮添加事件监听器 + updateBtn.addActionListener(this); // 为更新按钮添加事件监听器 + deleteBtn.addActionListener(this); // 为删除按钮添加事件监听器 + searchBtn.addActionListener(this); // 为搜索按钮添加事件监听器 + + opePanel.add(addBtn); // 将添加按钮添加到操作面板 + opePanel.add(updateBtn); // 将更新按钮添加到操作面板 + opePanel.add(deleteBtn); // 将删除按钮添加到操作面板 + + searchPanel.add(locationLabel); // 将当前位置标签添加到搜索面板 + searchPanel.add(nameLabel); // 将公司名称标签添加到搜索面板 + searchPanel.add(nameSearchTF); // 将公司名称文本框添加到搜索面板 + searchPanel.add(searchBtn); // 将搜索按钮添加到搜索面板 + + + toolBarPanel.add(searchPanel,"West"); // 将搜索面板添加到工具栏面板的西边 + toolBarPanel.add(opePanel,"East"); // 将操作面板添加到工具栏面板的东边 + + //中间表格 - SupplierTableModel supplierTableModel = new SupplierTableModel(); - supplierTableModel.all(); - supplierTable = new JTable(supplierTableModel); - supplierTable.setFont(FontUtil.tableFont); - supplierTable.setRowHeight(50); - - tableScrollPane = new JScrollPane(supplierTable);/*滑动条*/ - + supplierTableModel = new SupplierTableModel(); // 创建供应商表格模型实例 + supplierTableModel.all(); // 调用表格模型的方法以加载所有数据 + supplierTable = new JTable(supplierTableModel); // 创建表格并使用供应商表格模型 + supplierTable.setFont(FontUtil.tableFont); // 设置表格字体 + supplierTable.setRowHeight(50); // 设置表格行高为50像素 + + tableScrollPane = new JScrollPane(supplierTable); // 创建滚动面板并添加表格/*滑动条*/ + //下面 - bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); - countInfoLabel = new JLabel("总共"+supplierTableModel.getRowCount()+"条"); - bottomPanel.add(countInfoLabel); - - - this.add(toolBarPanel,"North"); - this.add(tableScrollPane,"Center");/*将表格放到中间*/ - this.add(bottomPanel,"South"); - - setVisible(true); + bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 创建底部面板并设置布局为左对齐的FlowLayout + countInfoLabel = new JLabel("总共"+supplierTableModel.getRowCount()+"条"); // 创建信息标签并设置显示总条数 + bottomPanel.add(countInfoLabel); // 将信息标签添加到底部面板 + + + this.add(toolBarPanel,"North"); // 将工具栏面板添加到当前面板的北边 + this.add(tableScrollPane,"Center"); // 将表格滚动面板添加到当前面板的中间/*将表格放到中间*/ + this.add(bottomPanel,"South"); // 将底部面板添加到当前面板的南边 + + setVisible(true); // 设置当前面板可见 } + + + + @Override - public void actionPerformed(ActionEvent e) { - Object source = e.getSource(); - if(addBtn==source) { - SupplierInfDialog supplierInfDialog = new SupplierInfDialog(jFrame, null); - supplierInfDialog.setVisible(true); - refreshSupplier(); - }else if(updateBtn==source) { - refreshSupplier(); - }else if(deleteBtn==source) { + public void actionPerformed(ActionEvent e) { // 重写actionPerformed方法以处理按钮点击事件 + Object source = e.getSource(); // 获取事件源 + if(addBtn==source) { // 如果事件源是添加按钮 + SupplierInfDialog supplierInfDialog = new SupplierInfDialog(jFrame, null); // 创建供应商信息对话框 + supplierInfDialog.setVisible(true); // 显示供应商信息对话框 + refreshSupplier(); // 刷新供应商列表 + }else if(updateBtn==source) { // 如果事件源是更新按钮 + refreshSupplier(); // 刷新供应商列表 + }else if(deleteBtn==source) { // 如果事件源是删除按钮 //获取选中记录 - int rowIndex = supplierTable.getSelectedRow(); - if(rowIndex==-1) { - JOptionPane.showMessageDialog(this,"请选中一条"); - return; - } - int id = (Integer)supplierTable.getValueAt(rowIndex,0); - int select=JOptionPane.showConfirmDialog(this,"是否删除id="+id,"提示",JOptionPane.YES_NO_OPTION); - if(select==JOptionPane.YES_OPTION){ - if(supplierInfService.deleteSupplierInf(id)==1) { - JOptionPane.showMessageDialog(this,"删除成功","提示",JOptionPane.INFORMATION_MESSAGE); - refreshSupplier(); - }else { - JOptionPane.showMessageDialog(this,"删除失败","提示",JOptionPane.ERROR_MESSAGE); + int rowIndex = supplierTable.getSelectedRow(); // 获取表格中选中的行索引 + if(rowIndex==-1) { // 如果没有选中任何行 + JOptionPane.showMessageDialog(this,"请选中一条"); // 显示提示信息 + return; // 结束方法执行 } + int id = (Integer)supplierTable.getValueAt(rowIndex,0); // 获取选中行的ID + int select=JOptionPane.showConfirmDialog(this,"是否删除id="+id,"提示",JOptionPane.YES_NO_OPTION); // 显示确认删除对话框 + if(select==JOptionPane.YES_OPTION){ // 如果用户选择是 + if(supplierInfService.deleteSupplierInf(id)==1) { // 尝试删除供应商信息 + JOptionPane.showMessageDialog(this,"删除成功","提示",JOptionPane.INFORMATION_MESSAGE); // 显示删除成功信息 + refreshSupplier(); // 刷新供应商列表 + }else { + JOptionPane.showMessageDialog(this,"删除失败","提示",JOptionPane.ERROR_MESSAGE); // 显示删除失败信息 + } } - }else{ - System.out.println("搜索"); - refreshFindRname(); + }else{ // 如果事件源不是以上按钮 + System.out.println("搜索"); // 输出搜索信息 + refreshFindRname(); // 刷新搜索结果 } } - public void refreshFindRname() { - String name = nameSearchTF.getText(); - SupplierInf supplierInf =new SupplierInf(); - supplierInf.setName(name); - - - supplierTableModel = new SupplierTableModel(); - supplierTableModel.Byname(supplierInf); - - supplierTable.setModel(supplierTableModel); - - refreshCount(); - + public void refreshFindRname() { // 刷新搜索结果的方法 + String name = nameSearchTF.getText(); // 获取文本框中的搜索名称 + SupplierInf supplierInf =new SupplierInf(); // 创建供应商信息对象 + supplierInf.setName(name); // 设置供应商名称 + + supplierTableModel = new SupplierTableModel(); // 创建供应商表格模型 + supplierTableModel.Byname(supplierInf); // 根据名称过滤供应商信息 + supplierTable.setModel(supplierTableModel); // 更新表格模型 + refreshCount(); // 刷新记录数显示 } - public void refreshSupplier() { - supplierTableModel = new SupplierTableModel(); - supplierTableModel.all(); - supplierTable.setModel(supplierTableModel); - - refreshCount(); + public void refreshSupplier() { // 刷新供应商列表的方法 + supplierTableModel = new SupplierTableModel(); // 创建供应商表格模型 + supplierTableModel.all(); // 加载所有供应商信息 + supplierTable.setModel(supplierTableModel); // 更新表格模型 + refreshCount(); // 刷新记录数显示 } - public void refreshCount(){ - bottomPanel.removeAll(); - countInfoLabel = new JLabel("总共"+supplierTableModel.getRowCount()+"条"); - bottomPanel.add(countInfoLabel); + public void refreshCount(){ // 刷新记录数显示的方法 + bottomPanel.removeAll(); // 清除底部面板上的所有组件 + countInfoLabel = new JLabel("总共"+supplierTableModel.getRowCount()+"条"); // 创建新的记录数标签 + bottomPanel.add(countInfoLabel); // 将记录数标签添加到底部面板 } - -} \ No newline at end of file + +} + + + +