You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
localhost9000/ProductListFrame.java

249 lines
8.0 KiB

package com.tyj.ui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.event.ListSelectionEvent;
import com.tyj.dao.ProductDao;
import com.tyj.dao.mysql.productDaolmp;
import com.tyj.domain.Product;
public class ProductListFrame extends MyFrametyj{
private JTable table;
private JLabel lblImage;
private JLabel lblPrice;
private JLabel lblBrief;
private JLabel lblPrice1;
private List<Product> products = null;
private ProductDao pdao = new productDaolmp();
private Map<String,Integer> cart = new HashMap<String,Integer>();
private int selectedRow = -1;
public ProductListFrame() {
super("商品列表", 1000, 700);
products = pdao.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 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);
//设置字体
lblPrice.setFont(new Font("微软雅黑",Font.PLAIN,16));
lblPrice1 = new JLabel();
detailPanel.add(lblPrice1);
//设置字体
lblPrice1.setFont(new Font("微软雅黑",Font.PLAIN,16));
lblBrief = new JLabel();
detailPanel.add(lblBrief);
//设置字体
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 < 0) {
return;
}
//添加商品到购物车处理
Product selectProduct = products.get(selectedRow);
String productid = selectProduct.getProductid();
if(cart.containsKey(productid)) {//购物车中已经有该商品
//获得商品数量
Integer quantity = cart.get(productid);
cart.put(productid, ++quantity);
}else {//购物车中还没有还商品
cart.put(productid, 1);
}
System.out.println(cart);
});
btnCheck.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15));
detailPanel.add(btnAdd);
JButton btnCheck1 = new JButton("\u67E5\u7788\u8D2D\u7269\u8F66");
//注册【查看购物车】按钮的ActionEvent事件监听器
//查看购物车
btnCheck1.addActionListener(e -> {
CartFrame cartFrame=new CartFrame(cart,this);
cartFrame.setVisible(true);
this.setVisible(false);
});
btnCheck1.setFont(new Font("Microsoft YaHei UI",Font.PLAIN,15));
detailPanel.add(btnCheck1);
return rightPanel;
}
//初始化左侧面板
private JScrollPane getLeftPanel() {
JScrollPane leftScrollPane = new JScrollPane();
//将表格作为滚动面板的各个视口试图
leftScrollPane.setViewportView(getTable());
return leftScrollPane;
}
//初始化左侧面板中的表格控件
private JTable getTable() {
ProductTableModel 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.BOLD,16));
//设置表行高
table.setRowHeight(51);
table.setRowSelectionAllowed(true);
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;
}
//更新右侧面板内容
int selectedRow = table.convertRowIndexToModel(e.getFirstIndex());
Product p = products.get(selectedRow);
String petImage = String.format("/images/%s",p.getImage());
ImageIcon icon = new ImageIcon(ProductListFrame.class.getResource(petImage));
lblImage.setIcon(icon);
String descn = p.getDescn();
lblDescn.setText("菜品介绍"+ brief);
double price = p.getPrice();
String sprice = String.format("菜品市场价:%.2f",price);
lblListprice.setText(sprice);
double price1 = p.getPrice1();
String slblPrice1 = String.format("商品会员价:%.2f",price1);
lblUnitcost.setText(slblPrice1);
});
}else {
table.setModel(model);
}
return table;
}
//初始化搜索面板
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 = pdao.findCategory(category);
ProductTableModel model = new ProductTableModel(products);
table.setModel(model);
});
// 注册重置按钮的ActionEvent事件监听器
btnReset.addActionListener(e -> {
products = pdao.findAll();
ProductTableModel model = new ProductTableModel(products);
table.setModel(model);
});
return searchPanel;
}}