|
|
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; // 导入事件监听接口
|
|
|
import java.awt.event.ActionListener; // 导入事件监听器接口
|
|
|
|
|
|
import javax.swing.ImageIcon; // 导入ImageIcon类,用于显示图片
|
|
|
import javax.swing.JButton; // 导入JButton类,用于创建按钮
|
|
|
import javax.swing.JFrame; // 导入JFrame类,用于创建窗口
|
|
|
import javax.swing.JLabel; // 导入JLabel类,用于创建标签
|
|
|
import javax.swing.JOptionPane; // 导入JOptionPane类,用于显示对话框
|
|
|
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.productionService; // 导入productionService接口
|
|
|
import com.lingnan.supermarket.dao.impl.productionImpl; // 导入productionImpl实现类
|
|
|
import com.lingnan.supermarket.dialog.ProductionDialog; // 导入ProductionDialog类
|
|
|
import com.lingnan.supermarket.dialog.UserDialog; // 导入UserDialog类
|
|
|
import com.lingnan.supermarket.dto.Production; // 导入Production类
|
|
|
import com.lingnan.supermarket.table.StorageRecordTM; // 导入StorageRecordTM类
|
|
|
import com.lingnan.supermarket.table.StorageTableModel; // 导入StorageTableModel类
|
|
|
import com.lingnan.supermarket.utils.FontUtil; // 导入FontUtil类,用于字体设置
|
|
|
|
|
|
public class StorageView extends JPanel implements ActionListener{ // 创建StorageView类,继承JPanel并实现ActionListener接口
|
|
|
|
|
|
//上面
|
|
|
private JPanel toolBarPanel; // 工具栏面板变量声明
|
|
|
|
|
|
private JPanel searchPanel; // 搜索面板变量声明
|
|
|
private JLabel nameLabel,locationLabel; // 标签变量声明
|
|
|
private JTextField nameSearchTF; // 文本框变量声明
|
|
|
private JButton searchBtn; // 搜索按钮变量声明
|
|
|
|
|
|
private JPanel opePanel; // 操作面板变量声明
|
|
|
private JButton addBtn,updateBtn,deleteBtn,historyBtn,backBtn; // 按钮变量声明
|
|
|
|
|
|
//中间
|
|
|
private JScrollPane tableScrollPane; // 表格滚动面板变量声明
|
|
|
private JTable storageTable; // 表格变量声明
|
|
|
|
|
|
//下面
|
|
|
private JPanel bottomPanel; // 底部面板变量声明
|
|
|
private JLabel countInfoLabel; // 记录总数标签变量声明
|
|
|
|
|
|
private StorageTableModel storageTableModel ; // 表格模型变量声明
|
|
|
private JFrame jFrame; // 窗口变量声明
|
|
|
|
|
|
private productionService productionService=new productionImpl(); // 创建productionService实例
|
|
|
|
|
|
public StorageView(JFrame jFrame) { // StorageView构造方法
|
|
|
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)); // 设置标签前景色
|
|
|
nameLabel = new JLabel("商品名"); // 创建商品名标签并设置文本
|
|
|
nameSearchTF = new JTextField(10); // 创建文本框并设置大小
|
|
|
|
|
|
|
|
|
searchBtn = new JButton(new ImageIcon("static\\icon\\search.png")); // 创建搜索按钮并设置图标
|
|
|
|
|
|
opePanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); // 创建操作面板并设置布局为右对齐的FlowLayout
|
|
|
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")); // 创建删除按钮并设置图标
|
|
|
backBtn =new JButton(new ImageIcon("static\\icon\\back.png")); // 创建返回按钮并设置图标
|
|
|
historyBtn =new JButton(new ImageIcon("static\\icon\\history.png")); // 创建历史按钮并设置图标
|
|
|
backBtn.setVisible(false); // 设置返回按钮为不可见
|
|
|
|
|
|
addBtn.addActionListener(this); // 为添加按钮添加事件监听器
|
|
|
updateBtn.addActionListener(this); // 为更新按钮添加事件监听器
|
|
|
deleteBtn.addActionListener(this); // 为删除按钮添加事件监听器
|
|
|
searchBtn.addActionListener(this); // 为搜索按钮添加事件监听器
|
|
|
backBtn.addActionListener(this); // 为返回按钮添加事件监听器
|
|
|
historyBtn.addActionListener(this); // 为历史按钮添加事件监听器
|
|
|
|
|
|
opePanel.add(addBtn); // 将添加按钮添加到操作面板
|
|
|
opePanel.add(updateBtn); // 将更新按钮添加到操作面板
|
|
|
opePanel.add(deleteBtn); // 将删除按钮添加到操作面板
|
|
|
opePanel.add(backBtn); // 将返回按钮添加到操作面板
|
|
|
opePanel.add(historyBtn); // 将历史按钮添加到操作面板
|
|
|
|
|
|
searchPanel.add(locationLabel); // 将位置标签添加到搜索面板
|
|
|
searchPanel.add(nameLabel); // 将商品名标签添加到搜索面板
|
|
|
searchPanel.add(nameSearchTF); // 将商品名文本框添加到搜索面板
|
|
|
searchPanel.add(searchBtn); // 将搜索按钮添加到搜索面板
|
|
|
|
|
|
toolBarPanel.add(searchPanel,"West"); // 将搜索面板添加到工具栏面板的西边
|
|
|
toolBarPanel.add(opePanel,"East"); // 将操作面板添加到工具栏面板的东边
|
|
|
|
|
|
//中间表格
|
|
|
storageTableModel = new StorageTableModel(); // 创建表格模型实例
|
|
|
storageTableModel.all(); // 调用表格模型的方法以加载所有数据
|
|
|
storageTable = new JTable(storageTableModel); // 创建表格并设置模型
|
|
|
storageTable.setFont(FontUtil.tableFont); // 设置表格字体
|
|
|
storageTable.setRowHeight(50); // 设置表格行高
|
|
|
|
|
|
tableScrollPane = new JScrollPane(storageTable); // 创建滚动面板并添加表格
|
|
|
|
|
|
//下面
|
|
|
bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 创建底部面板并设置布局为左对齐的FlowLayout
|
|
|
countInfoLabel = new JLabel("总共"+storageTableModel.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) { // 如果事件源是添加按钮
|
|
|
//添加数据的对话框
|
|
|
ProductionDialog productionDialog = new ProductionDialog(jFrame); // 创建添加数据的对话框
|
|
|
productionDialog.setVisible(true); // 设置对话框为可见
|
|
|
refreshProduction(); // 刷新生产数据
|
|
|
}else if(updateBtn==source) { // 如果事件源是更新按钮
|
|
|
refreshProduction(); // 刷新生产数据
|
|
|
}else if(historyBtn==source) { // 如果事件源是历史按钮
|
|
|
storageRecord(); // 处理库存记录
|
|
|
}else if(backBtn==source) { // 如果事件源是返回按钮
|
|
|
refreshProduction(); // 刷新生产数据
|
|
|
}
|
|
|
|
|
|
else if(deleteBtn==source) { // 如果事件源是删除按钮
|
|
|
//获取选中记录,删除
|
|
|
int[] rowIndexs = storageTable.getSelectedRows(); // 获取表格中所有选中行的索引
|
|
|
if(rowIndexs.length==0) { // 如果没有选中任何行
|
|
|
JOptionPane.showMessageDialog(this,"请至少选中一条"); // 显示提示信息
|
|
|
return; // 结束方法执行
|
|
|
}
|
|
|
|
|
|
int select=JOptionPane.showConfirmDialog(this,"是否删除选中的"+rowIndexs.length+"条记录","提示",JOptionPane.YES_NO_OPTION); // 显示确认删除的对话框
|
|
|
if(select==JOptionPane.YES_OPTION){ // 如果用户选择是
|
|
|
for(int i=0;i<rowIndexs.length;i++){ // 遍历所有选中的行
|
|
|
String id = (String)storageTable.getValueAt(rowIndexs[i],0); // 获取选中行的ID
|
|
|
if(productionService.deleteProduction(id)==1) { // 尝试删除记录
|
|
|
// 删除成功,不需要额外操作
|
|
|
}else {
|
|
|
JOptionPane.showMessageDialog(this,"删除失败,id="+id,"提示",JOptionPane.ERROR_MESSAGE); // 显示删除失败的提示信息
|
|
|
}
|
|
|
}
|
|
|
|
|
|
JOptionPane.showMessageDialog(this,"删除操作结束","提示",JOptionPane.INFORMATION_MESSAGE); // 显示删除操作结束的提示信息
|
|
|
}
|
|
|
|
|
|
refreshProduction(); // 刷新生产数据
|
|
|
|
|
|
}else if(source==searchBtn){ // 如果事件源是搜索按钮
|
|
|
|
|
|
refreshFindRname(); // 刷新搜索结果
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void refreshFindRname() {
|
|
|
String name = nameSearchTF.getText(); // 获取搜索框中的文本
|
|
|
Production p=new Production(); // 创建Production对象
|
|
|
p.setName(name); // 设置Production对象的名称
|
|
|
storageTableModel = new StorageTableModel(); // 创建新的StorageTableModel对象
|
|
|
storageTableModel.Byname(p); // 根据名称过滤数据
|
|
|
storageTable.setModel(storageTableModel); // 设置表格模型
|
|
|
//同时更新下面的记录数
|
|
|
refreshCount(); // 刷新记录数
|
|
|
hiddinBtn(); // 隐藏或显示按钮
|
|
|
}
|
|
|
|
|
|
public void refreshProduction() {
|
|
|
storageTableModel = new StorageTableModel(); // 创建新的StorageTableModel对象
|
|
|
storageTableModel.all(); // 获取所有数据
|
|
|
storageTable.setModel(storageTableModel); // 设置表格模型
|
|
|
//同时更新下面的记录数
|
|
|
refreshCount(); // 刷新记录数
|
|
|
hiddinBtn(); // 隐藏或显示按钮
|
|
|
}
|
|
|
|
|
|
public void refreshCount(){
|
|
|
bottomPanel.removeAll(); // 清除底部面板上的所有组件
|
|
|
countInfoLabel = new JLabel("总共"+storageTableModel.getRowCount()+"条"); // 创建新的标签显示记录总数
|
|
|
bottomPanel.add(countInfoLabel); // 将记录总数标签添加到底部面板
|
|
|
hiddinBtn(); // 隐藏或显示按钮
|
|
|
}
|
|
|
|
|
|
public void storageRecord() {
|
|
|
StorageRecordTM storageRecordTM= new StorageRecordTM(); // 创建StorageRecordTM对象
|
|
|
storageRecordTM.allStoragrRecord(); // 获取所有库存记录
|
|
|
storageTable.setModel(storageRecordTM); // 设置表格模型为库存记录
|
|
|
//同时更新下面的记录数
|
|
|
bottomPanel.removeAll(); // 清除底部面板上的所有组件
|
|
|
countInfoLabel = new JLabel("总共"+storageRecordTM.getRowCount()+"条"); // 创建新的标签显示记录总数
|
|
|
bottomPanel.add(countInfoLabel); // 将记录总数标签添加到底部面板
|
|
|
backBtn.setVisible(true); // 设置返回按钮为可见
|
|
|
updateBtn.setVisible(false); // 设置更新按钮为不可见
|
|
|
addBtn.setVisible(false); // 设置添加按钮为不可见
|
|
|
historyBtn.setVisible(false); // 设置历史按钮为不可见
|
|
|
deleteBtn.setVisible(false); // 设置删除按钮为不可见
|
|
|
}
|
|
|
|
|
|
public void hiddinBtn() {
|
|
|
backBtn.setVisible(false); // 设置返回按钮为不可见
|
|
|
updateBtn.setVisible(true); // 设置更新按钮为可见
|
|
|
addBtn.setVisible(true); // 设置添加按钮为可见
|
|
|
historyBtn.setVisible(true); // 设置历史按钮为可见
|
|
|
deleteBtn.setVisible(true); // 设置删除按钮为可见
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|