From 7fe44258db18919d222754faff55e08c121ef85e Mon Sep 17 00:00:00 2001 From: Redmi Date: Thu, 12 Dec 2024 00:07:50 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=80=E6=AC=A1=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/lingnan/supermarket/view/OutView.java | 486 +++++++++--------- 1 file changed, 230 insertions(+), 256 deletions(-) diff --git a/Supermarket/src/com/lingnan/supermarket/view/OutView.java b/Supermarket/src/com/lingnan/supermarket/view/OutView.java index 01a2ddb..a1979f2 100644 --- a/Supermarket/src/com/lingnan/supermarket/view/OutView.java +++ b/Supermarket/src/com/lingnan/supermarket/view/OutView.java @@ -16,323 +16,297 @@ import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.JTextField; -import javax.swing.text.html.HTMLDocument.Iterator; import com.lingnan.supermarket.dao.impl.BufferImpl; import com.lingnan.supermarket.dialog.ChangeSumDialog; import com.lingnan.supermarket.dialog.OutDialog; -import com.lingnan.supermarket.dialog.UserDialog; import com.lingnan.supermarket.dto.Buffer; import com.lingnan.supermarket.dto.User; import com.lingnan.supermarket.table.*; import com.lingnan.supermarket.utils.FontUtil; import com.lingnan.supermarket.utils.TimeAndOrder; -public class OutView extends JPanel implements ActionListener{ - - +public class OutView extends JPanel implements ActionListener { + + //上面 private JPanel toolBarPanel; - + private JPanel searchPanel; - private JLabel nameLabel,locationLabel; + private JLabel nameLabel, locationLabel; private JTextField nameSearchTF; - private JButton searchBtn,AccountBtn,exitBtn; - + private JButton searchBtn, AccountBtn, exitBtn; + private JPanel opePanel; - private JButton addBtn,updateBtn,deleteBtn,historyBtn,backBtn,detailBtn; - + private JButton addBtn, updateBtn, deleteBtn, historyBtn, backBtn, detailBtn; + //中间 private JScrollPane tableScrollPane; private JTable outTable; - + //下面 - private JPanel bottomPanel,bottomPanelLeft,bottomPanelRight; - private JLabel countInfoLabel,countInfoLabel2; - + private JPanel bottomPanel, bottomPanelLeft, bottomPanelRight; + private JLabel countInfoLabel, countInfoLabel2; + private Buffer Buffer; private BufferImpl BufferImpl; - + private Vector v; - - + + private JFrame jFrame; private User user; - + private OutTableModel outTableModel = new OutTableModel(); - + private int mark;/*标记订单表和订单详情表*/ - - public OutView(JFrame jFrame,User user) { - this.setLayout(new BorderLayout()); - initView(); - this.jFrame = jFrame; - this.user = user; + + public OutView(JFrame jFrame, User user) { + this.setLayout(new BorderLayout()); // 设置主布局为BorderLayout + initView(); // 初始化界面组件 + this.jFrame = jFrame; // 将传入的JFrame赋值给成员变量 + this.user = user; // 将传入的User赋值给成员变量 } - + private void initView() { - - toolBarPanel = new JPanel(new BorderLayout()); - - searchPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); - nameLabel = new JLabel("订单号"); - 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)); - - 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")); - backBtn.setVisible(false);/*在记录页面显示出来*/ - detailBtn.setVisible(false);/*在订单详情页显示出来*/ - - - addBtn.addActionListener(this); - updateBtn.addActionListener(this); - deleteBtn.addActionListener(this); - historyBtn.addActionListener(this); - backBtn.addActionListener(this); - detailBtn.addActionListener(this); - - opePanel.add(addBtn); + + toolBarPanel = new JPanel(new BorderLayout()); // 创建顶部工具栏面板,设置为BorderLayout布局 + + searchPanel = new JPanel(new FlowLayout(FlowLayout.CENTER)); // 创建搜索面板,设置为居中对齐的FlowLayout布局 + nameLabel = new JLabel("订单号"); // 创建“订单号”标签 + nameSearchTF = new JTextField(20); // 创建订单号输入框,长度为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)); // 设置标签字体颜色 + + opePanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 创建操作按钮面板,设置为左对齐的FlowLayout布局 + 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")); // 创建查看详情按钮并设置图标 + backBtn.setVisible(false); // 初始时隐藏返回按钮,记录页面显示 + detailBtn.setVisible(false); // 初始时隐藏查看详情按钮,订单详情页显示 + + addBtn.addActionListener(this); // 为添加按钮添加点击事件监听 + updateBtn.addActionListener(this); // 为更新按钮添加点击事件监听 + deleteBtn.addActionListener(this); // 为删除按钮添加点击事件监听 + historyBtn.addActionListener(this); // 为历史记录按钮添加点击事件监听 + backBtn.addActionListener(this); // 为返回按钮添加点击事件监听 + detailBtn.addActionListener(this); // 为查看详情按钮添加点击事件监听 + + // 将各按钮添加到操作面板 + opePanel.add(addBtn); opePanel.add(backBtn); opePanel.add(detailBtn); opePanel.add(updateBtn); opePanel.add(deleteBtn); opePanel.add(historyBtn); - + + // 将搜索相关组件添加到搜索面板 searchPanel.add(locationLabel); searchPanel.add(nameLabel); searchPanel.add(nameSearchTF); searchPanel.add(searchBtn); - - - toolBarPanel.add(searchPanel,"West"); - toolBarPanel.add(opePanel,"East"); - - - //中间表格 - outTableModel = new OutTableModel(); - outTableModel.allOutBuffer();/*查找所有购物车*/ - outTable = new JTable(outTableModel); - outTable.setFont(FontUtil.tableFont); - outTable.setRowHeight(50); - tableScrollPane = new JScrollPane(outTable); - - //下面 - bottomPanelLeft = new JPanel(new FlowLayout(FlowLayout.LEFT)); - countInfoLabel = new JLabel("商品种类:"+outTableModel.getRowCount()+",总价:"+outTableModel.getAllPrice()); - bottomPanelLeft.add(countInfoLabel); - - bottomPanelRight = new JPanel(new FlowLayout(FlowLayout.RIGHT)); - AccountBtn =new JButton(new ImageIcon("static\\icon\\Account.png"));/*结账按钮*/ - exitBtn =new JButton(new ImageIcon("static\\icon\\exit.png"));/*退出按钮*/ - AccountBtn.addActionListener(this); - exitBtn.addActionListener(this); - bottomPanelRight.add(AccountBtn); - bottomPanelRight.add(exitBtn); - + + // 将搜索面板和操作面板分别添加到工具栏的两侧 + toolBarPanel.add(searchPanel, "West"); + toolBarPanel.add(opePanel, "East"); + + // 中间表格区域 + outTableModel = new OutTableModel(); // 创建订单表格数据模型 + outTableModel.allOutBuffer(); // 查询所有购物车数据 + outTable = new JTable(outTableModel); // 创建表格并设置数据模型 + outTable.setFont(FontUtil.tableFont); // 设置表格字体样式 + outTable.setRowHeight(50); // 设置表格行高 + tableScrollPane = new JScrollPane(outTable); // 创建带滚动条的表格 + + // 底部左侧面板,用于显示统计信息 + bottomPanelLeft = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 设置为左对齐的FlowLayout布局 + countInfoLabel = new JLabel("商品种类:" + outTableModel.getRowCount() + ",总价:" + outTableModel.getAllPrice()); // 显示商品种类和总价 + bottomPanelLeft.add(countInfoLabel); // 将统计信息标签添加到左侧面板 + + // 底部右侧面板,用于显示操作按钮 + bottomPanelRight = new JPanel(new FlowLayout(FlowLayout.RIGHT)); // 设置为右对齐的FlowLayout布局 + AccountBtn = new JButton(new ImageIcon("static\\icon\\Account.png")); // 创建结账按钮并设置图标 + exitBtn = new JButton(new ImageIcon("static\\icon\\exit.png")); // 创建退出按钮并设置图标 + AccountBtn.addActionListener(this); // 为结账按钮添加点击事件监听 + exitBtn.addActionListener(this); // 为退出按钮添加点击事件监听 + bottomPanelRight.add(AccountBtn); // 将结账按钮添加到底部右侧面板 + 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");*/ - this.add(bottomPanel,"South"); - - setVisible(true); + bottomPanel.add(bottomPanelRight, "East"); // 将右侧面板添加到底部面板的右侧 + bottomPanel.add(bottomPanelLeft, "West"); // 将左侧面板添加到底部面板的左侧 + + // 将工具栏、表格和底部面板依次添加到主面板 + this.add(toolBarPanel, "North"); // 将工具栏放在顶部 + this.add(tableScrollPane, "Center"); // 将表格区域放在中间 + this.add(bottomPanel, "South"); // 将底部面板放在底部 + + setVisible(true); // 设置主面板为可见状态 } - - - //按钮组件隐藏 + + // 按钮组件隐藏和显示设置 public void OrderView() { - backBtn.setVisible(true); - detailBtn.setVisible(true); - updateBtn.setVisible(false); - deleteBtn.setVisible(true); - addBtn.setVisible(false); - historyBtn.setVisible(false); + backBtn.setVisible(true); // 显示返回按钮 + detailBtn.setVisible(true); // 显示查看详情按钮 + updateBtn.setVisible(false); // 隐藏更新按钮 + deleteBtn.setVisible(true); // 显示删除按钮 + addBtn.setVisible(false); // 隐藏添加按钮 + historyBtn.setVisible(false); // 隐藏历史记录按钮 } - - - - - + + //通过订单查找 public void resultOfNumber(String oNumber) { - this.mark=1; - OutOrderTM outOrderTM = new OutOrderTM(); - outOrderTM.resultOfNumber(oNumber); - outTable.setModel(outOrderTM); - bottomPanelLeft.removeAll(); - countInfoLabel = new JLabel("共"+outOrderTM.getRowCount()+"条记录"); - bottomPanelLeft.add(countInfoLabel); - OrderView(); + this.mark = 1; // 设置标记为1,表示当前显示的是订单数据 + OutOrderTM outOrderTM = new OutOrderTM(); // 创建一个OutOrderTM对象,用于存储订单数据 + outOrderTM.resultOfNumber(oNumber); // 根据订单号查询相关的订单数据 + outTable.setModel(outOrderTM); // 设置表格数据模型为查询到的订单数据 + bottomPanelLeft.removeAll(); // 清除底部左侧面板的所有组件 + countInfoLabel = new JLabel("共" + outOrderTM.getRowCount() + "条记录"); // 显示当前记录的数量 + bottomPanelLeft.add(countInfoLabel); // 将记录数量标签添加到左侧面板 + OrderView(); // 调用OrderView方法,更新按钮的显示状态 } - - + /*刷新*/ public void refreshOutBuffer() { - OutTableModel outTableModel = new OutTableModel(); - outTableModel.allOutBuffer(); - outTable.setModel(outTableModel); - bottomPanelLeft.removeAll(); - countInfoLabel = new JLabel("商品种类:"+outTableModel.getRowCount()+",总价:"+outTableModel.getAllPrice()); - bottomPanelLeft.add(countInfoLabel); - backBtn.setVisible(false); - detailBtn.setVisible(false); - historyBtn.setVisible(true); - updateBtn.setVisible(true); - addBtn.setVisible(true); - deleteBtn.setVisible(true); + OutTableModel outTableModel = new OutTableModel(); // 创建一个新的OutTableModel对象 + outTableModel.allOutBuffer(); // 查询所有购物车数据 + outTable.setModel(outTableModel); // 设置表格数据模型为查询到的所有购物车数据 + bottomPanelLeft.removeAll(); // 清除底部左侧面板的所有组件 + countInfoLabel = new JLabel("商品种类:" + outTableModel.getRowCount() + ",总价:" + outTableModel.getAllPrice()); // 显示商品种类和总价 + bottomPanelLeft.add(countInfoLabel); // 将统计信息标签添加到左侧面板 + backBtn.setVisible(false); // 隐藏返回按钮 + detailBtn.setVisible(false); // 隐藏查看详情按钮 + historyBtn.setVisible(true); // 显示历史记录按钮 + updateBtn.setVisible(true); // 显示更新按钮 + addBtn.setVisible(true); // 显示添加按钮 + deleteBtn.setVisible(true); // 显示删除按钮 } - + /*调出收银出货订单表*/ public void OutOrderRecord() { - this.mark=1; - OutOrderTM outOrderTM = new OutOrderTM(); - outOrderTM.allOutOrderRecord(); - outTable.setModel(outOrderTM); - bottomPanelLeft.removeAll(); - countInfoLabel = new JLabel("共"+outOrderTM.getRowCount()+"条记录"); - bottomPanelLeft.add(countInfoLabel); - OrderView(); + this.mark = 1; // 设置标记为1,表示当前显示的是订单记录数据 + OutOrderTM outOrderTM = new OutOrderTM(); // 创建一个OutOrderTM对象,用于存储订单记录数据 + outOrderTM.allOutOrderRecord(); // 查询所有的出货订单记录 + outTable.setModel(outOrderTM); // 设置表格数据模型为查询到的订单记录 + bottomPanelLeft.removeAll(); // 清除底部左侧面板的所有组件 + countInfoLabel = new JLabel("共" + outOrderTM.getRowCount() + "条记录"); // 显示订单记录的数量 + bottomPanelLeft.add(countInfoLabel); // 将订单记录数量标签添加到左侧面板 + OrderView(); // 调用OrderView方法,更新按钮的显示状态 } - + public void OutRecord(String oNumber) { - this.mark=0; - OutRecordTM outRecordTM = new OutRecordTM(oNumber); - outRecordTM.findOutRecordByINumber(); - outTable.setModel(outRecordTM); - bottomPanelLeft.removeAll(); - countInfoLabel = new JLabel("订单号@"+oNumber+"共有"+outRecordTM.getRowCount()+"条记录"); - bottomPanelLeft.add(countInfoLabel); - backBtn.setVisible(true); - detailBtn.setVisible(false); - updateBtn.setVisible(false); - addBtn.setVisible(false); - historyBtn.setVisible(false); - deleteBtn.setVisible(false); + this.mark = 0; // 设置标记为0,表示当前显示的是订单详情数据 + OutRecordTM outRecordTM = new OutRecordTM(oNumber); // 创建一个OutRecordTM对象,根据订单号查询订单详情数据 + outRecordTM.findOutRecordByINumber(); // 查询根据订单号找到的订单详情 + outTable.setModel(outRecordTM); // 设置表格数据模型为查询到的订单详情数据 + bottomPanelLeft.removeAll(); // 清除底部左侧面板的所有组件 + countInfoLabel = new JLabel("订单号@" + oNumber + "共有" + outRecordTM.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();/*获得购物车*/ - Object source = e.getSource(); - - if(searchBtn==source) { - String number = nameSearchTF.getText(); - resultOfNumber(number); - } - - if(addBtn==source) { - OutDialog outDialog = new OutDialog(jFrame); - outDialog.setVisible(true); - refreshOutBuffer(); - } - - else if(updateBtn==source) { - int rowIndex = outTable.getSelectedRow(); - if(rowIndex==-1) { - JOptionPane.showMessageDialog(this,"请选中一条进行更改数量"); - return; - } - String id =(String) outTable.getValueAt(rowIndex,0); - ChangeSumDialog changesumDialog = new ChangeSumDialog(jFrame,id,"Out"); - changesumDialog.setVisible(true); - refreshOutBuffer(); + BufferImpl = new BufferImpl(); // 创建BufferImpl对象,用于获取购物车数据 + Object source = e.getSource(); // 获取触发事件的按钮 + + if (searchBtn == source) { // 如果点击的是搜索按钮 + String number = nameSearchTF.getText(); // 获取订单号 + resultOfNumber(number); // 根据订单号查询订单数据 } - - else if(deleteBtn==source) { - int rowIndex = outTable.getSelectedRow(); - if(rowIndex==-1) { - JOptionPane.showMessageDialog(this,"请选中一条"); - return; - } - String id =(String) outTable.getValueAt(rowIndex,0); - int select = JOptionPane.showConfirmDialog(this,"是否删除id为"+id+"的记录","提示",JOptionPane.YES_NO_OPTION); - if(select==JOptionPane.YES_OPTION) {/*选择是*/ - if(BufferImpl.DelOutBufferById(id)==true) { - JOptionPane.showMessageDialog(this,"删除成功","提示",JOptionPane.INFORMATION_MESSAGE); - }else { - JOptionPane.showMessageDialog(this,"删除失败","提示",JOptionPane.ERROR_MESSAGE); - } - refreshOutBuffer(); - } - - - }else if(historyBtn==source) {/*查看历史全部记录*/ - OutOrderRecord(); - }else if(backBtn==source) {/*历史记录中的返回按钮*/ - System.out.println("outView中的mark="+mark); - if(mark==1) { - refreshOutBuffer(); - }else if(mark==0) { - OutOrderRecord(); + + if (addBtn == source) { // 如果点击的是添加按钮 + OutDialog outDialog = new OutDialog(jFrame); // 创建OutDialog对话框 + outDialog.setVisible(true); // 显示添加对话框 + refreshOutBuffer(); // 刷新购物车数据 + } else if (updateBtn == source) { // 如果点击的是更新按钮 + int rowIndex = outTable.getSelectedRow(); // 获取选中的表格行索引 + if (rowIndex == -1) { // 如果没有选中行 + JOptionPane.showMessageDialog(this, "请选中一条进行更改数量"); // 提示用户选择记录 + return; } - - }else if(detailBtn==source) {/*查看订单详细*/ - - int rowIndex = outTable.getSelectedRow(); - if(rowIndex==-1) { - JOptionPane.showMessageDialog(this,"请选中一条查看订单详细信息"); + String id = (String) outTable.getValueAt(rowIndex, 0); // 获取选中行的订单ID + ChangeSumDialog changesumDialog = new ChangeSumDialog(jFrame, id, "Out"); // 创建修改数量的对话框 + changesumDialog.setVisible(true); // 显示修改对话框 + refreshOutBuffer(); // 刷新购物车数据 + } else if (deleteBtn == source) { // 如果点击的是删除按钮 + int rowIndex = outTable.getSelectedRow(); // 获取选中的表格行索引 + if (rowIndex == -1) { // 如果没有选中行 + JOptionPane.showMessageDialog(this, "请选中一条"); // 提示用户选择记录 return; } - - String oNumber =(String) outTable.getValueAt(rowIndex,0); - System.out.println("详情订单号为="+oNumber); - OutRecord(oNumber); - } - - - - else if(AccountBtn==source) {/*结账*/ - refreshOutBuffer(); - v = BufferImpl.allOutBuffer(); - if(v.size()==0)/*购物车为空*/{ - JOptionPane.showMessageDialog(null,"您的购物车为空", "提示", JOptionPane.YES_OPTION); - } - else {/*购物车不为空*/ - int res = JOptionPane.showConfirmDialog(null,"总金额:"+outTableModel.getAllPrice()+"元\r\n是否已经结账", "结账", JOptionPane.YES_NO_OPTION); - if(res==JOptionPane.YES_OPTION)/*如果已经结账*/{ - - String[] s =TimeAndOrder.TimeAndOrder(user.getUsername());/*获得时间和订单号*/ - - BufferImpl.InsertOutOrder(s[0],outTableModel.getAllPrice(), s[1], user.getUsername());/*往订单表插入一条记录*/ - for(int i=0;i