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/SupplierView.java

180 lines
10 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 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.SupplierInfService; // 导入供应商信息服务的接口
import com.lingnan.supermarket.dao.impl.SupplierInfImpl; // 导入供应商信息服务的实现类
import com.lingnan.supermarket.dialog.SupplierInfDialog; // 导入供应商信息对话框类
import com.lingnan.supermarket.dto.SupplierInf; // 导入供应商信息数据传输对象
import com.lingnan.supermarket.table.SupplierTableModel; // 导入供应商信息表格模型类
import com.lingnan.supermarket.utils.FontUtil; // 导入字体工具类
public class SupplierView extends JPanel implements ActionListener{ // 定义一个公共类SupplierView继承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; // 按钮的私有成员变量,用于添加、更新、删除操作
//中间
private JScrollPane tableScrollPane; // 带滚动条的表格面板的私有成员变量
private JTable supplierTable; // 表格的私有成员变量,用于显示供应商信息
//下面
private JPanel bottomPanel; // 底部面板的私有成员变量
private JLabel countInfoLabel; // 标签的私有成员变量,用于显示记录总数信息
private SupplierTableModel supplierTableModel; // 供应商信息表格模型的私有成员变量
private JFrame jFrame; // 窗口的私有成员变量
private SupplierInfService supplierInfService = new SupplierInfImpl(); // 供应商信息服务的实例化对象
public SupplierView(JFrame jFrame) { // SupplierView类的构造方法接收一个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)); // 设置标签的前景色(文字颜色)
nameLabel = new JLabel("公司名称"); // 创建标签并设置文本为公司名称
nameSearchTF = new JTextField(10); // 创建文本框并设置大小为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")); // 创建更新按钮
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")); // 创建删除按钮并设置图标
addBtn.addActionListener(this); // 为添加按钮添加事件监听器
updateBtn.addActionListener(this); // 为更新按钮添加事件监听器
deleteBtn.addActionListener(this); // 为删除按钮添加事件监听器
searchBtn.addActionListener(this); // 为搜索按钮添加事件监听器
opePanel.add(addBtn); // 将添加按钮添加到操作面板
opePanel.add(updateBtn); // 将更新按钮添加到操作面板
opePanel.add(deleteBtn); // 将删除按钮添加到操作面板
searchPanel.add(locationLabel); // 将位置标签添加到搜索面板
searchPanel.add(nameLabel); // 将公司名称标签添加到搜索面板
searchPanel.add(nameSearchTF); // 将公司名称文本框添加到搜索面板
searchPanel.add(searchBtn); // 将搜索按钮添加到搜索面板
toolBarPanel.add(searchPanel,"West"); // 将搜索面板添加到工具栏面板的西边
toolBarPanel.add(opePanel,"East"); // 将操作面板添加到工具栏面板的东边
//中间表格
supplierTableModel = new SupplierTableModel(); // 创建供应商表格模型对象
supplierTableModel.all(); // 调用表格模型的方法以加载所有数据
supplierTable = new JTable(supplierTableModel); // 创建表格并设置模型
supplierTable.setFont(FontUtil.tableFont); // 设置表格字体
supplierTable.setRowHeight(50); // 设置表格行高
tableScrollPane = new JScrollPane(supplierTable); // 创建带滚动条的容器并添加表格
//下面
bottomPanel = new JPanel(new FlowLayout(FlowLayout.LEFT)); // 创建底部面板并设置布局为左对齐的FlowLayout
countInfoLabel = new JLabel("总共"+supplierTableModel.getRowCount()+"条"); // 创建标签并设置显示记录总数的文本
bottomPanel.add(countInfoLabel); // 将记录总数标签添加到底部面板
this.add(toolBarPanel,"North"); // 将工具栏面板添加到主面板的北边
this.add(tableScrollPane,"Center"); // 将带滚动条的表格容器添加到主面板的中间
this.add(bottomPanel,"South"); // 将底部面板添加到主面板的南边
setVisible(true); // 设置主面板可见(通常不需要,因为主面板会在添加到窗口时自动设置为可见)
}
@Override
public void actionPerformed(ActionEvent e) { // 实现ActionListener接口的actionPerformed方法
Object source = e.getSource(); // 获取事件源
if(addBtn==source) { // 如果事件源是添加按钮
SupplierInfDialog supplierInfDialog = new SupplierInfDialog(jFrame, null); // 创建供应商信息对话框
supplierInfDialog.setVisible(true); // 设置对话框可见
refreshSupplier(); // 刷新供应商信息
}else if(updateBtn==source) { // 如果事件源是更新按钮
refreshSupplier(); // 刷新供应商信息
}else if(deleteBtn==source) { // 如果事件源是删除按钮
//获取选中记录
int rowIndex = supplierTable.getSelectedRow(); // 获取表格中选中的行索引
if(rowIndex==-1) { // 如果没有行被选中
JOptionPane.showMessageDialog(this,"请选中一条"); // 显示提示信息
return; // 结束方法执行
}
int id = (Integer)supplierTable.getValueAt(rowIndex,0); // 获取选中行的ID
int select=JOptionPane.showConfirmDialog(this,"是否删除id="+id,"提示",JOptionPane.YES_NO_OPTION); // 显示确认删除的对话框并获取用户的选择
if(select==JOptionPane.YES_OPTION){ // 如果用户在确认对话框中选择“是”
if(supplierInfService.deleteSupplierInf(id)==1) { // 尝试删除供应商信息如果成功返回1
JOptionPane.showMessageDialog(this,"删除成功","提示",JOptionPane.INFORMATION_MESSAGE); // 显示删除成功的消息
refreshSupplier(); // 刷新供应商信息
}else {
JOptionPane.showMessageDialog(this,"删除失败","提示",JOptionPane.ERROR_MESSAGE); // 显示删除失败的消息
}
}
}else if(searchBtn==source){ // 如果事件源是搜索按钮
System.out.println("搜索"); // 打印搜索操作信息到控制台
refreshFindRname(); // 调用方法刷新搜索结果
}
}
// 根据名称刷新供应商信息的方法
public void refreshFindRname() {
String name = nameSearchTF.getText(); // 获取搜索文本框中的文本
SupplierInf supplierInf = new SupplierInf(); // 创建供应商信息对象
supplierInf.setName(name); // 设置供应商名称为搜索文本
supplierTableModel = new SupplierTableModel(); // 创建新的供应商表格模型
supplierTableModel.Byname(supplierInf); // 调用表格模型的方法按名称查询供应商信息
supplierTable.setModel(supplierTableModel); // 更新表格模型以显示查询结果
refreshCount(); // 刷新记录总数
}
// 刷新供应商信息的方法
public void refreshSupplier() {
supplierTableModel = new SupplierTableModel(); // 创建新的供应商表格模型
supplierTableModel.all(); // 加载所有供应商信息
supplierTable.setModel(supplierTableModel); // 更新表格模型以显示所有供应商信息
refreshCount(); // 刷新记录总数
}
// 刷新记录总数的方法
public void refreshCount(){
bottomPanel.removeAll(); // 清除底部面板上的所有组件
countInfoLabel = new JLabel("总共"+supplierTableModel.getRowCount()+"条"); // 创建新的标签显示记录总数
bottomPanel.add(countInfoLabel); // 将记录总数标签添加到底部面板
}
}