基于Java Swing的员工管理系统开发实践

目录

1. 引言

2. 技术栈

3. 开发工具

4. 项目结构

5. 主要功能

对于普通员工:

对于管理员:

6. 系统截图

7. 安装与配置

数据库设置

SQL脚本

8. 登录指南

9. 实践心得

9.1 数据库设计

9.2 Swing组件使用

示例代码:登录界面

用户

工资

10. 结语


1. 引言

随着企业规模的不断扩大,传统的手工记录员工信息的方式已经无法满足现代企业的管理需求。为此,本文将分享一款基于Java Swing技术开发的员工管理系统的设计与实现过程,旨在帮助企业提升员工信息管理的效率。

2. 技术栈

  • 前端框架:Java Swing,用于构建图形用户界面。

  • 后端语言:Java 8,强大的面向对象语言,提供了丰富的API。

  • 数据库:MySQL 8,关系型数据库管理系统。

3. 开发工具

  • IDE:Eclipse,虽然也提到了可以使用IntelliJ IDEA,但本文以Eclipse为例。

  • 数据库管理工具:如Navicat,用于创建数据库和导入SQL脚本。

4. 项目结构

项目采用了典型的MVC(Model-View-Controller)架构,主要分为以下几个部分:

  • Model:实体类,如EmployeeSalary等。

  • View:用户界面,使用Swing组件实现。

  • Controller:逻辑控制层,处理业务逻辑并与Model和View交互。

  • DAO:数据访问对象,负责与数据库交互。

5. 主要功能

系统提供了两种角色:管理员和普通员工。

对于普通员工:

  • 工资查询:员工可以登录系统查询自己的工资信息。

对于管理员:

  • 员工管理:可以添加、修改、删除和查询员工信息。

  • 工资管理:能够添加和发放工资。

6. 系统截图

图片

图片

图片

图片

图片

图片

图片

图片

图片

7. 安装与配置

数据库设置

  1. 创建数据库:在MySQL中创建一个名为db_salary的数据库。

  2. 导入SQL脚本:将项目提供的SQL文件导入到新建的数据库中,完成数据表的初始化。

SQL脚本

create database db_salary;

use db_salary;


SET FOREIGN_KEY_CHECKS=0;

