BIN
images/OIP.webp
Normal file
BIN
images/OIP.webp
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 10 KiB |
BIN
images/a.jpg
Normal file
BIN
images/a.jpg
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 649 KiB |
@@ -1,31 +1,63 @@
|
|||||||
package com.example;
|
package com.example;
|
||||||
|
|
||||||
import javax.swing.*;
|
import javax.swing.*;
|
||||||
|
import java.awt.*;
|
||||||
import java.awt.event.ActionEvent;
|
import java.awt.event.ActionEvent;
|
||||||
import java.awt.event.ActionListener;
|
import java.awt.event.ActionListener;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
public class Home extends JFrame implements ActionListener {
|
public class Home extends JFrame implements ActionListener {
|
||||||
|
|
||||||
JMenuBar menuBar;
|
JMenuBar menuBar;
|
||||||
JMenu menu;
|
|
||||||
// 库存菜单 进货菜单 销售菜单
|
// 库存菜单 进货菜单 销售菜单
|
||||||
JMenuItem StockMenuItem,InventoryMenuItem,SalesMenuItem;
|
JMenuItem StockMenu,InventoryMenu,SalesMenu,UserMenu;
|
||||||
|
class BackgroundPanel extends JPanel {
|
||||||
|
private Image bg;
|
||||||
|
public BackgroundPanel(String path) {
|
||||||
|
bg = new ImageIcon(path).getImage();
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
protected void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
if (bg != null) {
|
||||||
|
g.drawImage(bg, 0, 0, getWidth(), getHeight(), this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Home() {
|
Home(String username) {
|
||||||
super("超市进销存信息管理系统");
|
super("超市进销存信息管理系统");
|
||||||
|
BackgroundPanel bgPanel = new BackgroundPanel("E:\\SuManagement\\images\\a.jpg");
|
||||||
|
bgPanel.setLayout(new BorderLayout());
|
||||||
|
setContentPane(bgPanel);
|
||||||
|
|
||||||
menuBar = new JMenuBar();
|
menuBar = new JMenuBar();
|
||||||
menu = new JMenu("菜单");
|
StockMenu = new JMenuItem("库存信息管理");
|
||||||
StockMenuItem = new JMenuItem("库存信息管理");
|
InventoryMenu = new JMenuItem("进货信息管理");
|
||||||
InventoryMenuItem = new JMenuItem("进货信息管理");
|
SalesMenu = new JMenuItem("销售信息管理");
|
||||||
SalesMenuItem = new JMenuItem("销售信息管理");
|
menuBar.add(StockMenu);
|
||||||
menu.add(StockMenuItem);
|
menuBar.add(InventoryMenu);
|
||||||
menu.add(InventoryMenuItem);
|
menuBar.add(SalesMenu);
|
||||||
menu.add(SalesMenuItem);
|
|
||||||
menuBar.add(menu);
|
|
||||||
setJMenuBar(menuBar);
|
setJMenuBar(menuBar);
|
||||||
StockMenuItem.addActionListener(this);
|
StockMenu.addActionListener(this);
|
||||||
InventoryMenuItem.addActionListener(this);
|
InventoryMenu.addActionListener(this);
|
||||||
SalesMenuItem.addActionListener(this);
|
SalesMenu.addActionListener(this);
|
||||||
|
Jdbc jdbc = new Jdbc();
|
||||||
|
String sql = "select * from user where username='"+username+"'";
|
||||||
|
try {
|
||||||
|
ResultSet rs = jdbc.query(sql);
|
||||||
|
if(rs.next()){
|
||||||
|
username = rs.getString("identity");
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
if(username.equals("admin")){
|
||||||
|
UserMenu = new JMenuItem("用户管理");
|
||||||
|
menuBar.add(UserMenu);
|
||||||
|
UserMenu.addActionListener(this);
|
||||||
|
}
|
||||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
setSize(500,500);
|
setSize(500,500);
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
@@ -33,14 +65,20 @@ public class Home extends JFrame implements ActionListener {
|
|||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
public void actionPerformed(ActionEvent e) {
|
public void actionPerformed(ActionEvent e) {
|
||||||
if(e.getSource()==StockMenuItem){
|
if(e.getSource()==StockMenu){
|
||||||
new StockProductManagement();
|
new StockProductManagement();
|
||||||
}
|
}
|
||||||
if(e.getSource()==InventoryMenuItem){
|
if(e.getSource()==InventoryMenu){
|
||||||
new InventoryProductManagement();
|
new InventoryProductManagement();
|
||||||
}
|
}
|
||||||
if(e.getSource()==SalesMenuItem){
|
if(e.getSource()==SalesMenu){
|
||||||
new SalesProductManagement();
|
new SalesProductManagement();
|
||||||
}
|
}
|
||||||
|
if(e.getSource()==UserMenu){
|
||||||
|
new UserManagement();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new Home("admin");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,8 @@ public class InventoryProductManagement extends JFrame implements ActionListener
|
|||||||
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("帮助");
|
menuItem3 = new JMenuItem("帮助");
|
||||||
//添加菜单项到菜单
|
//添加菜单项到菜单
|
||||||
menu.add(menuItem1);
|
menu.add(menuItem1);
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ public class Main extends JFrame implements ActionListener {
|
|||||||
ResultSet rs = jdbc.query(sql);
|
ResultSet rs = jdbc.query(sql);
|
||||||
if(rs.next()&&tf3.getText().equals(jb3.getText())) {
|
if(rs.next()&&tf3.getText().equals(jb3.getText())) {
|
||||||
JOptionPane.showMessageDialog(null, "登录成功");
|
JOptionPane.showMessageDialog(null, "登录成功");
|
||||||
new Home();
|
new Home(rs.getString("username"));
|
||||||
this.setVisible(false);
|
this.setVisible(false);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|||||||
199
src/main/java/com/example/UserManagement.java
Normal file
199
src/main/java/com/example/UserManagement.java
Normal file
@@ -0,0 +1,199 @@
|
|||||||
|
package com.example;
|
||||||
|
|
||||||
|
import javax.swing.*;
|
||||||
|
import javax.swing.table.DefaultTableModel;
|
||||||
|
import java.awt.*;
|
||||||
|
import java.awt.event.ActionEvent;
|
||||||
|
import java.awt.event.ActionListener;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public class UserManagement extends JFrame implements ActionListener {
|
||||||
|
|
||||||
|
JMenuBar menuBar;
|
||||||
|
JMenuItem addItem, deleteItem, updateItem, refreshItem;
|
||||||
|
private JTable table;
|
||||||
|
private DefaultTableModel tableModel;
|
||||||
|
|
||||||
|
public UserManagement() {
|
||||||
|
super("用户信息管理");
|
||||||
|
|
||||||
|
// 菜单栏
|
||||||
|
menuBar = new JMenuBar();
|
||||||
|
setJMenuBar(menuBar);
|
||||||
|
|
||||||
|
addItem = new JMenuItem("添加用户");
|
||||||
|
deleteItem = new JMenuItem("删除用户");
|
||||||
|
updateItem = new JMenuItem("修改用户");
|
||||||
|
refreshItem = new JMenuItem("刷新数据");
|
||||||
|
|
||||||
|
menuBar.add(addItem);
|
||||||
|
menuBar.add(deleteItem);
|
||||||
|
menuBar.add(updateItem);
|
||||||
|
menuBar.add(refreshItem);
|
||||||
|
|
||||||
|
addItem.addActionListener(this);
|
||||||
|
deleteItem.addActionListener(this);
|
||||||
|
updateItem.addActionListener(this);
|
||||||
|
refreshItem.addActionListener(this);
|
||||||
|
|
||||||
|
// 表格模型
|
||||||
|
String[] columnNames = {"用户名", "密码", "身份"};
|
||||||
|
tableModel = new DefaultTableModel(columnNames, 0) {
|
||||||
|
@Override
|
||||||
|
public boolean isCellEditable(int row, int column) {
|
||||||
|
return false; // 不直接在表格里改
|
||||||
|
}
|
||||||
|
};
|
||||||
|
table = new JTable(tableModel);
|
||||||
|
|
||||||
|
JScrollPane scrollPane = new JScrollPane(table);
|
||||||
|
add(scrollPane, BorderLayout.CENTER);
|
||||||
|
|
||||||
|
setSize(600, 400);
|
||||||
|
setLocationRelativeTo(null);
|
||||||
|
|
||||||
|
// 初始加载
|
||||||
|
loadData();
|
||||||
|
setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 加载 identity=user 的数据
|
||||||
|
private void loadData() {
|
||||||
|
Jdbc jdbc = new Jdbc();
|
||||||
|
try {
|
||||||
|
ResultSet rs = jdbc.query("SELECT username,password,identity FROM user WHERE identity='user'");
|
||||||
|
tableModel.setRowCount(0);
|
||||||
|
while (rs.next()) {
|
||||||
|
Object[] row = {
|
||||||
|
rs.getString("username"),
|
||||||
|
rs.getString("password"),
|
||||||
|
rs.getString("identity")
|
||||||
|
};
|
||||||
|
tableModel.addRow(row);
|
||||||
|
}
|
||||||
|
rs.close();
|
||||||
|
jdbc.close();
|
||||||
|
} catch (SQLException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
JOptionPane.showMessageDialog(this, "加载数据失败: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void actionPerformed(ActionEvent e) {
|
||||||
|
// 添加用户
|
||||||
|
if (e.getSource() == addItem) {
|
||||||
|
JFrame frame = new JFrame("添加用户");
|
||||||
|
frame.setLayout(new GridLayout(5, 2));
|
||||||
|
JLabel nameLabel = new JLabel("用户名:");
|
||||||
|
JTextField nameField = new JTextField();
|
||||||
|
JLabel passLabel = new JLabel("密码:");
|
||||||
|
JPasswordField passField = new JPasswordField();
|
||||||
|
JButton addButton = new JButton("添加");
|
||||||
|
JButton cancelButton = new JButton("取消");
|
||||||
|
|
||||||
|
frame.add(nameLabel); frame.add(nameField);
|
||||||
|
frame.add(passLabel); frame.add(passField);
|
||||||
|
frame.add(addButton); frame.add(cancelButton);
|
||||||
|
|
||||||
|
addButton.addActionListener(ev -> {
|
||||||
|
String username = nameField.getText();
|
||||||
|
String password = passField.getText();
|
||||||
|
|
||||||
|
Jdbc jdbc = new Jdbc();
|
||||||
|
try {
|
||||||
|
jdbc.update("INSERT INTO user (username,password,email,phone,identity) VALUES ('"
|
||||||
|
+ username + "','" + password + "','user')");
|
||||||
|
jdbc.close();
|
||||||
|
loadData();
|
||||||
|
frame.dispose();
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
JOptionPane.showMessageDialog(frame, "添加失败: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
cancelButton.addActionListener(ev -> frame.dispose());
|
||||||
|
frame.setSize(400, 250);
|
||||||
|
frame.setLocationRelativeTo(null);
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 删除用户
|
||||||
|
else if (e.getSource() == deleteItem) {
|
||||||
|
int row = table.getSelectedRow();
|
||||||
|
if (row == -1) {
|
||||||
|
JOptionPane.showMessageDialog(this, "请先选择要删除的用户!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String username = (String) tableModel.getValueAt(row, 0);
|
||||||
|
int confirm = JOptionPane.showConfirmDialog(this, "确定删除用户: " + username + " ?", "确认", JOptionPane.YES_NO_OPTION);
|
||||||
|
if (confirm == JOptionPane.YES_OPTION) {
|
||||||
|
Jdbc jdbc = new Jdbc();
|
||||||
|
try {
|
||||||
|
jdbc.update("DELETE FROM user WHERE username='" + username + "'");
|
||||||
|
jdbc.close();
|
||||||
|
loadData();
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
JOptionPane.showMessageDialog(this, "删除失败: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 修改用户(选中一行后弹窗修改)
|
||||||
|
else if (e.getSource() == updateItem) {
|
||||||
|
int row = table.getSelectedRow();
|
||||||
|
if (row == -1) {
|
||||||
|
JOptionPane.showMessageDialog(this, "请先选择要修改的用户!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
String oldUsername = (String) tableModel.getValueAt(row, 0);
|
||||||
|
|
||||||
|
JFrame frame = new JFrame("修改用户");
|
||||||
|
frame.setLayout(new GridLayout(5, 2));
|
||||||
|
JTextField nameField = new JTextField((String) tableModel.getValueAt(row, 0));
|
||||||
|
JTextField passField = new JTextField((String) tableModel.getValueAt(row, 1));
|
||||||
|
JTextField emailField = new JTextField((String) tableModel.getValueAt(row, 2));
|
||||||
|
JTextField phoneField = new JTextField((String) tableModel.getValueAt(row, 3));
|
||||||
|
JButton saveButton = new JButton("保存");
|
||||||
|
JButton cancelButton = new JButton("取消");
|
||||||
|
|
||||||
|
frame.add(new JLabel("用户名:")); frame.add(nameField);
|
||||||
|
frame.add(new JLabel("密码:")); frame.add(passField);
|
||||||
|
frame.add(new JLabel("邮箱:")); frame.add(emailField);
|
||||||
|
frame.add(new JLabel("电话:")); frame.add(phoneField);
|
||||||
|
frame.add(saveButton); frame.add(cancelButton);
|
||||||
|
|
||||||
|
saveButton.addActionListener(ev -> {
|
||||||
|
String username = nameField.getText();
|
||||||
|
String password = passField.getText();
|
||||||
|
String email = emailField.getText();
|
||||||
|
String phone = phoneField.getText();
|
||||||
|
|
||||||
|
Jdbc jdbc = new Jdbc();
|
||||||
|
try {
|
||||||
|
jdbc.update("UPDATE user SET username='" + username + "', password='" + password + "', email='" + email + "', phone='" + phone + "' WHERE username='" + oldUsername + "'");
|
||||||
|
jdbc.close();
|
||||||
|
loadData();
|
||||||
|
frame.dispose();
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
JOptionPane.showMessageDialog(frame, "修改失败: " + ex.getMessage());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
cancelButton.addActionListener(ev -> frame.dispose());
|
||||||
|
frame.setSize(400, 250);
|
||||||
|
frame.setLocationRelativeTo(null);
|
||||||
|
frame.setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 刷新数据
|
||||||
|
else if (e.getSource() == refreshItem) {
|
||||||
|
loadData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new UserManagement();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user