库存列表筛选功能
This commit is contained in:
Binary file not shown.
|
Before Width: | Height: | Size: 5.7 KiB |
@@ -2,7 +2,6 @@ package com.example;
|
|||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
import javax.swing.event.TableModelEvent;
|
import javax.swing.event.TableModelEvent;
|
||||||
import javax.swing.event.TableModelListener;
|
|
||||||
import javax.swing.table.DefaultTableModel;
|
import javax.swing.table.DefaultTableModel;
|
||||||
import javax.swing.table.TableCellRenderer;
|
import javax.swing.table.TableCellRenderer;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
|
|||||||
@@ -5,48 +5,55 @@ import javax.swing.table.DefaultTableModel;
|
|||||||
import javax.swing.table.TableCellRenderer;
|
import javax.swing.table.TableCellRenderer;
|
||||||
import java.awt.*;
|
import java.awt.*;
|
||||||
import java.io.File;
|
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.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 JTable table;
|
||||||
private DefaultTableModel tableModel;
|
private DefaultTableModel tableModel;
|
||||||
private String selectedImagePath = null;
|
private JTextField searchField;
|
||||||
|
private JComboBox<String> categoryComboBox;
|
||||||
|
private JButton resetButton; // 新增:重置按钮
|
||||||
|
|
||||||
public StockProductManagement() {
|
public StockProductManagement() {
|
||||||
super("库存商品信息管理");
|
super("库存商品信息管理");
|
||||||
menuBar = new JMenuBar();
|
|
||||||
setJMenuBar(menuBar);
|
|
||||||
|
|
||||||
menuItemAdd = new JMenuItem("添加商品");
|
setSize(900, 550);
|
||||||
menuItemDelete = new JMenuItem("删除商品");
|
|
||||||
menuBar.add(menuItemAdd);
|
|
||||||
menuBar.add(menuItemDelete);
|
|
||||||
|
|
||||||
menuItemAdd.addActionListener(this);
|
|
||||||
menuItemDelete.addActionListener(this);
|
|
||||||
|
|
||||||
setSize(800, 500);
|
|
||||||
setLocationRelativeTo(null);
|
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 = {"商品照片","商品名称","商品类别","商品数量","进货单价","销售单价","单个利润","销售数量"};
|
String[] columnNames = {"商品照片","商品名称","商品类别","商品数量","进货单价","销售单价","单个利润","销售数量"};
|
||||||
tableModel = new DefaultTableModel(columnNames, 0){
|
tableModel = new DefaultTableModel(columnNames, 0){
|
||||||
@Override
|
@Override
|
||||||
public boolean isCellEditable(int row, int column) {
|
public boolean isCellEditable(int row, int column) {
|
||||||
return column == 7; // 只有销售数量可编辑
|
return column == 7; // 只有销售数量可编辑
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Class<?> getColumnClass(int column) {
|
public Class<?> getColumnClass(int column) {
|
||||||
if(column == 0) return Icon.class; // 图片列
|
if(column == 0) return Icon.class;
|
||||||
return super.getColumnClass(column);
|
return super.getColumnClass(column);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -64,11 +71,11 @@ public class StockProductManagement extends JFrame implements java.awt.event.Act
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// 销售数量编辑事件
|
// 销售数量更新
|
||||||
table.getModel().addTableModelListener(e -> {
|
table.getModel().addTableModelListener(e -> {
|
||||||
int row = e.getFirstRow();
|
int row = e.getFirstRow();
|
||||||
int column = e.getColumn();
|
int column = e.getColumn();
|
||||||
if(column == 7) { // 销售数量列
|
if(column == 7) {
|
||||||
String name = (String)tableModel.getValueAt(row, 1);
|
String name = (String)tableModel.getValueAt(row, 1);
|
||||||
Object value = tableModel.getValueAt(row, column);
|
Object value = tableModel.getValueAt(row, column);
|
||||||
if(value != null && !value.toString().trim().isEmpty()) {
|
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);
|
JScrollPane scrollPane = new JScrollPane(table);
|
||||||
add(scrollPane, BorderLayout.CENTER);
|
add(scrollPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
loadData();
|
// === 事件绑定 ===
|
||||||
|
searchButton.addActionListener(ev -> reloadData());
|
||||||
|
categoryComboBox.addActionListener(ev -> reloadData());
|
||||||
|
resetButton.addActionListener(ev -> { // 重置功能
|
||||||
|
searchField.setText("");
|
||||||
|
categoryComboBox.setSelectedItem("全部");
|
||||||
|
reloadData();
|
||||||
|
});
|
||||||
|
|
||||||
|
reloadData(); // 初次加载
|
||||||
setVisible(true);
|
setVisible(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
public void actionPerformed(java.awt.event.ActionEvent e) {
|
* 按条件加载数据
|
||||||
if(e.getSource() == menuItemAdd){
|
*/
|
||||||
showAddProductWindow();
|
private void reloadData(){
|
||||||
} else if(e.getSource() == menuItemDelete){
|
String keyword = searchField.getText().trim();
|
||||||
deleteSelectedProduct();
|
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();
|
Jdbc jdbc = new Jdbc();
|
||||||
try{
|
try{
|
||||||
ResultSet rs = jdbc.query("SELECT image_path FROM commodity WHERE name='"+name+"'");
|
StringBuilder sql = new StringBuilder("SELECT name,count,purchase_price,selling_price,profit,quantity_sold,type,image_path FROM commodity WHERE 1=1 ");
|
||||||
String imagePath = null;
|
if(!keyword.isEmpty()){
|
||||||
if(rs.next()) imagePath = rs.getString("image_path");
|
sql.append("AND name LIKE '%").append(keyword).append("%' ");
|
||||||
rs.close();
|
}
|
||||||
|
if(category != null && !"全部".equals(category)){
|
||||||
jdbc.update("DELETE FROM commodity WHERE name='"+name+"'");
|
sql.append("AND type='").append(category).append("' ");
|
||||||
jdbc.close();
|
|
||||||
|
|
||||||
if(imagePath != null && !imagePath.isEmpty()){
|
|
||||||
File imgFile = new File("images/"+imagePath);
|
|
||||||
if(imgFile.exists()) imgFile.delete();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
loadData();
|
ResultSet rs = jdbc.query(sql.toString());
|
||||||
}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");
|
|
||||||
tableModel.setRowCount(0);
|
tableModel.setRowCount(0);
|
||||||
|
Set<String> categories = new HashSet<>();
|
||||||
|
|
||||||
while(rs.next()){
|
while(rs.next()){
|
||||||
String name = rs.getString("name");
|
String name = rs.getString("name");
|
||||||
int count = rs.getInt("count");
|
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 type = rs.getString("type");
|
||||||
String imagePath = rs.getString("image_path");
|
String imagePath = rs.getString("image_path");
|
||||||
|
|
||||||
|
categories.add(type);
|
||||||
|
|
||||||
ImageIcon icon = null;
|
ImageIcon icon = null;
|
||||||
if(imagePath!=null && !imagePath.isEmpty()){
|
if(imagePath!=null && !imagePath.isEmpty()){
|
||||||
File imgFile = new File("images/"+imagePath);
|
File imgFile = new File("images/"+imagePath);
|
||||||
@@ -259,6 +154,18 @@ public class StockProductManagement extends JFrame implements java.awt.event.Act
|
|||||||
}
|
}
|
||||||
rs.close();
|
rs.close();
|
||||||
jdbc.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){
|
}catch(Exception e){
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
JOptionPane.showMessageDialog(this,"加载数据失败: "+e.getMessage());
|
JOptionPane.showMessageDialog(this,"加载数据失败: "+e.getMessage());
|
||||||
|
|||||||
Reference in New Issue
Block a user