This commit is contained in:
fgl
2025-09-19 14:33:37 +08:00
parent 43024e6b3e
commit 76a7f5df6d

View File

@@ -9,78 +9,89 @@ import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
public class InventoryProductManagement extends JFrame implements ActionListener { public class InventoryProductManagement extends JFrame implements ActionListener {
JMenuBar menuBar; JMenuBar menuBar;//创建主菜单栏容器
JMenu menu; JMenu menu;//创建顶级菜单项“菜单”
JMenuItem menuItem1,menuItem2,menuItem3; JMenuItem menuItem1,menuItem2,menuItem3;//创建菜单子项(添加/删除商品)
private JTable table; private JTable table;//表格组件,用于展示商品数据
private DefaultTableModel tableModel; private DefaultTableModel tableModel;//表格数据模型
InventoryProductManagement() { InventoryProductManagement() {
super("进货信息管理"); super("进货信息管理");//设置窗口标题
menuBar = new JMenuBar(); menuBar = new JMenuBar();//实例化菜单栏
setJMenuBar(menuBar); setJMenuBar(menuBar);//将菜单栏添加到窗体顶部
menu = new JMenu("菜单"); menu = new JMenu("菜单");//创建顶级菜单“菜单”
menuBar.add(menu); menuBar.add(menu);//将菜单添加到菜单栏
//创建菜单项
menuItem1 = new JMenuItem("添加商品"); menuItem1 = new JMenuItem("添加商品");
menuItem2 = new JMenuItem("删除商品"); menuItem2 = new JMenuItem("删除商品");
menuItem3 = new JMenuItem("帮助");
//添加菜单项到菜单
menu.add(menuItem1); menu.add(menuItem1);
menu.add(menuItem2); menu.add(menuItem2);
menu.add(menuItem3);
// menu.add(menuItem3); // menu.add(menuItem3);
//注册菜单项点击监听器this表示当前类处理事件
menuItem1.addActionListener(this); menuItem1.addActionListener(this);
menuItem2.addActionListener(this); menuItem2.addActionListener(this);
menuItem3.addActionListener(this);
// menuItem3.addActionListener(this); // menuItem3.addActionListener(this);
setSize(500,500); setSize(500,500);//设置窗口大小
setLocationRelativeTo(null); setLocationRelativeTo(null);//窗口居中显示
// 表 // 表格初始化
String[] columnNames = {"商品名称", "商品数量", "进货单价"}; String[] columnNames = {"商品名称(种类)", "商品数量(件)", "进货单价(元)"};//定义表头,此处更改表头文字展示
tableModel = new DefaultTableModel(columnNames, 0); tableModel = new DefaultTableModel(columnNames, 0);//创建空表格模型
table = new JTable(tableModel); table = new JTable(tableModel);//基于模型创建表格
// 滚动面板 // 添加带滚动条的表格面板到窗口中央
JScrollPane scrollPane = new JScrollPane(table); JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane, BorderLayout.CENTER); add(scrollPane, BorderLayout.CENTER);
// 初始化加载数据库数据 // 初始化加载数据库数据
loadData(); loadData();//加载数据库数据到表格
setVisible(true); setVisible(true);//显示窗口
} }
private void loadData() { private void loadData() {
Jdbc jdbc = new Jdbc(); Jdbc jdbc = new Jdbc();//创建数据库连接对象
try { try {
//执行SQL查询获取商品名称/库存/进阶/售价/利润/销量)
ResultSet rs = jdbc.query("SELECT name,count,purchase_price,selling_price,profit,quantity_sold FROM commodity"); ResultSet rs = jdbc.query("SELECT name,count,purchase_price,selling_price,profit,quantity_sold FROM commodity");
// 清空旧数据 // 清空旧数据
tableModel.setRowCount(0); tableModel.setRowCount(0);//清空表格现有数据
// 填充新数据 // 填充新数据(遍历查询结果)
while (rs.next()) { while (rs.next()) {
//构建表格行数据(只显示名称/库存/进阶)
Object[] row = { Object[] row = {
rs.getString("name"), rs.getString("name"),
rs.getInt("count"), rs.getInt("count"),
rs.getDouble("purchase_price"), rs.getDouble("purchase_price"),
}; };
tableModel.addRow(row); tableModel.addRow(row);//添加数据行到表格
} }
rs.close(); rs.close();//关闭结果集
jdbc.close(); jdbc.close();//关闭数据库连接
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
JOptionPane.showMessageDialog(this, "加载数据失败: " + e.getMessage()); JOptionPane.showMessageDialog(this, "加载数据失败: " + e.getMessage());//弹出错误提示
} }
} }
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
if (e.getSource() == menuItem1) { if (e.getSource() == menuItem1) {
JFrame frame = new JFrame(); JFrame frame = new JFrame();//创建添加窗口
frame.setTitle("添加商品"); frame.setTitle("添加商品");
frame.setLayout(new GridLayout(4, 2)); frame.setLayout(new GridLayout(4, 2));//四行两列网格布局
//创建输入组件
JLabel nameLabel = new JLabel("商品名称:"); JLabel nameLabel = new JLabel("商品名称:");
JTextField nameField = new JTextField(); JTextField nameField = new JTextField();
JLabel countLabel = new JLabel("商品数量:"); JLabel countLabel = new JLabel("商品数量:");
JTextField countField = new JTextField(); JTextField countField = new JTextField();
JLabel purchasePriceLabel = new JLabel("进货单价:"); JLabel purchasePriceLabel = new JLabel("进货单价:");
JTextField purchasePriceField = new JTextField(); JTextField purchasePriceField = new JTextField();
//添加按钮和取消按钮
JButton addButton = new JButton("添加"); JButton addButton = new JButton("添加");
JButton cancelButton = new JButton("取消"); JButton cancelButton = new JButton("取消");
//将组件添加到窗口
frame.add(nameLabel); frame.add(nameLabel);
frame.add(nameField); frame.add(nameField);
frame.add(countLabel); frame.add(countLabel);
@@ -89,28 +100,30 @@ public class InventoryProductManagement extends JFrame implements ActionListener
frame.add(purchasePriceField); frame.add(purchasePriceField);
frame.add(addButton); frame.add(addButton);
frame.add(cancelButton); frame.add(cancelButton);
//添加按钮事件
addButton.addActionListener(new ActionListener() { addButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
//获取输入值
String name = nameField.getText(); String name = nameField.getText();
int count = Integer.parseInt(countField.getText()); int count = Integer.parseInt(countField.getText());
double purchasePrice = Double.parseDouble(purchasePriceField.getText()); double purchasePrice = Double.parseDouble(purchasePriceField.getText());
// 调用数据库添加方法 // 调用数据库添加方法 执行SQL插入初始化销量为0
Jdbc jdbc = new Jdbc(); Jdbc jdbc = new Jdbc();
try { try {
jdbc.update("INSERT INTO commodity (name, count, purchase_price, quantity_sold) VALUES ('" + name + "', " + count + ", " + purchasePrice + ", " + 0 + ");"); jdbc.update("INSERT INTO commodity (name, count, purchase_price, quantity_sold) VALUES ('" + name + "', " + count + ", " + purchasePrice + ", " + 0 + ");");
jdbc.close(); jdbc.close();
// 刷新数据 // 刷新数据
loadData(); loadData();//刷新表格数据
// 关闭弹窗 // 关闭弹窗
frame.dispose(); frame.dispose();//关闭添加窗口
} catch (SQLException ex) { } catch (SQLException ex) {
ex.printStackTrace(); ex.printStackTrace();
JOptionPane.showMessageDialog(frame, "添加商品失败: " + ex.getMessage()); JOptionPane.showMessageDialog(frame, "添加商品失败: " + ex.getMessage());
} }
} }
}); });
cancelButton.addActionListener(new ActionListener() { cancelButton.addActionListener(new ActionListener() {//取消按钮事件
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
frame.dispose(); frame.dispose();
@@ -122,7 +135,7 @@ public class InventoryProductManagement extends JFrame implements ActionListener
frame.setVisible(true); frame.setVisible(true);
} }
else if (e.getSource() == menuItem2) { else if (e.getSource() == menuItem2) {
JFrame frame = new JFrame(); JFrame frame = new JFrame();//创建删除窗口
frame.setTitle("删除商品"); frame.setTitle("删除商品");
frame.setLayout(new GridLayout(2, 2)); frame.setLayout(new GridLayout(2, 2));
JLabel nameLabel = new JLabel("商品名称:"); JLabel nameLabel = new JLabel("商品名称:");
@@ -133,16 +146,18 @@ public class InventoryProductManagement extends JFrame implements ActionListener
frame.add(nameField); frame.add(nameField);
frame.add(deleteButton); frame.add(deleteButton);
frame.add(cancelButton); frame.add(cancelButton);
//删除按钮事件
deleteButton.addActionListener(new ActionListener() { deleteButton.addActionListener(new ActionListener() {
@Override @Override
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {
String name = nameField.getText(); String name = nameField.getText();//获取要删除的商品名
// 调用数据库删除方法 // 调用数据库删除方法
Jdbc jdbc = new Jdbc(); Jdbc jdbc = new Jdbc();
try { try {
//执行SQL删除
jdbc.update("DELETE FROM commodity WHERE name='" + name + "'"); jdbc.update("DELETE FROM commodity WHERE name='" + name + "'");
jdbc.close(); jdbc.close();
// 刷新数据 // 刷新表格
loadData(); loadData();
// 关闭弹窗 // 关闭弹窗
frame.dispose(); frame.dispose();
@@ -165,6 +180,6 @@ public class InventoryProductManagement extends JFrame implements ActionListener
} }
} }
public static void main(String[] args) { public static void main(String[] args) {
new InventoryProductManagement(); new InventoryProductManagement();//启动应用程序
} }
} }