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.
test/branch_zyx/ProdCatalogView.java

201 lines
9.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.lingnan.supermarket.view; // 定义包名表示这个类属于com.lingnan.supermarket.view包
import java.awt.BorderLayout; // 导入BorderLayout布局管理器类
import java.awt.Color; // 导入Color类用于定义颜色
import java.awt.FlowLayout; // 导入FlowLayout布局管理器类
import java.awt.event.ActionEvent; // 导入ActionEvent类用于处理按钮点击等事件
import java.awt.event.ActionListener; // 导入ActionListener接口用于实现事件监听
import java.awt.event.ItemEvent; // 导入ItemEvent类用于处理下拉框选择事件
import java.awt.event.ItemListener; // 导入ItemListener接口用于实现下拉框事件监听
import java.util.ArrayList; // 导入ArrayList类用于存储动态数组
import javax.swing.ImageIcon; // 导入ImageIcon类用于显示图片
import javax.swing.JButton; // 导入JButton类用于创建按钮
import javax.swing.JComboBox; // 导入JComboBox类用于创建下拉框
import javax.swing.JFrame; // 导入JFrame类用于创建窗口框架
import javax.swing.JLabel; // 导入JLabel类用于创建标签
import javax.swing.JPanel; // 导入JPanel类用于创建面板
import javax.swing.JScrollPane; // 导入JScrollPane类用于创建滚动面板
import javax.swing.JTable; // 导入JTable类用于创建表格
import javax.swing.JTextField; // 导入JTextField类用于创建文本框
import com.lingnan.supermarket.dao.impl.prodCatalogImpl; // 导入prodCatalogImpl类用于操作商品目录数据
import com.lingnan.supermarket.dialog.UserDialog; // 导入UserDialog类用于显示用户对话框
import com.lingnan.supermarket.dto.ProdCatalog; // 导入ProdCatalog类用于表示商品目录数据传输对象
import com.lingnan.supermarket.dto.Production; // 导入Production类用于表示产品数据传输对象
import com.lingnan.supermarket.dto.User; // 导入User类用于表示用户数据传输对象
import com.lingnan.supermarket.table.ProdCatalogTM; // 导入ProdCatalogTM类用于表示商品目录表格模型
import com.lingnan.supermarket.table.StorageTableModel; // 导入StorageTableModel类用于表示库存表格模型
import com.lingnan.supermarket.table.UserTableModel; // 导入UserTableModel类用于表示用户表格模型
import com.lingnan.supermarket.utils.FontUtil; // 导入FontUtil类用于字体工具类
public class ProdCatalogView extends JPanel { // 定义ProdCatalogView类继承自JPanel
// 上面
private JPanel toolBarPanel; // 工具栏面板
private JPanel searchPanel; // 搜索面板
private JLabel logLabel, locationLabel; // 标签,用于显示文本
private JTextField nameSearchTF; // 文本框,用于输入搜索内容
private JButton searchBtn; // 搜索按钮
private JPanel opePanel; // 操作面板
private JButton addBtn, updateBtn, deleteBtn; // 添加、更新、删除按钮
private String catalog = "0"; // 商品目录ID
private JComboBox<String> combo; // 下拉框,用于选择商品目录
private String log[]=null; // 商品目录名称数组
private ArrayList<String>alog=null; // 商品目录名称列表
private ProdCatalogTM prodCatalogTM; // 商品目录表格模型
private ProdCatalog pc; // 商品目录数据传输对象
private prodCatalogImpl pci; // 商品目录数据操作实现类
// 中间
private JScrollPane tableScrollPane; // 表格滚动面板
private JTable prodCatalogTable; // 商品目录表格
// 下面
private JPanel bottomPanel; // 底部面板
private JLabel countInfoLabel; // 标签,用于显示记录总数
private JFrame jFrame; // 窗口框架
// 构造方法接收一个JFrame参数
public ProdCatalogView(JFrame jFrame) {
this.setLayout(new BorderLayout()); // 设置面板布局为BorderLayout
initView(); // 初始化视图
this.jFrame = jFrame; // 保存传入的JFrame实例
}
private void initView() {
// 初始化视图方法
toolBarPanel = new JPanel(new BorderLayout()); // 创建工具栏面板并设置布局为BorderLayout
searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 创建搜索面板并设置布局为左对齐的FlowLayout
locationLabel = new JLabel("商品目录"); // 创建商品目录标签并设置文本
locationLabel.setFont(new FontUtil().userFont); // 设置标签字体
locationLabel.setForeground(new Color(18, 150, 219)); // 设置标签前景色
logLabel = new JLabel("分类"); // 创建分类标签并设置文本
nameSearchTF = new JTextField(10); // 创建文本框并设置大小
searchBtn = new JButton("搜索", new ImageIcon("static\\icon\\search.png")); // 创建搜索按钮并设置图标和文本
// 创建操作面板和按钮,并设置图标(注释部分,未使用)
// opePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
// addBtn = new JButton(new ImageIcon("static\\icon\\add.png"));
// updateBtn = new JButton(new ImageIcon("static\\icon\\update.png"));
// deleteBtn = new JButton(new ImageIcon("static\\icon\\delete.png"));
// 为按钮添加事件监听(注释部分,未使用)
// addBtn.addActionListener(this);
// updateBtn.addActionListener(this);
// deleteBtn.addActionListener(this);
// 将按钮添加到操作面板(注释部分,未使用)
// opePanel.add(addBtn);
// opePanel.add(updateBtn);
// opePanel.add(deleteBtn);
pci=new prodCatalogImpl(); // 创建商品目录数据操作实现类实例
this.alog=pci.findNameProdCatalog(); // 查找所有商品目录名称
this.log=new String[alog.size()]; // 创建字符串数组以存储商品目录名称
for(int i=0;i<alog.size();i++)
log[i]=alog.get(i); // 将商品目录名称填充到数组
for(int i=0;i<log.length;i++)
{
System.out.println(log[i]); // 打印所有商品目录名称
}
combo = new JComboBox<String>(log); // 创建下拉框并设置选项
combo.addItemListener(new MyItemListener()); // 为下拉框添加事件监听器
searchPanel.add(locationLabel); // 将商品目录标签添加到搜索面板
searchPanel.add(logLabel); // 将分类标签添加到搜索面板
searchPanel.add(combo); // 将下拉框添加到搜索面板
/*
* searchPanel.add(nameSearchTF); // 将文本框添加到搜索面板(注释部分,未使用)
* searchPanel.add(searchBtn); // 将搜索按钮添加到搜索面板(注释部分,未使用)
*/
toolBarPanel.add(searchPanel, "West"); // 将搜索面板添加到工具栏面板的西边
// toolBarPanel.add(opePanel, "East"); // 将操作面板添加到工具栏面板的东边(注释部分,未使用)
// 中间表格
prodCatalogTM = new ProdCatalogTM(); // 创建商品目录表格模型
prodCatalogTM.all(); // 加载所有商品目录数据
prodCatalogTable = new JTable(prodCatalogTM); // 创建表格并设置模型
prodCatalogTable.setFont(FontUtil.tableFont); // 设置表格字体
prodCatalogTable.setRowHeight(50); // 设置表格行高
tableScrollPane = new JScrollPane(prodCatalogTable); // 创建滚动面板并添加表格
// 下面
bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 创建底部面板并设置布局为左对齐的FlowLayout
countInfoLabel = new JLabel("总共" + prodCatalogTable.getRowCount() + "条"); // 创建记录数标签并设置文本
bottomPanel.add(countInfoLabel); // 将记录数标签添加到底部面板
this.add(toolBarPanel, "North"); // 将工具栏面板添加到面板的北边
this.add(tableScrollPane, "Center"); /* 将表格放到中间 */ // 将表格滚动面板添加到面板的中间
this.add(bottomPanel, "South"); // 将底部面板添加到面板的南边
setVisible(true); // 设置面板为可见
}
// @Override
// public void actionPerformed(ActionEvent e) {
// Object source = e.getSource();
// if (addBtn == source) { // 如果事件源是添加按钮
// UserDialog userDialog = new UserDialog(jFrame); // 创建用户对话框
// userDialog.setVisible(true); // 设置用户对话框为可见
// } else if (updateBtn == source) { // 如果事件源是更新按钮
// // 更新按钮的事件处理代码(未实现)
// } else if (deleteBtn == source) { // 如果事件源是删除按钮
// // 删除按钮的事件处理代码(未实现)
// }
// }
public class MyItemListener implements ItemListener { // 创建一个内部类实现ItemListener接口
@Override
public void itemStateChanged(ItemEvent e) { // 重写itemStateChanged方法
JComboBox cb = (JComboBox) e.getSource(); // 获取事件源下拉框
String catalog1 = (String) cb.getSelectedItem(); // 获取选中的商品目录名称
pci =new prodCatalogImpl(); // 创建商品目录数据操作实现类实例
for(int i=0;i<log.length;i++){ // 遍历商品目录数组
if(catalog1.equals(log[i])) // 如果选中的商品目录名称与数组中的名称匹配
catalog=pci.findProdCatalogByname(catalog1); // 查找对应的商品目录
System.out.println(catalog); // 打印商品目录信息
}
refreshFindId2(); // 刷新界面
}
}
// 刷新当前界面
public void refreshFindId2() {
Production p = new Production(); // 创建Production对象
p.setId2(catalog); // 设置Production对象的ID2
prodCatalogTM = new ProdCatalogTM(); // 创建商品目录表格模型
prodCatalogTM.ById2(p); // 根据ID2查询商品目录数据
prodCatalogTable.setModel(prodCatalogTM); // 更新表格模型
// 同时更新下面的记录数
refreshCount(); // 刷新记录数
}
public void refreshCount() {
bottomPanel.removeAll(); // 清空底部面板
countInfoLabel = new JLabel("总共" + prodCatalogTM.getRowCount() + "条"); // 创建新的记录数标签
bottomPanel.add(countInfoLabel); // 将记录数标签添加到底部面板
}
}