package com.lsy.ui; import com.lsy.dao.ProductDaolsy; import com.lsy.dao.impl.ProductDaoImplsy; import com.lsy.model.Productlsy; import javax.swing.*; import javax.swing.table.TableModel; import java.awt.*; import java.util.HashMap; import java.util.List; import java.util.Map; public class ReserveTablemodel extends Myframelsy{ private JTable table; private JLabel lblImage; private JLabel lblListprice; private JLabel lblDescn; private JLabel lblUnitcost; //商品列表集合 private List products; ///创建商品Dao对象 private final ProductDaolsy dao =new ProductDaoImplsy(); //购物车,键是选择商品的Id,值是商品的数量 private Map cart = new HashMap(); //选择的商品索引 private int selectedRow = -1; public ReserveTablemodel(String title,int width,int height){ super(title,width,height); //查询所有商品 products = dao.findAll(); //初始化界面 //添加顶部搜索面板 getContentPane().add(getSearchPanel(), BorderLayout.NORTH); //初始化分隔面板 JSplitPane splitPane = new JSplitPane(); //设置分隔线位置 splitPane.setDividerLocation(600); //设置左侧面板 splitPane.setLeftComponent(getLeftPanel()); //设置右侧面板 splitPane.setRightComponent(getRightPanel()); //添加分隔面板 getContentPane().add(splitPane,BorderLayout.CENTER); } //初始化搜索面板 private JPanel getSearchPanel(){ JPanel searchPanel = new JPanel(); FlowLayout flowLayout = (FlowLayout) searchPanel.getLayout(); flowLayout.setVgap(20); flowLayout.setHgap(40); JLabel lbl = new JLabel("选择商品类别:"); lbl.setFont(new Font("微软雅黑", Font.PLAIN, 15)); searchPanel.add(lbl); String[] categorys = {"鱼类","狗类","爬行类","猫类","鸟类"}; JComboBox comboBox = new JComboBox(categorys); comboBox.setFont(new Font("微软雅黑",Font.PLAIN,15)); searchPanel.add(comboBox); JButton btnGo = new JButton("查询"); btnGo.setFont(new Font("微软雅黑",Font.PLAIN,15)); searchPanel.add(btnGo); JButton btnReset = new JButton("重置"); btnReset.setFont(new Font("微软雅黑",Font.PLAIN,15)); searchPanel.add(btnReset); //注册查询按钮的ActionEvent事件监听器 btnGo.addActionListener(e -> { //所选择的类别 String category = (String) comboBox.getSelectedItem(); //按照类别进行查询 products = dao.findByCategory(category); TableModel model = new ProductTablemodel(products); table.setModel(model); }); //注册重置按钮的ActionEvent事件监听器 btnReset.addActionListener(e -> { products = dao.findAll(); TableModel model = new ProductTablemodel(products); table.setModel(model); }); return searchPanel; } //右侧面板 private JPanel getRightPanel() { JPanel rightPanel = new JPanel(); rightPanel.setBackground(Color.WHITE); rightPanel.setLayout(new GridLayout(2, 1, 0, 0)); lblImage = new JLabel(); rightPanel.add(lblImage); lblImage.setHorizontalAlignment(SwingConstants.CENTER); JPanel detailPanel = new JPanel(); detailPanel.setBackground(Color.WHITE); rightPanel.add(detailPanel); detailPanel.setLayout(new GridLayout(8, 1, 0, 5)); JSeparator separator_1 = new JSeparator(); detailPanel.add(separator_1); lblListprice = new JLabel(); detailPanel.add(lblListprice); //设置字体 lblListprice.setFont(new Font("微软雅黑", Font.PLAIN, 16)); lblUnitcost = new JLabel(); detailPanel.add(lblUnitcost); //设置字体 lblUnitcost.setFont(new Font("微软雅黑", Font.PLAIN, 16)); lblDescn = new JLabel(); detailPanel.add(lblDescn); //设置字体 lblDescn.setFont(new Font("微软雅黑", Font.PLAIN, 16)); JSeparator separator_2 = new JSeparator(); detailPanel.add(separator_2); JButton btnAdd = new JButton("添加购物车"); btnAdd.setFont(new Font("微软雅黑", Font.PLAIN, 15)); detailPanel.add(btnAdd); //布局占位使用 JLabel lb1 = new JLabel(""); detailPanel.add(lb1); JButton btnCheck = new JButton("查看购物车"); btnCheck.setFont(new Font("微软雅黑", Font.PLAIN, 15)); detailPanel.add(btnCheck); //注册【查看购物车】按钮的ActionEvent事件监听器 btnAdd.addActionListener(e -> { if (selectedRow == -1) { return; } //添加商品到购物车处理 Productlsy selectproduct = products.get(selectedRow); String productid = selectproduct.getProductidlsy(); if (cart.containsKey(productid)) { //购物车中已有该商品 //获得商品数量 Integer quantity = cart.get(productid); cart.put(productid, ++quantity); } else { cart.put(productid, 1); } System.out.println(cart); }); //注册【查看购物车】按钮的ActionEvent事件监听器 // btnCheck.addActionListener(e -> { // CartFrame cartFrame = new CartFrame(cart, this); // cartFrame.setVisible(true); // setVisible(false); // } // ); return rightPanel; } //初始化左侧面板 private JScrollPane getLeftPanel(){ JScrollPane leftScrollPane = new JScrollPane(); leftScrollPane.setViewportView(getTable()); return leftScrollPane; } //初始化左侧面板中的表格控件 private JTable getTable() { TableModel model = new ProductTablemodel(this.products); if (table == null) { table = new JTable(model); //设置表中内容字体 table.setFont(new Font("微软雅黑", Font.PLAIN, 16)); //设置表列标题字体 table.getTableHeader().setFont(new Font("微软雅黑", Font.PLAIN, 16)); //设置表行高 table.setRowHeight(51); table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); ListSelectionModel rowSelectionModel = table.getSelectionModel(); rowSelectionModel.addListSelectionListener(e -> { //只处理鼠标释放 if (e.getValueIsAdjusting()) { return; } ListSelectionModel lsm = (ListSelectionModel) e.getSource(); selectedRow = lsm.getMinSelectionIndex(); if (selectedRow < 0) { return; } //更新右侧面板内容 Productlsy p = products.get(selectedRow); String petImage = String.format("/images/%s", p.getImagelsy()); ImageIcon icon = new ImageIcon(ProductListFrame.class.getResource(petImage)); lblImage.setIcon(icon); String descn = p.getDescnlsy(); lblDescn.setText("商品描述:" + descn); double listprice = p.getListpricelsy(); String slistprice = String.format("商品市场价%.2f", listprice); lblListprice.setText(slistprice); double unitcost = p.getUnitcostlsy(); String slilUnitcost = String.format("商品单价%.2f", unitcost); lblUnitcost.setText(slilUnitcost); }); } else { table.setModel(model); } return table; } }