|
|
package view;
|
|
|
|
|
|
import dao.DishDAOwcy;
|
|
|
import dao.impl.DishDaoImplwcy;
|
|
|
import domain.Dishwcy;
|
|
|
|
|
|
import javax.swing.*;
|
|
|
import javax.swing.table.TableModel;
|
|
|
import java.awt.*;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.List;
|
|
|
|
|
|
//商品列表窗口
|
|
|
public class DishListFramewcy extends MyFrame{
|
|
|
|
|
|
|
|
|
private JTable table;
|
|
|
//菜品图片
|
|
|
private JLabel lblImage;
|
|
|
//列表价格
|
|
|
private JLabel lblListprice;
|
|
|
//菜品描述
|
|
|
private JLabel lblDescn;
|
|
|
//菜品价格
|
|
|
private JLabel lblUnitcost;
|
|
|
|
|
|
//商品列表集合
|
|
|
private List<Dishwcy> dishs;
|
|
|
///创建商品Dao对象
|
|
|
private final DishDAOwcy dao =new DishDaoImplwcy();
|
|
|
|
|
|
//购物车,键是选择商品的Id,值是商品的数量
|
|
|
private Map<String,Integer> cart = new HashMap<String,Integer>();
|
|
|
|
|
|
//选择的商品索引
|
|
|
private int selectedRow = -1;
|
|
|
// 构造方法,初始化窗口和界面
|
|
|
public DishListFramewcy(String title,int width,int height){
|
|
|
super(title,width,height);
|
|
|
this.tubiao();
|
|
|
|
|
|
//查询所有商品
|
|
|
dishs = 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 中。
|
|
|
JPanel searchPanel = new JPanel();
|
|
|
//获取 searchPanel 当前的布局管理器,并将其强制转换为 FlowLayout 类型。
|
|
|
FlowLayout flowLayout = (FlowLayout) searchPanel.getLayout();
|
|
|
//设置 FlowLayout 的垂直间隔为 20 像素。
|
|
|
flowLayout.setVgap(20);
|
|
|
//设置 FlowLayout 的水平间隔为 40 像素。
|
|
|
flowLayout.setHgap(40);
|
|
|
|
|
|
//创建一个新的 JLabel 对象,用于显示文本 "选择商品类别:"。
|
|
|
JLabel lbl = new JLabel("选择商品类别:");
|
|
|
//为标签 lbl 设置字体样式和大小。
|
|
|
lbl.setFont(new Font("微软雅黑", Font.PLAIN, 15));
|
|
|
//将标签 lbl 添加到 searchPanel 中。
|
|
|
searchPanel.add(lbl);
|
|
|
|
|
|
//定义一个字符串数组 categorys,包含商品类别的名称。
|
|
|
String[] categorys = {"凉菜","肉类","主食","海鲜","饮料"};
|
|
|
//创建一个新的 JComboBox 对象,使用 categorys 数组作为下拉列表的选项。
|
|
|
JComboBox comboBox = new JComboBox(categorys);
|
|
|
//为下拉列表 comboBox 设置字体样式和大小。
|
|
|
comboBox.setFont(new Font("微软雅黑",Font.PLAIN,15));
|
|
|
//将下拉列表 comboBox 添加到 searchPanel 中。
|
|
|
searchPanel.add(comboBox);
|
|
|
|
|
|
//创建一个新的 JButton 对象,用于触发查询操作,按钮上显示文本 "查询"。
|
|
|
JButton btnGo = new JButton("查询");
|
|
|
//为按钮 btnGo 设置字体样式和大小
|
|
|
btnGo.setFont(new Font("微软雅黑",Font.PLAIN,15));
|
|
|
//将按钮 btnGo 添加到 searchPanel 中。
|
|
|
searchPanel.add(btnGo);
|
|
|
|
|
|
//创建一个新的 JButton 对象,用于重置搜索条件,按钮上显示文本 "重置"。
|
|
|
JButton btnReset = new JButton("重置");
|
|
|
//为按钮 btnReset 设置字体样式和大小。
|
|
|
btnReset.setFont(new Font("微软雅黑",Font.PLAIN,15));
|
|
|
//将按钮 btnReset 添加到 searchPanel 中。
|
|
|
searchPanel.add(btnReset);
|
|
|
|
|
|
//注册查询按钮的ActionEvent事件监听器
|
|
|
btnGo.addActionListener(e -> {
|
|
|
//从下拉列表 comboBox 中获取选中的商品类别。
|
|
|
String category = (String) comboBox.getSelectedItem();
|
|
|
//调用 dao 对象的 findByCategory 方法,根据选中的类别查询商品列表,并更新 dishs 变量。
|
|
|
dishs = dao.findByCategory(category);
|
|
|
//创建一个新的 DishTableModelwcy 对象,使用更新后的 dishs 列表作为数据源。
|
|
|
TableModel model = new DishTableModelwcy(dishs);
|
|
|
//更新 table 组件的数据模型为新创建的 model。
|
|
|
table.setModel(model);
|
|
|
});
|
|
|
//注册重置按钮的ActionEvent事件监听器
|
|
|
btnReset.addActionListener(e -> {
|
|
|
//调用 dao 对象的 findAll 方法,获取所有商品的列表,并更新 dishs 变量。
|
|
|
dishs = dao.findAll();
|
|
|
//创建一个新的 DishTableModelwcy 对象,使用 dishs 列表作为数据源。
|
|
|
TableModel model = new DishTableModelwcy(dishs);
|
|
|
//更新 table 组件的数据模型为新创建的 model。
|
|
|
table.setModel(model);
|
|
|
|
|
|
});
|
|
|
return searchPanel;
|
|
|
}
|
|
|
|
|
|
|
|
|
// 获取右侧面板,显示商品详情和操作按钮
|
|
|
private JPanel getRightPanel() {
|
|
|
|
|
|
//创建一个新的 JPanel 对象,这将是右侧面板。
|
|
|
JPanel rightPanel = new JPanel();
|
|
|
//设置 rightPanel 的背景颜色为白色。
|
|
|
rightPanel.setBackground(Color.WHITE);
|
|
|
|
|
|
//为 rightPanel 设置布局管理器为 GridLayout,其参数分别为行数、列数以及行和列之间的间隙
|
|
|
rightPanel.setLayout(new GridLayout(2, 1, 0, 0));
|
|
|
|
|
|
//创建一个新的 JLabel 对象,用于后续显示商品图片。
|
|
|
lblImage = new JLabel();
|
|
|
//将图片标签 lblImage 添加到 rightPanel。
|
|
|
rightPanel.add(lblImage);
|
|
|
//设置 lblImage 的水平对齐方式为居中。
|
|
|
lblImage.setHorizontalAlignment(SwingConstants.CENTER);
|
|
|
|
|
|
//创建一个新的 JPanel 对象,用于展示商品的详细信息。
|
|
|
JPanel detailPanel = new JPanel();
|
|
|
//设置 detailPanel 的背景颜色为白色。
|
|
|
detailPanel.setBackground(Color.WHITE);
|
|
|
//将 detailPanel 添加到 rightPanel。
|
|
|
rightPanel.add(detailPanel);
|
|
|
|
|
|
//为 detailPanel 设置布局管理器为 GridLayout,具有8行1列,以及行和列之间的间隙。
|
|
|
detailPanel.setLayout(new GridLayout(8, 1, 0, 5));
|
|
|
|
|
|
//创建一个水平分隔线组件。
|
|
|
JSeparator separator_1 = new JSeparator();
|
|
|
//将分隔线 separator_1 添加到 detailPanel。
|
|
|
detailPanel.add(separator_1);
|
|
|
|
|
|
//创建一个新的 JLabel 对象,用于显示商品的列表价格。
|
|
|
lblListprice = new JLabel();
|
|
|
//将 lblListprice 添加到 detailPanel。
|
|
|
detailPanel.add(lblListprice);
|
|
|
//为 lblListprice 设置字体样式和大小。
|
|
|
lblListprice.setFont(new Font("微软雅黑", Font.PLAIN, 16));
|
|
|
|
|
|
//
|
|
|
lblUnitcost = new JLabel();
|
|
|
//将 lblUnitcost 添加到 detailPanel。
|
|
|
detailPanel.add(lblUnitcost);
|
|
|
//为 lblUnitcost 设置字体样式和大小。
|
|
|
lblUnitcost.setFont(new Font("微软雅黑", Font.PLAIN, 16));
|
|
|
|
|
|
lblDescn = new JLabel();
|
|
|
//将 lblDescn 添加到 detailPanel。
|
|
|
detailPanel.add(lblDescn);
|
|
|
//为 lblDescn 设置字体样式和大小。
|
|
|
lblDescn.setFont(new Font("微软雅黑", Font.PLAIN, 16));
|
|
|
|
|
|
JSeparator separator_2 = new JSeparator();
|
|
|
detailPanel.add(separator_2);
|
|
|
|
|
|
|
|
|
//创建一个按钮,用于将选中的商品添加到购物车。
|
|
|
JButton btnAdd = new JButton("添加购物车");
|
|
|
//为按钮 btnAdd 设置字体样式和大小。
|
|
|
btnAdd.setFont(new Font("微软雅黑", Font.PLAIN, 15));
|
|
|
//将按钮 btnAdd 添加到 detailPanel。
|
|
|
detailPanel.add(btnAdd);
|
|
|
|
|
|
//创建一个空的标签,用作布局占位。
|
|
|
JLabel lb1 = new JLabel("");
|
|
|
//将占位标签 lb1 添加到 detailPanel。
|
|
|
detailPanel.add(lb1);
|
|
|
|
|
|
//创建一个按钮,用于查看购物车。
|
|
|
JButton btnCheck = new JButton("查看购物车");
|
|
|
//为按钮 btnCheck 设置字体样式和大小。
|
|
|
btnCheck.setFont(new Font("微软雅黑", Font.PLAIN, 15));
|
|
|
//将按钮 btnCheck 添加到 detailPanel。
|
|
|
detailPanel.add(btnCheck);
|
|
|
|
|
|
//注册【查看购物车】按钮的ActionEvent事件监听器
|
|
|
btnAdd.addActionListener(e -> {
|
|
|
//检查是否有商品被选中,如果没有选中任何商品(selectedRow 为 -1),则不执行任何操作。
|
|
|
if (selectedRow == -1) {
|
|
|
return;
|
|
|
}
|
|
|
//获取选中的商品对象。
|
|
|
Dishwcy selectdish = dishs.get(selectedRow);
|
|
|
//获取选中商品的ID。
|
|
|
String dishid = selectdish.getDishid();
|
|
|
|
|
|
//检查购物车中是否已经包含该商品ID。
|
|
|
if (cart.containsKey(dishid)) {
|
|
|
//如果购物车中已有该商品,则增加其数量。
|
|
|
Integer quantity = cart.get(dishid);
|
|
|
cart.put(dishid, ++quantity);
|
|
|
|
|
|
} else {
|
|
|
//如果购物车中没有该商品,则添加到购物车并设置数量为1。
|
|
|
cart.put(dishid, 1);
|
|
|
}
|
|
|
//打印购物车的内容到控制台(调试用)。
|
|
|
System.out.println(cart);
|
|
|
});
|
|
|
//注册【查看购物车】按钮的ActionEvent事件监听器
|
|
|
btnCheck.addActionListener(e -> {
|
|
|
//创建一个新的 CartFrametyj 对象,传递当前的购物车和当前窗口作为参数。
|
|
|
CartFrametyj cartFrame = new CartFrametyj(cart, this);
|
|
|
//显示购物车窗口。
|
|
|
cartFrame.setVisible(true);
|
|
|
//隐藏当前窗口。
|
|
|
setVisible(false);
|
|
|
});
|
|
|
//返回配置好的 rightPanel 对象。
|
|
|
return rightPanel;
|
|
|
}
|
|
|
|
|
|
// 获取左侧面板,包含商品列表的滚动窗格
|
|
|
private JScrollPane getLeftPanel(){
|
|
|
//创建一个新的 JScrollPane 对象,用于提供滚动功能。
|
|
|
JScrollPane leftScrollPane = new JScrollPane();
|
|
|
//调用 getTable 方法获取 JTable 对象,并将其设置为 JScrollPane 的视图组件。
|
|
|
leftScrollPane.setViewportView(getTable());
|
|
|
//返回配置好的 JScrollPane 对象,即左侧面板。
|
|
|
return leftScrollPane;
|
|
|
}
|
|
|
// 定义了一个名为 getTable 的私有方法,用于创建和配置商品列表的 JTable 控件。
|
|
|
private JTable getTable() {
|
|
|
//创建一个新的 TableModel 对象,使用当前的 dishs 商品列表作为数据源。
|
|
|
TableModel model = new DishTableModelwcy(this.dishs);
|
|
|
|
|
|
//检查是否已经创建了 JTable 对象。如果尚未创建,将进入条件块进行初始化。
|
|
|
if (table == null) {
|
|
|
//创建一个新的 JTable 对象,使用上面创建的 model 作为其数据模型。
|
|
|
table = new JTable(model);
|
|
|
//为 JTable 设置字体样式和大小。
|
|
|
table.setFont(new Font("微软雅黑", Font.PLAIN, 16));
|
|
|
//为 JTable 的表头设置字体样式和大小。
|
|
|
table.getTableHeader().setFont(new Font("微软雅黑", Font.PLAIN, 16));
|
|
|
//设置 JTable 的行高。
|
|
|
table.setRowHeight(51);
|
|
|
//设置 JTable 的选择模式为单选。
|
|
|
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
|
|
|
//获取 JTable 的选择模型。
|
|
|
ListSelectionModel rowSelectionModel = table.getSelectionModel();
|
|
|
//为选择模型添加一个列表选择监听器,用于处理表格行的选择事件。
|
|
|
rowSelectionModel.addListSelectionListener(e -> {
|
|
|
//如果选择值正在调整中,则不处理事件。
|
|
|
if (e.getValueIsAdjusting()) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
//获取触发事件的选择模型,并更新 selectedRow 为当前选中的行索引。
|
|
|
ListSelectionModel lsm = (ListSelectionModel) e.getSource();
|
|
|
selectedRow = lsm.getMinSelectionIndex();
|
|
|
//如果没有选中任何行,则不执行任何操作。
|
|
|
if (selectedRow < 0) {
|
|
|
return;
|
|
|
}
|
|
|
//根据选中行索引获取对应的 Dishwcy 商品对象。
|
|
|
Dishwcy p = dishs.get(selectedRow);
|
|
|
//格式化商品图片的路径。
|
|
|
String petImage = String.format("/images/%s", p.getImage());
|
|
|
//创建一个 ImageIcon 对象,用于显示商品图片。
|
|
|
ImageIcon icon = new ImageIcon(DishListFramewcy.class.getResource(petImage));
|
|
|
//更新 lblImage 标签的图标为商品图片。
|
|
|
lblImage.setIcon(icon);
|
|
|
//获取商品描述并更新 lblDescn 标签的文本。
|
|
|
String descn = p.getDescn();
|
|
|
lblDescn.setText("商品描述:" + descn);
|
|
|
|
|
|
//获取商品单位成本,格式化为字符串,并更新 lblUnitcost 标签的文本。
|
|
|
double unitcost = p.getUnitcost();
|
|
|
String slilUnitcost = String.format("商品单价%.2f", unitcost);
|
|
|
lblUnitcost.setText(slilUnitcost);
|
|
|
});
|
|
|
} else {
|
|
|
table.setModel(model);
|
|
|
}
|
|
|
return table;
|
|
|
|
|
|
}
|
|
|
// 设置窗口图标
|
|
|
public void tubiao() {
|
|
|
MyFrame.setTitleImage(this,"e39b4ce2ebe83d39adabb339c23a1e0.png" );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|