DROP TABLE IF EXISTS `department`;
CREATE TABLE `department` (
                              `id` varchar(255) NOT NULL,
                              `name` varchar(255) NOT NULL,
                              `director` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
                              PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

INSERT INTO `department` VALUES ('DE001', '财务部', '小李');
INSERT INTO `department` VALUES ('DE002', '经理部', '小黑');

DROP TABLE IF EXISTS `employee`;
CREATE TABLE `employee` (
                            `id` varchar(255) NOT NULL,
                            `name` varchar(255) NOT NULL,
                            `sex` varchar(255) NOT NULL,
                            `age` int NOT NULL,
                            `department_id` varchar(255) NOT NULL,
                            `position` varchar(255) NOT NULL,
                            `phone` varchar(255) NOT NULL,
                            `time` date NOT NULL,
                            `address` varchar(255) NOT NULL,
                            PRIMARY KEY (`id`,`department_id`),
                            KEY `ed_dept_id` (`department_id`),
                            KEY `id` (`id`),
                            CONSTRAINT `ed_dept_id` FOREIGN KEY (`department_id`) REFERENCES `department` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3;

INSERT INTO `employee` VALUES ('1001', '王一', '男', '25', 'DE002', '会计', '18474588569', '2021-07-20', '山东曹县');
INSERT INTO `employee` VALUES ('1002', '王二3', '女', '36', 'DE002', '工程师', '15478453654', '2021-08-23', '山东日照');
INSERT INTO `employee` VALUES ('1003', '1', '1', '1', 'DE002', '1', '1', '2021-06-12', '1');

DROP TABLE IF EXISTS `salary`;
CREATE TABLE `salary` (
                          `id` int NOT NULL AUTO_INCREMENT,
                          `emp_id` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
                          `basepay` int DEFAULT NULL,
                          `welfare` int DEFAULT NULL,
                          `reward` int DEFAULT NULL,
                          `insurance` int DEFAULT NULL,
                          `funds` int DEFAULT NULL,
                          `month` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
                          `remark` varchar(25) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
                          PRIMARY KEY (`id`,`emp_id`),
                          KEY `se_name` (`emp_id`),
                          CONSTRAINT `se_name` FOREIGN KEY (`emp_id`) REFERENCES `employee` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=17 DEFAULT CHARSET=utf8mb3 COLLATE=utf8_unicode_ci;

INSERT INTO `salary` VALUES ('1', '1001', '3000', '100', '100', '100', '500', '1月', '已发放');
INSERT INTO `salary` VALUES ('13', '1002', '3000', '200', '100', '100', '400', '1月', '未结算');
INSERT INTO `salary` VALUES ('15', '1003', '2500', '100', '200', '100', '300', '1月', '未结算');

DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
                        `id` int NOT NULL AUTO_INCREMENT,
                        `username` varchar(255) NOT NULL,
                        `password` varchar(255) NOT NULL,
                        `status` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL DEFAULT '管理员',
                        PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;


INSERT INTO `user` VALUES ('1', 'admin', '123456', '管理员');
INSERT INTO `user` VALUES ('3', 'test', '123456', '管理员');

8. 登录指南

  • 管理员登录:用户名为admin,密码为123456

9. 实践心得

9.1 数据库设计

合理的设计数据库表结构是保证系统稳定性的基础。在设计时,应考虑到扩展性和数据的一致性。

9.2 Swing组件使用

Swing提供了丰富的组件来构建用户界面,但在使用时需要注意界面布局的美观性和用户体验。

示例代码:登录界面
package com.system.view;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import com.system.entity.User;
import com.system.service.UserService;
import com.system.service.UserServiceImpl;

public class Login extends JFrame {

   private JPanel contentPane;
   private JPasswordField passwordField;
   private JTextField usernameField;
   private JComboBox comboBox;

   public static void main(String[] args) {
      EventQueue.invokeLater(new Runnable() {
         public void run() {
            try {
               Login frame = new Login();
               frame.setVisible(true);
            } catch (Exception e) {
               e.printStackTrace();
            }
         }
      });
   }

   public Login() {
      setTitle("员工工资管理系统");
      setBounds(100, 100, 734, 548);
      contentPane = new JPanel();
      contentPane.setBackground(Color.WHITE);
      contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
      setContentPane(contentPane);
      contentPane.setLayout(null);

      JPanel panel = new JPanel();
      panel.setBackground(Color.BLACK);
      panel.setBounds(0, 0, 361, 509);
      contentPane.add(panel);
      panel.setLayout(null);

      JLabel lblNewLabel = new JLabel("");
      lblNewLabel.setIcon(new ImageIcon(Login.class.getResource("/images/welcome.jpg")));
      lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
      lblNewLabel.setBounds(0, 0, 361, 426);
      panel.add(lblNewLabel);

      JLabel lblNewLabel_1 = new JLabel("员工工资管理系统");
      lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
      lblNewLabel_1.setFont(new Font("微软雅黑", Font.BOLD, 18));
      lblNewLabel_1.setForeground(Color.WHITE);
      lblNewLabel_1.setBounds(67, 450, 212, 36);
      panel.add(lblNewLabel_1);

      JPanel panel_1 = new JPanel();
      panel_1.setBackground(Color.WHITE);
      panel_1.setBounds(361, 0, 357, 509);
      contentPane.add(panel_1);
      panel_1.setLayout(null);

      JLabel lblNewLabel_2 = new JLabel("登     录");
      lblNewLabel_2.setForeground(Color.BLUE);
      lblNewLabel_2.setFont(new Font("微软雅黑", Font.BOLD, 18));
      lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
      lblNewLabel_2.setBounds(97, 61, 155, 29);
      panel_1.add(lblNewLabel_2);

      JLabel lblNewLabel_3 = new JLabel("用户名:");
      lblNewLabel_3.setFont(new Font("微软雅黑", Font.BOLD, 16));
      lblNewLabel_3.setHorizontalAlignment(SwingConstants.RIGHT);
      lblNewLabel_3.setBounds(41, 123, 72, 29);
      panel_1.add(lblNewLabel_3);

      JLabel lblNewLabel_3_1 = new JLabel("密  码:");
      lblNewLabel_3_1.setHorizontalAlignment(SwingConstants.RIGHT);
      lblNewLabel_3_1.setFont(new Font("微软雅黑", Font.BOLD, 16));
      lblNewLabel_3_1.setBounds(41, 184, 72, 29);
      panel_1.add(lblNewLabel_3_1);

      JLabel lblNewLabel_3_1_1 = new JLabel("权  限:");
      lblNewLabel_3_1_1.setHorizontalAlignment(SwingConstants.RIGHT);
      lblNewLabel_3_1_1.setFont(new Font("微软雅黑", Font.BOLD, 16));
      lblNewLabel_3_1_1.setBounds(41, 253, 72, 29);
      panel_1.add(lblNewLabel_3_1_1);
      
      passwordField = new JPasswordField();
      passwordField.setBounds(116, 184, 167, 29);
      panel_1.add(passwordField);
      
      usernameField = new JTextField();
      usernameField.setBounds(116, 123, 167, 29);
      panel_1.add(usernameField);
      usernameField.setColumns(10);
      
      String[] menu = { "管理员", "普通用户" };
      comboBox = new JComboBox(menu);
      comboBox.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            String a = (String) comboBox.getSelectedItem();
            if ("普通用户".equals(a)) {
               setVisible(false);
               new EmployeeView().setVisible(true);
            }
         }
      });
      comboBox.setBounds(116, 253, 167, 29);
      panel_1.add(comboBox);
      
      JButton btnNewButton = new JButton("登录");
      btnNewButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            String username = usernameField.getText();
            String password = passwordField.getText();
            String status = (String) comboBox.getSelectedItem();
            if ("".equals(username) || "".equals(password)) {
               JOptionPane.showMessageDialog(null, "用户名或密码不能为空!");
            } else {
               UserService userService = new UserServiceImpl();
               User user = userService.Login(username, password, status);
               if (user != null) {
                  JOptionPane.showMessageDialog(null, "登录成功!");
                  setVisible(false);
                  new BG().setVisible(true);
               } else {
                  JOptionPane.showMessageDialog(null, "登录失败!");
               }

            }
         }
      });
      btnNewButton.setFont(new Font("微软雅黑", Font.BOLD, 14));
      btnNewButton.setForeground(Color.BLACK);
      btnNewButton.setBounds(41, 356, 93, 29);
      panel_1.add(btnNewButton);
      
      JButton btnNewButton_1 = new JButton("退出");
      btnNewButton_1.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent arg0) {
            System.exit(1);
         }
      });
      btnNewButton_1.setForeground(Color.BLACK);
      btnNewButton_1.setFont(new Font("微软雅黑", Font.BOLD, 14));
      btnNewButton_1.setBounds(231, 356, 93, 29);
      panel_1.add(btnNewButton_1);
   }
}
用户
package com.system.entity;

