库存列表筛选功能

This commit is contained in:
lu
2025-09-25 17:08:07 +08:00
parent f9d1241a2d
commit d98573b777
3 changed files with 71 additions and 165 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.7 KiB

View File

@@ -2,7 +2,6 @@ package com.example;
import javax.swing.*;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;

View File

@@ -5,48 +5,55 @@ import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.sql.ResultSet;
import java.util.HashSet;
import java.util.Set;
public class StockProductManagement extends JFrame implements java.awt.event.ActionListener {
public class StockProductManagement extends JFrame {
JMenuBar menuBar;
JMenuItem menuItemAdd, menuItemDelete;
private JTable table;
private DefaultTableModel tableModel;
private String selectedImagePath = null;
private JTextField searchField;
private JComboBox<String> categoryComboBox;
private JButton resetButton; // 新增:重置按钮
public StockProductManagement() {
super("库存商品信息管理");
menuBar = new JMenuBar();
setJMenuBar(menuBar);
menuItemAdd = new JMenuItem("添加商品");
menuItemDelete = new JMenuItem("删除商品");
menuBar.add(menuItemAdd);
menuBar.add(menuItemDelete);
menuItemAdd.addActionListener(this);
menuItemDelete.addActionListener(this);
setSize(800, 500);
setSize(900, 550);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// 表头增加“商品类别”列
// === 顶部工具面板 ===
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
topPanel.add(new JLabel("搜索商品名称:"));
searchField = new JTextField(15);
topPanel.add(searchField);
JButton searchButton = new JButton("搜索");
topPanel.add(searchButton);
topPanel.add(new JLabel("筛选类别:"));
categoryComboBox = new JComboBox<>();
categoryComboBox.addItem("全部"); // 初始
topPanel.add(categoryComboBox);
resetButton = new JButton("重置"); // 新增
topPanel.add(resetButton);
add(topPanel, BorderLayout.NORTH);
// === 表格 ===
String[] columnNames = {"商品照片","商品名称","商品类别","商品数量","进货单价","销售单价","单个利润","销售数量"};
tableModel = new DefaultTableModel(columnNames, 0){
@Override
public boolean isCellEditable(int row, int column) {
return column == 7; // 只有销售数量可编辑
}
@Override
public Class<?> getColumnClass(int column) {
if(column == 0) return Icon.class; // 图片列
if(column == 0) return Icon.class;
return super.getColumnClass(column);
}
};
@@ -64,11 +71,11 @@ public class StockProductManagement extends JFrame implements java.awt.event.Act
}
});
// 销售数量编辑事件
// 销售数量更新
table.getModel().addTableModelListener(e -> {
int row = e.getFirstRow();
int column = e.getColumn();
if(column == 7) { // 销售数量列
if(column == 7) {
String name = (String)tableModel.getValueAt(row, 1);
Object value = tableModel.getValueAt(row, column);
if(value != null && !value.toString().trim().isEmpty()) {
@@ -87,154 +94,40 @@ public class StockProductManagement extends JFrame implements java.awt.event.Act
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER);
loadData();
// === 事件绑定 ===
searchButton.addActionListener(ev -> reloadData());
categoryComboBox.addActionListener(ev -> reloadData());
resetButton.addActionListener(ev -> { // 重置功能
searchField.setText("");
categoryComboBox.setSelectedItem("全部");
reloadData();
});
reloadData(); // 初次加载
setVisible(true);
}
@Override
public void actionPerformed(java.awt.event.ActionEvent e) {
if(e.getSource() == menuItemAdd){
showAddProductWindow();
} else if(e.getSource() == menuItemDelete){
deleteSelectedProduct();
}
}
/**
* 按条件加载数据
*/
private void reloadData(){
String keyword = searchField.getText().trim();
String category = (String) categoryComboBox.getSelectedItem();
private void showAddProductWindow(){
JFrame frame = new JFrame("添加商品");
frame.setLayout(new GridLayout(7,2));
JLabel nameLabel = new JLabel("商品名称:");
JTextField nameField = new JTextField();
JLabel countLabel = new JLabel("商品数量:");
JTextField countField = new JTextField();
JLabel purchasePriceLabel = new JLabel("进货单价:");
JTextField purchasePriceField = new JTextField();
JLabel sellingPriceLabel = new JLabel("销售单价:");
JTextField sellingPriceField = new JTextField();
JLabel typeLabel = new JLabel("商品类别:");
String[] categories = {"食品","生活用品","娱乐物品","学习物品","其他物品"};
JComboBox<String> typeComboBox = new JComboBox<>(categories);
JLabel imageLabel = new JLabel("商品图片:");
JButton chooseImageButton = new JButton("选择图片");
JButton addButton = new JButton("添加");
JButton cancelButton = new JButton("取消");
frame.add(nameLabel); frame.add(nameField);
frame.add(countLabel); frame.add(countField);
frame.add(purchasePriceLabel); frame.add(purchasePriceField);
frame.add(sellingPriceLabel); frame.add(sellingPriceField);
frame.add(typeLabel); frame.add(typeComboBox);
frame.add(imageLabel); frame.add(chooseImageButton);
frame.add(addButton); frame.add(cancelButton);
chooseImageButton.addActionListener(ev -> {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("选择商品图片");
fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
int result = fileChooser.showOpenDialog(frame);
if(result == JFileChooser.APPROVE_OPTION){
File selectedFile = fileChooser.getSelectedFile();
selectedImagePath = selectedFile.getAbsolutePath();
JOptionPane.showMessageDialog(frame, "已选择图片: " + selectedFile.getName());
}
});
addButton.addActionListener(ev -> {
String name = nameField.getText();
int count;
double purchasePrice, sellingPrice;
String type = (String) typeComboBox.getSelectedItem();
try {
count = Integer.parseInt(countField.getText());
purchasePrice = Double.parseDouble(purchasePriceField.getText());
sellingPrice = Double.parseDouble(sellingPriceField.getText());
} catch(NumberFormatException ex){
JOptionPane.showMessageDialog(frame, "数量和价格必须为数字");
return;
}
String savedImageFileName = null;
if(selectedImagePath != null){
try{
File imageDir = new File("images");
if(!imageDir.exists()) imageDir.mkdir();
String extension = selectedImagePath.substring(selectedImagePath.lastIndexOf("."));
savedImageFileName = name + System.currentTimeMillis() + extension;
Path targetPath = Paths.get("images", savedImageFileName);
Files.copy(Paths.get(selectedImagePath), targetPath, StandardCopyOption.REPLACE_EXISTING);
}catch(IOException ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(frame, "图片保存失败: "+ex.getMessage());
return;
}
}
Jdbc jdbc = new Jdbc();
try{
String sql = "INSERT INTO commodity (name, count, purchase_price, selling_price, profit, quantity_sold, type, image_path) " +
"VALUES ('"+name+"', "+count+", "+purchasePrice+", "+sellingPrice+", "+(sellingPrice-purchasePrice)+", 0, '"+type+"', '"+(savedImageFileName!=null?savedImageFileName:"")+"')";
jdbc.update(sql);
jdbc.close();
loadData();
frame.dispose();
}catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(frame, "添加商品失败: "+ex.getMessage());
}
});
cancelButton.addActionListener(ev -> frame.dispose());
frame.setSize(450,400);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
private void deleteSelectedProduct() {
int selectedRow = table.getSelectedRow();
if(selectedRow == -1){
JOptionPane.showMessageDialog(this, "请先选中要删除的商品行");
return;
}
String name = (String)tableModel.getValueAt(selectedRow,1); // 商品名称
int confirm = JOptionPane.showConfirmDialog(this,"确定删除商品 \"" + name + "\" 吗?","确认删除",JOptionPane.YES_NO_OPTION);
if(confirm != JOptionPane.YES_OPTION) return;
// 删除数据库和图片
Jdbc jdbc = new Jdbc();
try{
ResultSet rs = jdbc.query("SELECT image_path FROM commodity WHERE name='"+name+"'");
String imagePath = null;
if(rs.next()) imagePath = rs.getString("image_path");
rs.close();
jdbc.update("DELETE FROM commodity WHERE name='"+name+"'");
jdbc.close();
if(imagePath != null && !imagePath.isEmpty()){
File imgFile = new File("images/"+imagePath);
if(imgFile.exists()) imgFile.delete();
StringBuilder sql = new StringBuilder("SELECT name,count,purchase_price,selling_price,profit,quantity_sold,type,image_path FROM commodity WHERE 1=1 ");
if(!keyword.isEmpty()){
sql.append("AND name LIKE '%").append(keyword).append("%' ");
}
if(category != null && !"全部".equals(category)){
sql.append("AND type='").append(category).append("' ");
}
loadData();
}catch(Exception ex){
ex.printStackTrace();
JOptionPane.showMessageDialog(this, "删除商品失败: "+ex.getMessage());
}
}
private void loadData(){
Jdbc jdbc = new Jdbc();
try{
ResultSet rs = jdbc.query("SELECT name,count,purchase_price,selling_price,profit,quantity_sold,type,image_path FROM commodity");
ResultSet rs = jdbc.query(sql.toString());
tableModel.setRowCount(0);
Set<String> categories = new HashSet<>();
while(rs.next()){
String name = rs.getString("name");
int count = rs.getInt("count");
@@ -245,6 +138,8 @@ public class StockProductManagement extends JFrame implements java.awt.event.Act
String type = rs.getString("type");
String imagePath = rs.getString("image_path");
categories.add(type);
ImageIcon icon = null;
if(imagePath!=null && !imagePath.isEmpty()){
File imgFile = new File("images/"+imagePath);
@@ -259,6 +154,18 @@ public class StockProductManagement extends JFrame implements java.awt.event.Act
}
rs.close();
jdbc.close();
// 更新类别下拉框
String selected = (String) categoryComboBox.getSelectedItem();
categoryComboBox.removeAllItems();
categoryComboBox.addItem("全部");
for(String c : categories){
categoryComboBox.addItem(c);
}
if(selected != null && !selected.isEmpty()){
categoryComboBox.setSelectedItem(selected);
}
}catch(Exception e){
e.printStackTrace();
JOptionPane.showMessageDialog(this,"加载数据失败: "+e.getMessage());