完整版本

This commit is contained in:
lx
2025-09-24 23:13:03 +08:00
parent 2f386d22a6
commit f9d1241a2d
3 changed files with 178 additions and 57 deletions

View File

@@ -29,7 +29,7 @@ public class Home extends JFrame implements ActionListener {
Home(String username) {
super("超市进销存信息管理系统");
BackgroundPanel bgPanel = new BackgroundPanel("E:\\SuManagement\\images\\a.jpg");
BackgroundPanel bgPanel = new BackgroundPanel("images\\a.jpg");
bgPanel.setLayout(new BorderLayout());
setContentPane(bgPanel);

View File

@@ -6,6 +6,7 @@ import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.io.File;
import java.sql.ResultSet;
@@ -15,6 +16,8 @@ public class SalesProductManagement extends JFrame {
private DefaultTableModel tableModel;
private JTextField totalField;
private JButton checkoutBtn;
private JButton addBtn; // 新增:上架按钮
private JButton removeBtn; // 新增:下架按钮
private String userIdentity; // 当前用户身份
public SalesProductManagement(String identity) {
@@ -27,6 +30,16 @@ public class SalesProductManagement extends JFrame {
JPanel mainPanel = new JPanel(new BorderLayout());
// 顶部按钮面板 - 新增
JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 10));
addBtn = new JButton("上架商品");
removeBtn = new JButton("从界面下架");
// 只有管理员可以上架商品
addBtn.setEnabled("admin".equals(userIdentity));
topPanel.add(addBtn);
topPanel.add(removeBtn);
mainPanel.add(topPanel, BorderLayout.NORTH);
// 表格
String[] columnNames = {"商品图片", "名称", "类别", "单价", "购买数量"};
tableModel = new DefaultTableModel(columnNames, 0) {
@@ -50,6 +63,7 @@ public class SalesProductManagement extends JFrame {
// 图片渲染器
table.getColumnModel().getColumn(0).setCellRenderer(new TableCellRenderer() {
JLabel label = new JLabel();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
label.setHorizontalAlignment(JLabel.CENTER);
@@ -74,10 +88,23 @@ public class SalesProductManagement extends JFrame {
add(mainPanel);
loadData();
setupEventListeners(); // 统一管理事件监听器
setVisible(true);
}
// 新增:统一设置事件监听器
private void setupEventListeners() {
// 结算按钮事件
checkoutBtn.addActionListener(e -> calculateTotal());
// 监听单元格变化,修改后立即更新数据库
// 上架按钮事件
addBtn.addActionListener(e -> addProductFromDatabase());
// 下架按钮事件
removeBtn.addActionListener(e -> removeSelectedProducts());
// 表格模型监听器,处理数据更新
tableModel.addTableModelListener(e -> {
if (e.getType() == TableModelEvent.UPDATE) {
int row = e.getFirstRow();
@@ -99,8 +126,102 @@ public class SalesProductManagement extends JFrame {
}
}
});
}
setVisible(true);
// 新增:从数据库上架商品(选择数据库中未显示的商品添加到界面)
private void addProductFromDatabase() {
try {
Jdbc jdbc = new Jdbc();
// 查询数据库中所有商品
ResultSet rs = jdbc.query("SELECT name, selling_price, quantity_sold, type, image_path FROM commodity");
// 收集已在界面上的商品名称
DefaultListModel<String> availableProducts = new DefaultListModel<>();
while (rs.next()) {
String name = rs.getString("name");
boolean isInTable = false;
// 检查商品是否已在表格中
for (int i = 0; i < tableModel.getRowCount(); i++) {
if (tableModel.getValueAt(i, 1).toString().equals(name)) {
isInTable = true;
break;
}
}
if (!isInTable) {
availableProducts.addElement(name);
}
}
rs.close();
if (availableProducts.size() == 0) {
JOptionPane.showMessageDialog(this, "所有商品已上架!");
jdbc.close();
return;
}
// 显示可选商品列表供选择
JList<String> productList = new JList<>(availableProducts);
productList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
int result = JOptionPane.showConfirmDialog(
this,
new JScrollPane(productList),
"选择要上架的商品",
JOptionPane.OK_CANCEL_OPTION
);
if (result == JOptionPane.OK_OPTION) {
// 上架选中的商品
for (String selectedName : productList.getSelectedValuesList()) {
ResultSet productRs = jdbc.query(
"SELECT name, selling_price, quantity_sold, type, image_path FROM commodity WHERE name='" + selectedName + "'"
);
if (productRs.next()) {
String name = productRs.getString("name");
double price = productRs.getDouble("selling_price");
int quantity = productRs.getInt("quantity_sold");
String type = productRs.getString("type");
String imagePath = productRs.getString("image_path");
ImageIcon icon = null;
if (imagePath != null && !imagePath.isEmpty()) {
File imgFile = new File("images/" + imagePath);
if (imgFile.exists()) {
icon = new ImageIcon(new ImageIcon(imgFile.getAbsolutePath())
.getImage().getScaledInstance(50, 50, Image.SCALE_SMOOTH));
}
}
Object[] row = {icon, name, type, price, quantity};
tableModel.addRow(row);
}
productRs.close();
}
JOptionPane.showMessageDialog(this, "已成功上架选中商品!");
}
jdbc.close();
} catch (Exception e) {
e.printStackTrace();
JOptionPane.showMessageDialog(this, "上架商品失败: " + e.getMessage());
}
}
// 新增:从界面下架商品(仅从当前界面移除,不删除数据库记录)
private void removeSelectedProducts() {
int[] selectedRows = table.getSelectedRows();
if (selectedRows.length == 0) {
JOptionPane.showMessageDialog(this, "请先选择要下架的商品!");
return;
}
// 从后往前删除,避免索引混乱
for (int i = selectedRows.length - 1; i >= 0; i--) {
tableModel.removeRow(selectedRows[i]);
}
JOptionPane.showMessageDialog(this, "已成功下架 " + selectedRows.length + " 件商品!");
calculateTotal(); // 更新总金额
}
private void loadData() {