package com.tyj.ui; import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.Font; import java.util.Date; import java.util.Map; import java.util.Set; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTable; import javax.swing.table.TableModel; import com.tyj.ui.ProductListFrame; import com.tyj.ui.MainApp; import com.tyj.dao.OrderDao; import com.tyj.dao.OrderDetailDao; import com.tyj.dao.ProductDao; import com.tyj.dao.mysql.OrderDaoImp; import com.tyj.dao.mysql.OrderDetailDaoImp; import com.tyj.dao.mysql.productDaolmp; import com.tyj.domain.Order; import com.tyj.domain.OrderDetail; import com.tyj.domain.Product; public class CartFrametyj extends MyFrametyj { //表格 private JTable table; //表格数据 private Object[][] data = null; private ProductDao dao = new productDaolmp(); private Map cart; private ProductListFrame productListFrame; //prodcutlistframe窗口 public CartFrame(Map cart,ProductListFrame productListFrame) { super("商品购物车", 1000, 700); this.cart = cart; this.productListFrame = productListFrame; //顶部的面板 JPanel topPanel = new JPanel(); FlowLayout fl_topPanel = (FlowLayout) topPanel.getLayout(); fl_topPanel.setVgap(10); fl_topPanel.setHgap(20); getContentPane().add(topPanel, BorderLayout.NORTH); JButton btnReturn = new JButton("\u8FD4\u56DE\u5546\u5217\u8868"); btnReturn.setFont(new Font("Microsoft YaHei", Font.PLAIN, 15)); topPanel.add(btnReturn); btnReturn.addActionListener(e->{ this.productListFrame.setVisible(true); this.setVisible(false); }); JButton btuSubmit = new JButton("\u63D0\u4EA4\u88A2\u5355"); topPanel.add(btuSubmit); btuSubmit.setFont(new Font("Microsoft YaHei", Font.PLAIN, 15)); //中部表格 JScrollPane scrollPane = new JScrollPane(); getContentPane().add(scrollPane, BorderLayout.CENTER); scrollPane.setViewportView(getTable()); //提交订单 btuSubmit.addActionListener(e -> { // generateOrders(); JLabel label = new JLabel("确认提交"); label.setFont(new Font("Microsoft YaHei", Font.PLAIN, 15)); if (JOptionPane.showConfirmDialog(this, label, "", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) { // TODO PayFrame p=new PayFrame(); p.setVisible(true); } else { System.exit(0); } }); btnReturn.addActionListener(e -> { for (int i = 0; i < data.length; i++) { String productid = (String) data[i][0]; Integer quantity = (Integer) data[i][3]; cart.put(productid, quantity); } this.productListFrame.setVisible(true); setVisible(false); }); } private void generateOrders() { // TODO 自动生成的方法存根 } //初始化表格 private JTable getTable() { JTable table = new JTable(); //准备数据 data = new Object[cart.size()][5]; Set keys = this.cart.keySet(); int indx = 0; //遍历购物车 for (String productid : keys) { Product p = dao.findById(productid); data[indx][0] = p.getmenuid();//商品编号 data[indx][1] = p.getName();//商品名 data[indx][2] = new Double(p.getPrice());//商品单价 data[indx][3] = new Integer(cart.get(productid));//商品数量 //计算应付金额 double amount = (double) data[indx][2] * (int) data[indx][3]; data[indx][4] = new Double(amount);//应付金额 indx++; } // CartTableModel model = new CartTableModel(data); if (table == null) { // 设置表格字体 table = new JTable(model); //设置表格字体 table.setFont(new Font("Microsoft YaHei", Font.PLAIN, 16)); //设置表格标题字体 table.getTableHeader().setFont(new Font("Microsoft YaHei", Font.BOLD, 17)); //设置行高 table.setRowHeight(51); table.setRowSelectionAllowed(false); } else { table.setModel(model); } return table; }}