1测试类-----------------------------------------------------------------
package kcsj;
import javax.swing.*;
import java.awt.event.*;
import java.util.ArrayList;
public class Test extends JFrame{
public static void main(String[] args) {
new System();
}
}
2登录注册类-----------------------------------------------------------------
package kcsj;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class System extends JFrame implements ActionListener {
JLabel sy= new JLabel("欢迎来到本系统");
JButton dl=new JButton("登录");
JButton zc=new JButton("注册");
public System() {
initxitong();
initFrame();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("首界面");
this.setSize(500, 500);//初始位置
this.setLayout(null);
this.setLocationRelativeTo(null);
sy.setForeground(Color.GREEN);
this.setDefaultCloseOperation(3);
}
private void initxitong() {
sy.setBounds(140,100,300,100);
dl.setBounds(100,300,100,50);
zc.setBounds(290,300,100,50);
sy.setFont(new Font("宋体",Font.BOLD,30));
this.add(dl);
this.add(sy);
this.add(zc);
dl.addActionListener(this);
zc.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == dl) {
new Login();
dispose();
}
if (e.getSource() == zc) {
new Register();
dispose();
}
}
}
3登录类-----------------------------------------------------------------
package kcsj;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Login extends JFrame implements ActionListener {
JTextField UID = new JTextField();
JTextField Password = new JTextField();
JLabel zh = new JLabel("账号:");
JLabel mm = new JLabel("密码:");
JButton zhk = new JButton("登录");
JButton zhl=new JButton("返回首界面");
public Login() {
initFrame();
initzhuce();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("登录界面");
this.setSize(500, 500);// 初始位置
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
}
private void initzhuce() {
// 文本框
UID.setBounds(190, 100, 160, 40);
Password.setBounds(190, 170, 160, 40);
// 标签
zh.setBounds(140, 75, 100, 80);
mm.setBounds(140, 150, 100, 80);
zh.setFont(new Font("宋体",Font.BOLD,15));
mm.setFont(new Font("宋体",Font.BOLD,15));
// 按钮
zhk.setBounds(80,300,100,50);
zhl.setBounds(300,300,100,50);
this.add(UID);
this.add(Password);
this.add(zh);
this.add(mm);
this.add(zhk);
this.add(zhl);
zhk.addActionListener(this);
zhl.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==zhk) {
String username = UID.getText();
String password = Password.getText();
try (BufferedReader reader = new BufferedReader(new FileReader("users.txt"))) {
String line;
if (validateUser(username, password)) {
// 登录成功,显示欢迎界面
JOptionPane.showMessageDialog(this, "欢迎来到系统!");
new Huazi();
dispose();
} else {
// 登录失败,显示错误消息
JOptionPane.showMessageDialog(this, "登录失败。");
}
} catch (IOException e1) {
e1.printStackTrace();
}
}
if(e.getSource()==zhl) {
new System();
dispose();
}
}
private boolean validateUser(String username, String password) {
while (true) {
try (BufferedReader reader = new BufferedReader(new FileReader("users.txt"))) {
String line;
boolean flag = false;
//______________________________________________
while ((line = reader.readLine()) != null) {
String[] userInfo = line.split(",");
if (userInfo[0].equals(username) && userInfo[1].equals(password)) {
flag = true;
return true;
}
}
//_______________________________________________
if (flag) {
continue;
}
} catch (IOException e) {
e.printStackTrace();
}
break;
}
return false;
}
}
4注册类-----------------------------------------------------------------
package kcsj;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class Register extends JFrame implements ActionListener {
JTextField UID = new JTextField();
JTextField Password = new JTextField();
JLabel zh = new JLabel("账号:");
JLabel mm = new JLabel("密码:");
JButton zhk = new JButton("确认注册");
JButton sjm=new JButton("返回首界面");
public Register() {
initFrame();
initzhuce();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("注册界面");
this.setSize(500, 500);// 初始位置
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
}
private void initzhuce() {
// 文本框
UID.setBounds(190, 100, 160, 40);
Password.setBounds(190, 170, 160, 40);
// 标签
zh.setBounds(140, 75, 100, 80);
mm.setBounds(140, 150, 100, 80);
zh.setFont(new Font("宋体",Font.BOLD,15));
mm.setFont(new Font("宋体",Font.BOLD,15));
// 按钮
zhk.setBounds(80, 300, 100, 50);
sjm.setBounds(300,300,100,50);
this.add(UID);
this.add(Password);
this.add(sjm);
this.add(zh);
this.add(mm);
this.add(zhk);
zhk.addActionListener(this);
sjm.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == zhk) {
String username = UID.getText();
String password = Password.getText();
// 注册用户
if (username.equals("") || password.equals("")) {
JOptionPane.showMessageDialog(this, "注册失败,请重试!");
} else {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("users.txt", true));
BufferedReader reader = new BufferedReader(new FileReader("users.txt"));
String line;
String str = UID.getText() + "," + Password.getText();
boolean found = false;
while((line = reader.readLine()) != null) {
String[] s = line.split(",");
if (UID.getText().equals(s[0])) {
JOptionPane.showMessageDialog(this, "该账号已存在,请重新注册!");
reader.close();
found=true;
new Register();
this.dispose();
}
}
if(!found) {
writer.write(username + "," + password);
writer.newLine();
writer.close();
JOptionPane.showMessageDialog(this, "注册成功,请登录!");
new Login();
dispose();
}
} catch (IOException e1) {
}
}
}
if (e.getSource() == sjm) {
new System();
dispose();
}
}
}
5添加类-----------------------------------------------------------------
package kcsj;
import java.io.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;
public class Add extends JFrame implements ActionListener {
JTextField a = new JTextField();
JTextField b = new JTextField();
//JTextField c = new JTextField();
//JTextField d = new JTextField();
JTextField g = new JTextField();
JButton j1 = new JButton("确认添加");
JButton j2 = new JButton("返回");
ButtonGroup group = null;
JRadioButton m,n;
JPanel p,x;
JComboBox<String> comBox = new JComboBox<String>();
JLabel a1 = new JLabel("姓名:");
JLabel a2 = new JLabel("编号:");
JLabel a3 = new JLabel("性别:");
JLabel a4 = new JLabel("所在部门:");
JLabel a5 = new JLabel("薪资级别:");
public Add() {
initwenben();
initFrame();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("添加信息");
this.setSize(500, 500);// 初始位置
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
}
private void initwenben() {
a.setBounds(220, 50, 100, 25);
a1.setBounds(150, 50, 60, 20);
b.setBounds(220, 90, 100, 25);
a2.setBounds(150, 90, 60, 20);
//p.setBounds(220, 120, 100, 25);
a3.setBounds(150, 130, 60, 20);
//d.setBounds(220, 150, 100, 25);
a4.setBounds(150, 170, 60, 20);
g.setBounds(220, 210, 100, 25);
a5.setBounds(150, 210, 60, 20);
j1.setBounds(100, 280, 100, 50);
j2.setBounds(280, 280, 100, 50);
this.add(j1);
this.add(j2);
j2.addActionListener(this);
j1.addActionListener(this);
this.add(a);
this.add(a1);
this.add(b);
this.add(a2);
this.add(a3);
//this.add(d);
this.add(a4);
this.add(g);
this.add(a5);
group = new ButtonGroup();
m=new JRadioButton("男");//初始化单选框,
n=new JRadioButton("女");
group.add(m);// 把按钮添加到按钮组
group.add(n);
p=new JPanel();
//p.add(new JLabel("性别:",JLabel.CENTER));
p.add(m);
p.add(n);
p.setBounds(220, 125, 100, 25);
this.add(p);
comBox.addItem("总经理");
comBox.addItem("销售经理");
comBox.addItem("销售人员");
comBox.addItem("技术经理");
comBox.addItem("技术人员");
x=new JPanel();
x.add(comBox);
x.setBounds(210, 165, 120, 25);
this.add(x);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == j1) {
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("t1.txt", true));
BufferedReader reader = new BufferedReader(new FileReader("t1.txt"));
String line;
boolean found = true;
String ssex=null;
if(m.isSelected()){//选择男女
ssex=m.getText();
}
else{
ssex=n.getText();
}
String str = a.getText() + "," + b.getText() + "," + ssex + "," + comBox.getSelectedItem() + ","
+ g.getText();
while ((line = reader.readLine()) != null) {
String[] s = line.split(",");
if (b.getText().equals(s[1])) {
JOptionPane.showMessageDialog(this, "该员工已经存在!");
reader.close();
found = false;
new Add();
this.dispose();
}
}
if (found) {
writer.write(str);
writer.newLine();
writer.close();
JOptionPane.showMessageDialog(this, "员工信息已添加。");
new Add();
this.dispose();
}
} catch (IOException ex) {
}
} else if (e.getSource() == j2) {
new Windows();
this.dispose();
}
}
}
67统计类(以及子类)-----------------------------------------------------------------
package kcsj;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
public class Count extends JFrame implements ActionListener {
JTextArea show=new JTextArea();
JButton cb = new JButton("返回主菜单");
JButton ck = new JButton("统计");
public Count() {
initFrame();
inittongji();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("统计");
this.setSize(500, 500);// 初始位置
this.setLayout(null);
this.setLocationRelativeTo(null);
// this.setResizable(false);不能改变大小
this.setDefaultCloseOperation(3);
}
private void inittongji() {
cb.setBounds(100, 200, 100, 50);
ck.setBounds(250, 200, 100, 50);
this.add(show);
this.add(cb);
this.add(ck);
cb.addActionListener(this);
ck.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == cb) {
new Windows();
this.dispose();
}
if (e.getSource() == ck) {
new Countzl();
this.dispose();
}
}
}
8删除类-----------------------------------------------------------------
package kcsj;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Delete extends JFrame implements ActionListener {
JButton db = new JButton("返回主菜单");
JButton dk = new JButton("删除");
public Delete() {
initFrame();
initshanchu();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("删除");
this.setSize(500, 500);// 初始位置
this.setLayout(null);
this.setLocationRelativeTo(null);
// this.setResizable(false);不能改变大小
this.setDefaultCloseOperation(3);
}
private void initshanchu() {
db.setBounds(100, 200, 100, 50);
dk.setBounds(250, 200, 100, 50);
this.add(db);
this.add(dk);
db.addActionListener(this);
dk.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == dk) {
String Input = JOptionPane.showInputDialog(this, "请输入要删除的员工编号:");
try {
BufferedReader reader = new BufferedReader(new FileReader("t1.txt"));
String line;
ArrayList<Tool> list = new ArrayList<>();
boolean found = false;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
Tool wd = new Tool(fields[0], fields[1], fields[2], fields[3], fields[4]);
list.add(wd);
}
reader.close();
for (int i = 0; i < list.size(); i++) {
Tool wdf = list.get(i);
if (wdf.getNum().equals(Input)) {
list.remove(i);
i--;
found=true;
}
}
if (found) {
// 先备份为临时文件
File f = new File("t1.txt");
File tempFile = File.createTempFile("t1_temp", ".txt", f.getParentFile());
BufferedWriter tempWriter = new BufferedWriter(new FileWriter(tempFile));
for (int i = 0; i < list.size(); i++) {
Tool wd = list.get(i);
String all = wd.getName() + "," + wd.getNum() + "," + wd.getSex() + "," + wd.getJob() + ","
+ wd.getLevel();
tempWriter.write(all);
tempWriter.newLine();
}
tempWriter.close();
// 删除原始文件并将临时文件重命名为原始文件名
if (f.delete() && tempFile.renameTo(f)) {
JOptionPane.showMessageDialog(this, "删除成功。");
}
else {
JOptionPane.showMessageDialog(this, "删除失败:无法备份或替换文件。");
}
}else {
JOptionPane.showMessageDialog(this, "未找到该员工!");
}
}
catch (IOException ex) {
ex.printStackTrace();
}
}
if (e.getSource() == db) {
new Windows();
this.dispose();
}
}
}
9身份选择类-----------------------------------------------------------------
package kcsj;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class Huazi extends JFrame implements ActionListener {
JButton gl = new JButton("管理人员");
JButton yg = new JButton("员工");
JLabel b = new JLabel("请选择你的身份");
public Huazi() {
initFrame();
initxiugai();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("领取工资");
this.setSize(500, 500);// 初始位置
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
}
private void initxiugai() {
b.setBounds(140, 100, 300, 100);
gl.setBounds(100, 240, 100, 50);
yg.setBounds(290, 240, 100, 50);
b.setFont(new Font("宋体", Font.BOLD, 30));
b.setForeground(Color.BLUE);
this.add(gl);
this.add(yg);
this.add(b);
gl.addActionListener(this);
yg.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == gl) {
new Windows();
this.dispose();
}
if (e.getSource() == yg) {
new Hy();
this.dispose();
}
}
}
10员工领工资类-----------------------------------------------------------------
package kcsj;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Hy extends JFrame implements ActionListener {
JButton hb = new JButton("领取工资");
JButton hk = new JButton("返回");
JTextArea ex = new JTextArea();
public Hy() {
initFrame();
initxiugaizile();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("工资");
this.setSize(500, 500);// 初始位置
ex.setBounds(0, 0, 500, 200);
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
}
private void initxiugaizile() {
hb.setBounds(290, 250, 100, 50);
hk.setBounds(100, 250, 100, 50);
this.add(ex);
this.add(hk);
this.add(hb);
hb.addActionListener(this);
hk.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == hb) {
String id = JOptionPane.showInputDialog(this, "请输入领取工资的员工编号:");
try {
BufferedReader reader = new BufferedReader(new FileReader("t1.txt"));
String line;
ArrayList<String[]> list = new ArrayList<String[]>();
boolean found = false;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields[1].equals(id)) {
String i = JOptionPane.showInputDialog(this, "员工员工领取月份:");
String g = JOptionPane.showInputDialog(this, "请输入员工绩效%::");
double num1 = Double.parseDouble(i);
double num2 = Double.parseDouble(g);
double num = Double.parseDouble(fields[4]);
num = num * num2 * num1 / 12;
fields[4] = Double.toString(num);
line = fields[0] + "," + fields[1] + "," + fields[2] + "," + "," + fields[3] + "," + fields[4];
found = true;
list.remove(fields[4]);
list.add(fields);
ex.append(line);
ex.append("\n");
}
}
reader.close();
if(found) {
BufferedWriter writer = new BufferedWriter(new FileWriter("t2.txt"));
for (String[] fields1 : list) {
String newLine = String.join(",", fields1);
writer.write(newLine);
writer.newLine();
}
writer.close();
JOptionPane.showMessageDialog(this, "员工工资已经分发。");
}
} catch (IOException ex) {
}
}
if (e.getSource() == hk) {
new Huazi();
dispose();
}
}
}
1112修改类(以及子类)-----------------------------------------------------------------
package kcsj;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Moidfy extends JFrame implements ActionListener{
JButton mb = new JButton("返回主菜单");
JButton mk = new JButton("修改");
public Moidfy() {
initFrame();
initxiugai();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("修改");
this.setSize(500, 500);// 初始位置
this.setLayout(null);
this.setLocationRelativeTo(null);
// this.setResizable(false);不能改变大小
this.setDefaultCloseOperation(3);
}
private void initxiugai() {
mb.setBounds(100, 200, 100, 50);
mk.setBounds(250, 200, 100, 50);
this.add(mb);
this.add(mk);
mb.addActionListener(this);
mk.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == mb) {
new Windows();
this.dispose();
}
if (e.getSource() == mk) {
new Moidfyzl();
this.dispose();
}
}
}
package kcsj;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class Moidfyzl extends JFrame implements ActionListener {
JButton mk1 = new JButton("修改姓名");
JButton mk2 = new JButton("修改编号");
JButton mk3 = new JButton("修改性别");
JButton mk4 = new JButton("修改在职部门");
JButton mk5 = new JButton("修改薪资级别");
JButton mkb = new JButton("返回主菜单");
JTextField a = new JTextField();
JTextField b = new JTextField();
JTextField c = new JTextField();
JTextField d = new JTextField();
JTextField g = new JTextField();
public Moidfyzl() {
initFrame();
initxiugaizile();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("修改");
this.setSize(500, 500);// 初始位置
this.setLayout(null);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(3);
}
private void initxiugaizile() {
mk1.setBounds(270, 30, 120, 30);
mk2.setBounds(270, 70, 120, 30);
mk3.setBounds(270, 110, 120, 30);
mk4.setBounds(270, 150, 120, 30);
mk5.setBounds(270, 190, 120, 30);
mkb.setBounds(180, 300, 100, 110);
this.add(mk1);
this.add(mk2);
this.add(mk3);
this.add(mk4);
this.add(mk5);
this.add(mkb);
mk1.addActionListener(this);
mk2.addActionListener(this);
mk3.addActionListener(this);
mk4.addActionListener(this);
mk5.addActionListener(this);
mkb.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == mk1) {
String id = JOptionPane.showInputDialog(this, "请输入要修改的员工编号:");
try {
BufferedReader reader = new BufferedReader(new FileReader("t1.txt"));
String line;
ArrayList<String[]> list = new ArrayList<String[]>();
boolean found = false;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields[1].equals(id)) {
fields[0] = JOptionPane.showInputDialog(this, "请输入新的员工姓名:");
line = fields[0] + "," + fields[1] + "," + fields[2] + "," + "," + fields[3] + "," + fields[4];
found = true;
}
list.remove(fields[0]);
list.add(fields);
}
reader.close();
if (found) {
BufferedWriter writer = new BufferedWriter(new FileWriter("t1.txt"));
for (String[] fields : list) {
String newLine = String.join(",", fields);
writer.write(newLine);
writer.newLine();
}
writer.close();
JOptionPane.showMessageDialog(this, "员工姓名已修改。");
} else {
JOptionPane.showMessageDialog(this, "未找到该员工!");
}
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == mk2) {
String id = JOptionPane.showInputDialog(this, "请输入要修改的员工编号:");
try {
BufferedReader reader = new BufferedReader(new FileReader("t1.txt"));
String line;
ArrayList<String[]> list = new ArrayList<String[]>();
boolean found = false;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields[1].equals(id)) {
fields[1] = JOptionPane.showInputDialog(this, "请输入新的员工编号:");
line = fields[0] + "," + fields[1] + "," + fields[2] + "," + "," + fields[3] + "," + fields[4];
found = true;
}
list.remove(fields[1]);
list.add(fields);
}
reader.close();
if (found) {
BufferedWriter writer = new BufferedWriter(new FileWriter("t1.txt"));
for (String[] fields : list) {
String newLine = String.join(",", fields);
writer.write(newLine);
writer.newLine();
}
writer.close();
JOptionPane.showMessageDialog(this, "员工编号已修改。");
} else {
JOptionPane.showMessageDialog(this, "未找到该员工!");
}
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == mk3) {
String id = JOptionPane.showInputDialog(this, "请输入要修改的员工编号:");
try {
BufferedReader reader = new BufferedReader(new FileReader("t1.txt"));
String line;
ArrayList<String[]> list = new ArrayList<String[]>();
boolean found = false;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields[1].equals(id)) {
fields[2] = JOptionPane.showInputDialog(this, "请输入新的员工性别:");
line = fields[0] + "," + fields[1] + "," + fields[2] + "," + "," + fields[3] + "," + fields[4];
found = true;
}
list.remove(fields[2]);
list.add(fields);
}
reader.close();
if (found) {
BufferedWriter writer = new BufferedWriter(new FileWriter("t1.txt"));
for (String[] fields : list) {
String newLine = String.join(",", fields);
writer.write(newLine);
writer.newLine();
}
writer.close();
JOptionPane.showMessageDialog(this, "员工性别已修改。");
} else {
JOptionPane.showMessageDialog(this, "未找到该员工!");
}
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == mk4) {
String id = JOptionPane.showInputDialog(this, "请输入要修改的员工编号:");
try {
BufferedReader reader = new BufferedReader(new FileReader("t1.txt"));
String line;
ArrayList<String[]> list = new ArrayList<String[]>();
boolean found = false;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields[1].equals(id)) {
fields[3] = JOptionPane.showInputDialog(this, "请输入新的员工部门:");
line = fields[0] + "," + fields[1] + "," + fields[2] + "," + "," + fields[3] + "," + fields[4];
found = true;
}
list.remove(fields[3]);
list.add(fields);
}
reader.close();
if (found) {
BufferedWriter writer = new BufferedWriter(new FileWriter("t1.txt"));
for (String[] fields : list) {
String newLine = String.join(",", fields);
writer.write(newLine);
writer.newLine();
}
writer.close();
JOptionPane.showMessageDialog(this, "员工在职部门已修改。");
} else {
JOptionPane.showMessageDialog(this, "未找到该员工!");
}
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == mk5) {
String id = JOptionPane.showInputDialog(this, "请输入要修改的员工编号:");
try {
BufferedReader reader = new BufferedReader(new FileReader("t1.txt"));
String line;
ArrayList<String[]> list = new ArrayList<String[]>();
boolean found = false;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields[1].equals(id)) {
fields[4] = JOptionPane.showInputDialog(this, "请输入新的员工薪资等级:");
line = fields[0] + "," + fields[1] + "," + fields[2] + "," + "," + fields[3] + "," + fields[4];
found = true;
}
list.remove(fields[4]);
list.add(fields);
}
reader.close();
if (found) {
BufferedWriter writer = new BufferedWriter(new FileWriter("t1.txt"));
for (String[] fields : list) {
String newLine = String.join(",", fields);
writer.write(newLine);
writer.newLine();
}
writer.close();
JOptionPane.showMessageDialog(this, "员工薪资等级已修改。");
} else {
JOptionPane.showMessageDialog(this, "未找到该员工!");
}
} catch (IOException ex) {
ex.printStackTrace();
}
} else if (e.getSource() == mkb) {
new Windows();
this.dispose();
}
}
}
13调查类-----------------------------------------------------------------
package kcsj;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.MenuKeyEvent;
import javax.swing.event.MenuKeyListener;
public class Qurry extends JFrame implements ActionListener {
JButton jb = new JButton("返回主菜单");
JButton jk = new JButton("查询");
public Qurry() {
initFrame();
initchazhao();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("查找");
this.setSize(500, 500);// 初始位置
this.setLayout(null);
this.setLocationRelativeTo(null);
// this.setResizable(false);不能改变大小
this.setDefaultCloseOperation(3);
}
private void initchazhao() {
jb.setBounds(100, 200, 100, 50);
jk.setBounds(250, 200, 100, 50);
this.add(jb);
this.add(jk);
jb.addActionListener(this);
jk.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == jb) {
new Windows();
this.dispose();
}
if (e.getSource() == jk) {
String keyword = JOptionPane.showInputDialog(this, "请输入要查询的员工编号:");
try {
BufferedReader reader = new BufferedReader(new FileReader("t1.txt"));
String line;
String result = "";
boolean found = false;
while ((line = reader.readLine()) != null) {
String[] fields = line.split(",");
if (fields[1].equals(keyword)) {
result += "姓名:" + fields[0] + "\n" + "编号:" + fields[1] + "\n" + "性别:" + fields[2] + "\n"
+ "所在部门:" + fields[3] + "\n" + "薪资级别:" + fields[4] + "\n\n";
found = true;
}
}
reader.close();
if (found) {
JTextArea resultArea = new JTextArea(result);
JScrollPane scrollPane = new JScrollPane(resultArea);
JFrame resultFrame = new JFrame("查询结果");
resultFrame.add(scrollPane);
resultFrame.setSize(400, 200);
resultFrame.setVisible(true);
} else {
JOptionPane.showMessageDialog(this, "没有找到匹配的员工信息。");
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "读取文件时发生错误!");
ex.printStackTrace();
}
}
}
}
14显示类-----------------------------------------------------------------
package kcsj;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class Show extends JFrame implements ActionListener{
JButton kb = new JButton("返回主菜单");
JButton kl = new JButton("显示");
JTextArea show=new JTextArea();
public Show() {
initFrame();
initxianshi();
this.setVisible(true);
}
private void initFrame() {
this.setTitle("显示");
this.setSize(500, 500);// 初始位置
show.setBounds(0,0,500,500);
this.setLayout(null);
this.setLocationRelativeTo(null);
// this.setResizable(false);不能改变大小
this.setDefaultCloseOperation(3);
}
private void initxianshi() {
kb.setBounds(100, 200, 100, 50);
kl.setBounds(250, 200, 100, 50);
this.add(kb);
this.add(kl);
this.add(show);
kl.addActionListener(this);
kb.addActionListener(this);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == kb) {
new Windows();
this.dispose();
}
if (e.getSource() == kl) {
try {
BufferedReader sw = new BufferedReader(new FileReader("t1.txt"));
String line;
while ((line = sw.readLine()) != null) {
show.append(line);
show.append("\n");
}
sw.close();
} catch (IOException ex) {
JOptionPane.showMessageDialog(this, "读取文件时发生错误!");
ex.printStackTrace();
}
}
}
}
15工具类----------------------------------------------------
package kcsj;
public class Tool {
private String name;
private String num;
private String sex;
private String job;
private String level;
public Tool() {
}
public Tool(String name, String num, String sex, String job, String level) {
super();
this.name = name;
this.num = num;
this.sex = sex;
this.job = job;
this.level = level;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNum() {
return num;
}
public void setNum(String num) {
this.num = num;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getLevel() {
return level;
}
public void setLevel(String level) {
this.level = level;
}
}
菜单类---------------------------------------------------------
package kcsj;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class Windows extends JFrame implements ActionListener{
JMenuItem AddMenuItem= new JMenuItem("添加");
JMenuItem QurryMenuItem= new JMenuItem("查询");
JMenuItem ShowMenuItem= new JMenuItem("显示");
JMenuItem MoidfyMenuItem= new JMenuItem("修改");
JMenuItem DeleteMenuItem= new JMenuItem("删除");
JMenuItem CountMenuItem= new JMenuItem("统计");
JLabel b= new JLabel("欢迎来到本系统");//标签
// 设置窗口大小和关闭方式
public Windows() {
initFrame();
initMenu();
this.setVisible(true);
}
private void initMenu() {
JMenuBar menuBar = new JMenuBar();
// 创建菜单
add(b);
b.setBounds(210, 70, 250, 250);
b.setFont(new Font("宋体",Font.BOLD,20));
JMenu fileMenu = new JMenu("打开");
JMenu editMenu = new JMenu("退出");
// 监听
AddMenuItem.addActionListener(this);
QurryMenuItem.addActionListener(this);
ShowMenuItem.addActionListener(this);
MoidfyMenuItem.addActionListener(this);
DeleteMenuItem.addActionListener(this);
CountMenuItem.addActionListener(this);
// 参加菜单子类
fileMenu.add(AddMenuItem);
fileMenu.add(QurryMenuItem);
fileMenu.add(ShowMenuItem);
fileMenu.add(MoidfyMenuItem);
fileMenu.add(DeleteMenuItem);
fileMenu.add(CountMenuItem);
menuBar.add(fileMenu);
menuBar.add(editMenu);
this.setJMenuBar(menuBar);
}
private void initFrame() {
this.setTitle("小型公司工资管理系统");
this.setSize(600, 500);//初始位置
this.setResizable(false);//不能改变大小
this.setDefaultCloseOperation(3);
this.setLayout(null);
this.setLocationRelativeTo(null);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == AddMenuItem) {
new Add();
this.dispose();
}
else if (e.getSource() == QurryMenuItem) {
new Qurry();
this.dispose();
}
else if(e.getSource() == ShowMenuItem) {
new Show();
this.dispose();
}
else if(e.getSource() == MoidfyMenuItem) {
new Moidfy();
this.dispose();
}
else if(e.getSource() == DeleteMenuItem) {
new Delete();
this.dispose();
}
else if(e.getSource() == CountMenuItem) {
new Count();
this.dispose();
}
}
}