public class User {

   private String id;
   private String username;
   private String password;
   private String status;

   public User() {
      super();
   }

   public User(String id, String username, String password, String status) {
      super();
      this.id = id;
      this.username = username;
      this.password = password;
      this.status = status;
   }

   public String getId() {
      return id;
   }

   public void setId(String id) {
      this.id = id;
   }

   public String getUsername() {
      return username;
   }

   public void setUsername(String username) {
      this.username = username;
   }

   public String getPassword() {
      return password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   public String getStatus() {
      return status;
   }

   public void setStatus(String status) {
      this.status = status;
   }

   @Override
   public String toString() {
      return "User [id=" + id + ", username=" + username + ", password=" + password + ", status=" + status + "]";
   }

}
工资
package com.system.entity;

public class Salary {

   private String id;
   private String empId;
   private int basepay; // 基础工资
   private int welfare; // 福利补贴
   private int reward; // 奖金
   private int insurance; // 失业保险
   private int funds; // 公积金
   private String month; // 月份
   private String remark; // 是否发放

   private Employee employee;
   private Department department;


   public Salary() {
      super();
   }


   public Salary(String id, String empId, int basepay, int welfare, int reward, int insurance, int funds, String month,
         String remark, Employee employee, Department department) {
      super();
      this.id = id;
      this.empId = empId;
      this.basepay = basepay;
      this.welfare = welfare;
      this.reward = reward;
      this.insurance = insurance;
      this.funds = funds;
      this.month = month;
      this.remark = remark;
      this.employee = employee;
      this.department = department;
   }

   public Employee getEmployee() {
      return employee;
   }

   public void setEmployee(Employee employee) {
      this.employee = employee;
   }

   public Department getDepartment() {
      return department;
   }

   public void setDepartment(Department department) {
      this.department = department;
   }

   public String getId() {
      return id;
   }

   public void setId(String id) {
      this.id = id;
   }

   public String getEmpId() {
      return empId;
   }

   public void setEmpId(String empId) {
      this.empId = empId;
   }

   public int getBasepay() {
      return basepay;
   }

   public void setBasepay(int basepay) {
      this.basepay = basepay;
   }

   public int getWelfare() {
      return welfare;
   }

   public void setWelfare(int welfare) {
      this.welfare = welfare;
   }

   public int getReward() {
      return reward;
   }

   public void setReward(int reward) {
      this.reward = reward;
   }

   public int getInsurance() {
      return insurance;
   }

   public void setInsurance(int insurance) {
      this.insurance = insurance;
   }

   public int getFunds() {
      return funds;
   }

   public void setFunds(int funds) {
      this.funds = funds;
   }

   public String getMonth() {
      return month;
   }

   public void setMonth(String month) {
      this.month = month;
   }

   public String getRemark() {
      return remark;
   }

   public void setRemark(String remark) {
      this.remark = remark;
   }


   @Override
   public String toString() {
      return "Salary [id=" + id + ", empId=" + empId + ", basepay=" + basepay + ", welfare=" + welfare + ", reward="
            + reward + ", insurance=" + insurance + ", funds=" + funds + ", month=" + month + ", remark=" + remark
            + ", employee=" + employee + ", department=" + department + "]";
   }


}

10. 结语

通过本文的介绍,希望能够帮助大家快速上手基于Java Swing的员工管理系统开发。当然,这只是冰山一角,实际的开发工作中还需要不断地学习和探索。希望各位读者能够在实践中不断进步,开发出更加优秀的应用系统。

源码获取

  • 13
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值