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