@Java Swing简易员工管理系统
系统功能模块图
系统包括管理员登录页面和主页面设计,在主界面包括员工信息的增删改查功能。
系统实现环境说明
开发环境:eclipse 2022-03
Jdk版本:11
数据库:MySQL
数据库实现代码
1.创建数据库employees。
CREATE DATABASE employees;
USE employees;
2.创建管理员表admin并增加数据。
DROP TABLE IF EXISTS `admin`;
CREATE TABLE `admin` (
`ano` int(0) NOT NULL COMMENT '管理员号',
`aname` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '账号',
`password` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL COMMENT '密码',
PRIMARY KEY (`ano`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO `admin` VALUES (1, 'admin', 'admin');
3.创建员工信息表emp并增加数据。
DROP TABLE IF EXISTS `emp`;
CREATE TABLE `emp` (
`yno` int(0) NOT NULL AUTO_INCREMENT COMMENT '员工号',
`yname` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '员工姓名',
`sex` varchar(4) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '员工性别',
`birth` date NULL DEFAULT NULL COMMENT '出生日期',
`home` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '籍贯',
`degree` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '学历',
`salary` double(10, 2) NULL DEFAULT NULL COMMENT '薪水',
`info` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '备注',
PRIMARY KEY (`yno`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 9 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
INSERT INTO `emp` VALUES (2, '王二', '男', '2000-10-11', '湖南长沙', '初中', 2300.00, '');
INSERT INTO `emp` VALUES (4, '张三', '男', '1996-08-27', '湖北武汉', '初中', 2300.00, '平时兢兢业业');
INSERT INTO `emp` VALUES (5, '麻子', '男', '1994-09-13', '湖南怀化', '专科', 4500.00, '有一技之长');
INSERT INTO `emp` VALUES (6, '李四', '男', '1996-12-21', '湖南怀化', '高中', 3200.00, '');
INSERT INTO `emp` VALUES (7, '林黛玉', '女', '1998-08-12', '湖南株洲', '研究生', 6500.00, '工作认真负责,可堪大用');
INSERT INTO `emp` VALUES (8, '王小二', '男', '1999-02-03', '湖南常德', '高中', 4500.00, '做事认真');
INSERT INTO `emp` VALUES (9, '张晓', '女', '2000-02-13', '湖南岳阳', '高中', 4500.00, '工作认真负责,兢兢业业');
INSERT INTO `emp` VALUES (10, '顾枫熙', '女', '2000-03-16', '湖南株洲', '专科', 4500.00, '');
Java实现代码
数据访问层
1.基础层BaseDao
package com.dao;
import java.sql.Connection;
import java.sql.SQLException;
import com.util.DbUtil;
//创建对数据库连接对象
public class BaseDao {
public Connection con=new DbUtil().getCon();
public void closeDao(){
try {
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
2.管理员AdminDao
package com.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.model.Admin;
public class AdminDao extends BaseDao {
public Admin login(Admin admin){
String sql = "select * from admin where aname=? and password=?";
Admin adminRst = null;
try {
PreparedStatement prst = con.prepareStatement(sql);//把sql语句传给数据库操作对象
prst.setString(1, admin.getName());
prst.setString(2, admin.getPassword());
ResultSet executeQuery = prst.executeQuery();
if(executeQuery.next()){
adminRst = new Admin();
adminRst.setId(executeQuery.getInt("ano"));
adminRst.setName(executeQuery.getString("aname"));
adminRst.setPassword(executeQuery.getString("password"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return adminRst;
}
}
3.员工StaffDao
package com.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.model.Staff;
import com.util.StringUtil;
public class StaffDao extends BaseDao {
//展示员工列表,按员工姓名模糊查询
public List<Staff> getStaffList(Staff staff ){
List<Staff> retList= new ArrayList<Staff>();
StringBuffer sqlString = new StringBuffer("select * from emp");
if(!StringUtil.isEmpty(staff.getYname())){
sqlString.append(" and yname like '%"+staff.getYname()+"%'");
}
try {
PreparedStatement preparedStatement = con.prepareStatement(sqlString.toString().replaceFirst("and", "where"));
ResultSet executeQuery = preparedStatement.executeQuery();
while(executeQuery.next()){
Staff s = new Staff();
s.setYno(executeQuery.getInt("yno"));
s.setYname(executeQuery.getString("yname"));
s.setSex(executeQuery.getString("sex"));
s.setBirth(executeQuery.getString("birth"));
s.setHome(executeQuery.getString("home"));
s.setDegree(executeQuery.getString("degree"));
s.setSalary(executeQuery.getDouble("salary"));
s.setInfo(executeQuery.getString("info"));
retList.add(s);
}
} catch (SQLException e) {
e.printStackTrace();
}
return retList;
}
//删除员工信息
public boolean delete(int yno){
String sql = "delete from emp where yno=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setInt(1, yno);
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
//修改员工信息
public boolean update(Staff staff){
String sql = "update emp set yname=?,sex=?,birth=?,home=?,degree=?,salary=?,info=? where yno=?";
try {
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, staff.getYname());
preparedStatement.setString(2, staff.getSex());
preparedStatement.setString(3, staff.getBirth());
preparedStatement.setString(4, staff.getHome());
preparedStatement.setString(5, staff.getDegree());
preparedStatement.setDouble(6, staff.getSalary());
preparedStatement.setString(7, staff.getInfo());
preparedStatement.setInt(8, staff.getYno());
if(preparedStatement.executeUpdate() > 0){
return true;
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
//增加员工信息
public boolean addStaff(Staff staff){
String sql = "insert into emp values(null,?,?,?,?,?,?,?)";
try {
java.sql.PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, staff.getYname());
preparedStatement.setString(2, staff.getSex());
preparedStatement.setString(3, staff.getBirth());
preparedStatement.setString(4, staff.getHome());
preparedStatement.setString(5, staff.getDegree());
preparedStatement.setDouble(6, staff.getSalary());
preparedStatement.setString(7, staff.getInfo());
if(preparedStatement.executeUpdate() > 0)return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
}
数据库实体层
1.管理员Admin
package com.model;
public class Admin{
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2.员工Staff
package com.model;
public class Staff {
private int yno;
private String yname;
private String ypassword;
private String sex;
private String birth;
private String home;
private String degree;
private Double salary;
private String info;
public int getYno() {
return yno;
}
public void setYno(int yno) {
this.yno = yno;
}
public String getYname() {
return yname;
}
public void setYname(String yname) {
this.yname = yname;
}
public String getYpassword() {
return ypassword;
}
public void setYpassword(String ypassword) {
this.ypassword = ypassword;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public String getDegree() {
return degree;
}
public void setDegree(String degree) {
this.degree = degree;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
public String getInfo() {
return info;
}
public void setInfo(String info) {
this.info = info;
}
}
工具类
1.DbUtil
package com.util;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
//利用JDBC连接MySQL数据库
public class DbUtil {
private String dbUrl="jdbc:mysql://localhost:3306/employees?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
private String dbUserName="root";
private String dbPassword="root";
private String jdbcName="com.mysql.cj.jdbc.Driver";
//获取数据库连接
public Connection getCon (){
try{
Class.forName(jdbcName);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
Connection con=null;
try {
con=DriverManager.getConnection(dbUrl,dbUserName,dbPassword);
}catch(SQLException e) {
e.printStackTrace();
}
return con;
}
//关闭数据库连接
public void closeCon(Connection con) throws Exception{
if(con!=null) {
con.close();
}
}
public static void main(String[] args) {
DbUtil dbUtil=new DbUtil();
try {
dbUtil.getCon();
System.out.println("数据库连接成功!");
}catch(Exception e) {
e.printStackTrace();
System.out.println("数据库连接失败!");
}
}
}
2.StringUtil
package com.util;
//判断字符串是否为空
public class StringUtil {
public static boolean isEmpty(String str) {
if("".equals(str) || str==null) {
return true;
}
return false;
}
}
视图层
1.LoginFrm
package com.view;
import java.awt.EventQueue;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import com.dao.AdminDao;
import com.model.Admin;
import com.view.MainFrm;
import com.util.StringUtil;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
@SuppressWarnings({ "serial", "unused" })
public class LoginFrm extends JFrame {
private JPanel contentPane;
private JLabel titleLabel;
private JLabel anameJlabel;
private JTextField anameTextField;
private JLabel passwordJlabel;
private JPasswordField passwordTextField;
private JButton submitButton;
private JButton resetButton;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginFrm frame = new LoginFrm();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public LoginFrm() {
setTitle("登录系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
setBounds(500, 150, 630, 450);//设置页面大小
setResizable(false);//设置页面不可拖拽改变大小
getContentPane().setLayout(null);
setVisible(true);
titleLabel = new JLabel("员工管理系统");
titleLabel.setFont(new Font("华文行楷", Font.PLAIN, 30));
titleLabel.setBounds(215, 65, 182, 34);
getContentPane().add(titleLabel);
anameJlabel =new JLabel("账号:");
anameJlabel.setFont(new Font("宋体", Font.PLAIN, 20));
anameTextField=new JTextField();
anameJlabel.setBounds(155, 152, 67, 30);
anameTextField.setBounds(243, 152, 200, 30);
getContentPane().add(anameJlabel);
getContentPane().add(anameTextField);
passwordJlabel =new JLabel("密码:");
passwordJlabel.setFont(new Font("宋体", Font.PLAIN, 20));
passwordTextField=new JPasswordField();
passwordJlabel.setBounds(155, 202, 67, 30);
passwordTextField.setBounds(243, 202, 200, 30);
getContentPane().add(passwordJlabel);
getContentPane().add(passwordTextField);
resetButton =new JButton("重置");
resetButton.setBounds(340, 265, 90, 30);
getContentPane().add(resetButton);
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
resetValue(ae);
}
});
submitButton =new JButton("登录");
submitButton.setBounds(195, 265, 90, 30);
getContentPane().add(submitButton);
submitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
loginAct(ae);
}
});
}
protected void loginAct(ActionEvent ae) {
// TODO 自动生成的方法存根
String aname=anameTextField.getText().toString();
String password=passwordTextField.getText().toString();
if(StringUtil.isEmpty(aname)){
JOptionPane.showMessageDialog(this, "用户名不能为空!");
return;
}
if(StringUtil.isEmpty(password)){
JOptionPane.showMessageDialog(this, "密码不能为空!");
return;
}
AdminDao adminDao = new AdminDao();
Admin adminTmp = new Admin();
adminTmp.setName(aname);
adminTmp.setPassword(password);
Admin admin = adminDao.login(adminTmp);
adminDao.closeDao();
if(admin == null){
JOptionPane.showMessageDialog(this, "用户名或密码错误!");
return;
}
JOptionPane.showMessageDialog(this, "欢迎"+admin.getName()+"登录本系统!");
this.dispose();
new MainFrm(admin).setVisible(true);
}
protected void resetValue(ActionEvent ae) {
anameTextField.setText("");
passwordTextField.setText("");
}
}
2.MainFrm文章来源:https://www.toymoban.com/news/detail-491220.html
package com.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import java.util.Vector;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import com.dao.StaffDao;
import com.util.StringUtil;
import com.model.Staff;
import java.awt.Color;
import java.awt.Font;
@SuppressWarnings("serial")
public class MainFrm extends JFrame {
private JTable staffListTable;
private ButtonGroup sexButtonGroup;
private JButton deleteStaffButton;
private JButton searchButton;
private JLabel ynameJlabel;
private JTextField ynameTextField;
private JTextField searchTextField;
private DefaultTableModel model;
private JScrollPane jscrollpane;
private JLabel sexJLabel;
private JRadioButton MRadioButton;
private JRadioButton FRadioButton;
private JLabel birthJlabel;
private JTextField birthTextField;
private JLabel homeJlabel;
private JTextField homeTextField;
private JLabel degreeJlabel;
private JComboBox degreeJComboBox;
private JLabel salaryJlabel;
private JTextField salaryTextField;
private JLabel infoJlabel;
private JTextArea infoTextArea;
private JButton addButton;
private JButton resetButton;
private JButton editButton;
public static Object userObject;
public MainFrm(Object userObject) {
this.userObject = userObject;
setTitle("员工管理系统");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭窗口
setBounds(200, 20, 1200, 700);//设置页面大小
setResizable(false);//设置页面不可拖拽改变大小
getContentPane().setLayout(null);
setVisible(true);
searchTextField = new JTextField();
searchTextField.setBounds(250, 10, 180, 30);
getContentPane().add(searchTextField);
searchTextField.setColumns(10);
searchButton = new JButton("搜索");
searchButton.setBounds(450, 10, 95, 30);
getContentPane().add(searchButton);
searchButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
searchStaff(ae);
}
});
Object[] header = {"员工号","员工姓名","性别","出生日期","籍贯","学历","工资","备注"};
model = new DefaultTableModel(header,0);
staffListTable = new JTable(model);
jscrollpane = new JScrollPane(staffListTable);
jscrollpane.setBounds(10, 50, 800, 600);
getContentPane().add(jscrollpane);
setTable(new Staff());//展示员工信息列表
staffListTable.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent me) {
selectedTableRow(me);
}
});
ynameJlabel =new JLabel("员工姓名:");
ynameTextField=new JTextField();
ynameJlabel.setBounds(860, 50, 100, 30);
ynameTextField.setBounds(950, 50, 200, 30);
getContentPane().add(ynameJlabel);
getContentPane().add(ynameTextField);
sexJLabel = new JLabel("性 别:");
sexJLabel.setBounds(860, 100, 74, 23);
getContentPane().add(sexJLabel);
MRadioButton = new JRadioButton("男");
MRadioButton.setBounds(975, 100, 74, 23);
getContentPane().add(MRadioButton);
FRadioButton = new JRadioButton("女");
FRadioButton.setBounds(1051, 100, 74, 23);
getContentPane().add(FRadioButton);
sexButtonGroup = new ButtonGroup();
sexButtonGroup.add(MRadioButton);
sexButtonGroup.add(FRadioButton);
MRadioButton.setSelected(true);
birthJlabel =new JLabel("出生日期:");
birthTextField=new JTextField();
birthJlabel.setBounds(860, 150, 100, 30);
birthTextField.setBounds(950, 150, 200, 30);
getContentPane().add(birthJlabel);
getContentPane().add(birthTextField);
homeJlabel =new JLabel("籍 贯:");
homeTextField=new JTextField();
homeJlabel.setBounds(860, 200, 100, 30);
homeTextField.setBounds(950, 200, 200, 30);
getContentPane().add(homeJlabel);
getContentPane().add(homeTextField);
degreeJlabel =new JLabel("学 历:");
degreeJComboBox =new JComboBox();
degreeJlabel.setBounds(860, 250, 100, 30);
degreeJComboBox.setBounds(950, 250, 200, 30);
getContentPane().add(degreeJlabel);
getContentPane().add(degreeJComboBox);
salaryJlabel =new JLabel("工 资:");
salaryTextField=new JTextField();
salaryJlabel.setBounds(860, 300, 100, 30);
salaryTextField.setBounds(950, 300, 200, 30);
getContentPane().add(salaryJlabel);
getContentPane().add(salaryTextField);
infoJlabel =new JLabel("备 注:");
infoTextArea=new JTextArea();
infoJlabel.setBounds(860, 350, 100, 30);
infoTextArea.setBounds(950, 350, 200, 80);
getContentPane().add(infoJlabel);
getContentPane().add(infoTextArea);
JLabel remindLabel = new JLabel("出生日期应为YYYY-MM-DD或YYYY.MM.DD类型,否则添加失败");
remindLabel.setFont(new Font("宋体", Font.PLAIN, 13));
remindLabel.setForeground(Color.RED);
remindLabel.setBounds(820, 440, 360, 32);
getContentPane().add(remindLabel);
addButton =new JButton("新增员工");
addButton.setBounds(820, 500, 90, 30);
getContentPane().add(addButton);
addButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
staffAddAct(ae);
}
});
setEduInfo();
editButton =new JButton("修改信息");
editButton.setBounds(910, 500, 90, 30);
getContentPane().add(editButton);
editButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
submitEditAct(ae);
}
});
resetButton =new JButton("重置");
resetButton.setBounds(1000, 500, 90, 30);
getContentPane().add(resetButton);
resetButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
resetValue(ae);
}
});
deleteStaffButton =new JButton("删除员工");
deleteStaffButton.setBounds(1090, 500, 90, 30);
getContentPane().add(deleteStaffButton);
deleteStaffButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
deleteStaff(ae);
}
});
}
//查询员工信息
protected void searchStaff(ActionEvent ae) {
// TODO 自动生成的方法存根
Staff staff = new Staff();
staff.setYname(searchTextField.getText().toString());
setTable(staff);
}
//删除员工信息
protected void deleteStaff(ActionEvent ae) {
// TODO 自动生成的方法存根
int row = staffListTable.getSelectedRow();
if(row == -1){
JOptionPane.showMessageDialog(this, "请选中要删除的数据!");
return;
}
if(JOptionPane.showConfirmDialog(this, "您确定删除么?") != JOptionPane.OK_OPTION){
return;
}
StaffDao staffDao = new StaffDao();
if(staffDao.delete(Integer.parseInt(staffListTable.getValueAt(row, 0).toString()))){
JOptionPane.showMessageDialog(this, "删除成功!");
}else{
JOptionPane.showMessageDialog(this, "删除失败!");
}
staffDao.closeDao();
setTable(new Staff());
}
//列表展示员工信息
public void setTable(Staff staff) {
model =(DefaultTableModel) staffListTable.getModel();
model.setRowCount(0);
StaffDao staffDao=new StaffDao();
List<Staff> StaffList = staffDao.getStaffList(staff);
for(Staff s:StaffList) {
Vector v=new Vector();
v.add(s.getYno());
v.add(s.getYname());
v.add(s.getSex());
v.add(s.getBirth());
v.add(s.getHome());
v.add(s.getDegree());
v.add(s.getSalary());
v.add(s.getInfo());
model.addRow(v);
}
staffDao.closeDao();
}
//修改员工信息
protected void submitEditAct(ActionEvent ae) {
// TODO 自动生成的方法存根
int row = staffListTable.getSelectedRow();
if(row == -1){
JOptionPane.showMessageDialog(this, "请选中要修改的数据!");
return;
}
String staffName = ynameTextField.getText().toString();
String birth = birthTextField.getText().toString();
String home = homeTextField.getText().toString();
String degree =(String) degreeJComboBox.getSelectedItem();
String sex = MRadioButton.isSelected() ? MRadioButton.getText() : FRadioButton.getText();
Double salary = Double.parseDouble(salaryTextField.getText().toString());
String info = infoTextArea.getText().toString();
if(StringUtil.isEmpty(staffName)){
JOptionPane.showMessageDialog(this, "请填写员工姓名!");
return;
}
if(StringUtil.isEmpty(home)){
JOptionPane.showMessageDialog(this, "请填写员工籍贯!");
return;
}
Staff staff = new Staff();
staff.setYname(staffName);
staff.setYno(Integer.parseInt(staffListTable.getValueAt(row, 0).toString()));
if(MRadioButton.isSelected())staff.setSex(MRadioButton.getText().toString());
if(FRadioButton.isSelected())staff.setSex(FRadioButton.getText().toString());
staff.setSex(sex);
staff.setBirth(birth);
staff.setHome(home);
staff.setDegree(degree);
staff.setSalary(salary);
staff.setInfo(info);
StaffDao staffDao = new StaffDao();
if(staffDao.update(staff)){
JOptionPane.showMessageDialog(this, "更新成功!");
}else{
JOptionPane.showMessageDialog(this, "更新失败!");
}
staffDao.closeDao();
setTable(new Staff());
}
//重置按钮
private void resetValue(ActionEvent ae) {
// TODO 自动生成的方法存根
ynameTextField.setText("");
birthTextField.setText("");
infoTextArea.setText("");
homeTextField.setText("");
salaryTextField.setText("");
degreeJComboBox.setSelectedIndex(0);
sexButtonGroup.clearSelection();
MRadioButton.setSelected(true);
}
//添加员工信息
protected void staffAddAct(ActionEvent ae) {
// TODO Auto-generated method stub
String staffName = ynameTextField.getText().toString();
String birth = birthTextField.getText().toString();
String home = homeTextField.getText().toString();
Double salary = Double.parseDouble(salaryTextField.getText().toString());
String info = infoTextArea.getText().toString();
if(StringUtil.isEmpty(staffName)){
JOptionPane.showMessageDialog(this, "请填写员工姓名!");
return;
}
if(StringUtil.isEmpty(home)){
JOptionPane.showMessageDialog(this, "请填写员工籍贯!");
return;
}
String degree =(String) degreeJComboBox.getSelectedItem();
String sex = MRadioButton.isSelected() ? MRadioButton.getText() : FRadioButton.getText();
Staff staff = new Staff();
staff.setYname(staffName);
staff.setSex(sex);
staff.setBirth(birth);
staff.setHome(home);
staff.setDegree(degree);
staff.setSalary(salary);
staff.setInfo(info);
StaffDao staffDao = new StaffDao();
if(staffDao.addStaff(staff)){
JOptionPane.showMessageDialog(this, "添加成功!");
setTable(new Staff());
}else{
JOptionPane.showMessageDialog(this, "添加失败!");
}
resetValue(ae);
}
//学历下拉框内容
private void setEduInfo(){
degreeJComboBox.addItem("文盲");
degreeJComboBox.addItem("小学");
degreeJComboBox.addItem("初中");
degreeJComboBox.addItem("高中");
degreeJComboBox.addItem("专科");
degreeJComboBox.addItem("本科");
degreeJComboBox.addItem("研究生");
degreeJComboBox.addItem("博士");
}
//实现列表与员工信息内容联动
private void selectedTableRow(MouseEvent me) {
model = (DefaultTableModel) staffListTable.getModel();
ynameTextField.setText(model.getValueAt(staffListTable.getSelectedRow(), 1).toString());
birthTextField.setText(model.getValueAt(staffListTable.getSelectedRow(), 3).toString());
String sex = model.getValueAt(staffListTable.getSelectedRow(), 2).toString();
sexButtonGroup.clearSelection();
if(sex.equals(MRadioButton.getText()))MRadioButton.setSelected(true);
if(sex.equals(FRadioButton.getText()))FRadioButton.setSelected(true);
homeTextField.setText(model.getValueAt(staffListTable.getSelectedRow(), 4).toString());
degreeJComboBox.setSelectedItem(model.getValueAt(staffListTable.getSelectedRow(), 5).toString());
salaryTextField.setText(model.getValueAt(staffListTable.getSelectedRow(), 6).toString());
infoTextArea.setText(model.getValueAt(staffListTable.getSelectedRow(), 7).toString());
}
}
运行效果图
1.登录界面
此页面使用来实现管理员的登录。管理员通过输入账号和密码进行登录,点击登录会触发登录判断事件,首先判断用户是否填写账号和密码,如果账号或密码为空会弹窗提示不能为空,需要用户重新输入,当输入均不为空时会获取文本框内容来判断是否和数据库中的管理员账号密码一致。当用户输入错误时可以直接点击重置按钮对文本框内容进行清空操作。
2.主界面
管理员登录成功后跳转到系统主界面,管理员可在此进行相应操作。此界面包括以下功能:
1)员工信息添加功能:该功能可以添加新员工信息。其中员工号是自动增加,无需手动添加,员工姓名和籍贯为必填项,性别通过使用JRadioButton和ButtonGroup组件限制用户只能选择性别为男或女,默认性别为男,学历通过使用JComboBox下拉框限制规范填写学历信息,默认学历为文盲,此外出生日期填写应规范为“YYYY-MM-DD”或“YYYY.MM.DD”模式,否则添加失败。
2)员工信息查询功能:该功能可以通过对员工姓名的模糊查询,查询到所需员工的基本信息。若需查询所有员工信息,可在不填写搜索框的情况下点击搜索,即可查看到全部员工的基本信息。
3)员工信息修改功能:该功能可以修改选中员工的基本信息,其中员工号是不可修改信息,员工姓名和籍贯为必填项,此外出生日期填写应规范为“YYYY-MM-DD”或“YYYY.MM.DD”模式,否则修改失败。
4)员工信息删除功能:该功能可以删除选中员工的基本信息。
文章来源地址https://www.toymoban.com/news/detail-491220.html
到了这里,关于Java Swing大作业--简易员工管理系统的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!