具体功能:
1.添加人员信息 2.修改人员信息 3.浏览所有人员信息 4.删除指定人员信息 5.退出系统具体细节:
1.添加时,给添加得人员一个唯一编码(uuid),姓名不可以重复
2.修改人员信息时,除了uuid不可以修改,其他信息都可以被修改(注意判断用户名重复问题)3.浏览所有人员信息,将所有信息列出
4.删除人员信息时,用户输入指定得用户名,匹配成功后才可以进行删除
添加内容:
1.设计图形界面
2.将存档内容保存进本地(通过对象序列化实现)
3.优化弹窗判断条件
4.优化内容验证方式
5.优化读取效果
6.增加登陆界面(要求用户名,密码登陆)
7.窗体内增加监听事件
实现效果:
import java.io.Serializable;
import java.util.UUID;
/**
* 1. 写一个人员信息管理系统
* 2. 提供对系统操作的菜单:添加人员信息,修改人员信息,浏览人员信息,退出系统
* 3. 其中:人员信息包含有姓名,年龄,成绩,在添加时三个都不可以为空;
* 修改时,必须先显示原来得指定信息再提示进行修改,三条信息中至少有一条被改变;
* 浏览人员信息中,显示出全部信息;退出系统,直接退出系统
* @author SUMMER
*/
public class PersonMessage implements Serializable {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 2667216823258661384L;
/**
* UUID
*/
private UUID uuid = null ;
/**
* 姓名
*/
private String name ;
/**
* 年龄
*/
private int age ;
/**
* 分数
*/
private double score ;
/**
* 无参构造
*/
public PersonMessage() {
super();
}
/**
* @param name
* @param age
* @param score
*/
public PersonMessage(String name, int age, double score) {
super();
this.name = name;
this.age = age;
this.score = score;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public int getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(int age) {
this.age = age;
}
/**
* @return the score
*/
public double getScore() {
return score;
}
/**
* @param score the score to set
*/
public void setScore(double score) {
this.score = score;
}
/**
* @return the uuid
*/
public UUID getUuid() {
return uuid;
}
/**
* @param uuid the uuid to set
*/
public void setUuid(UUID uuid) {
this.uuid = uuid;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "人员信息 [UUID:" + uuid + ", 姓名:" + name + ", 年龄:" + age + ", 成绩:" + score + "]";
}
}
用于对象序列化和反序列化,存储至指定路径中
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* 文件的读取写入类
*
* @author SUMMER
*
*/
public class FileOperate {
File file = null ;
public FileOperate(String pathName) {
file = new File(pathName);
}
/**
* 写入对象
* @param obj
* @return
*/
public boolean save(Object obj) {
ObjectOutputStream objectOutputStream = null ;
boolean sign = false ;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream(file));
objectOutputStream.writeObject(obj);
sign = true ;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
objectOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return sign ;
}
/**
* 读取对象
* @return
*/
public Object read() {
Object object = null ;
ObjectInputStream objectInputStream = null ;
try {
if (file.length()==0) {
this.save(PersonnelManagementArea.personList);
} else {
objectInputStream = new ObjectInputStream(new FileInputStream(file));
object = objectInputStream.readObject();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
objectInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return object;
}
}
创建一个登陆界面,验证用户名和密码
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.JButton;
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.border.Border;
import javax.swing.border.MatteBorder;
import study1026.PersonnelManagementArea;
public class LoginArea extends JFrame {
public static final String USERNAME = "admin" ;
public static final String PASSWORD = "admin" ;
public LoginArea() {
Container container = this.getContentPane();
container.setBackground(Color.WHITE);
container.setLayout(null);
container.addKeyListener(new KeyAdapter() {
public void keyReleased(KeyEvent paramKeyEvent) {
// TODO Auto-generated method stub
if (paramKeyEvent.getKeyCode()==paramKeyEvent.VK_ENTER) {
dispose();
new PersonnelManagementArea();
}else {
JOptionPane.showMessageDialog(null, "用户名或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
JPanel jPanel1 = new JPanel();
JLabel lab = new JLabel("艾盈科技", JLabel.CENTER);
JLabel label = new JLabel("人员管理系统用户登陆", JLabel.CENTER);
Font font = new Font("华文彩云", Font.PLAIN, 18);
lab.setFont(font);
label.setFont(font);
label.setForeground(Color.RED);
jPanel1.setLayout(new BorderLayout());
// jPanel1.setBackground(Color.blue);
jPanel1.setBackground(Color.WHITE);
jPanel1.setBounds(10, 5, 300, 50);
jPanel1.add(lab, BorderLayout.NORTH);
jPanel1.add(label, BorderLayout.SOUTH);
container.add(jPanel1);
JPanel jPanel2 = new JPanel();
Font fontText = new Font("幼圆", Font.PLAIN, 20);
Border border = new MatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY);
jPanel2.setLayout(null);
jPanel2.setBackground(Color.WHITE);
jPanel2.setBounds(5, 80, 300, 50);
JLabel jLabel2 = new JLabel("用户名:");
jLabel2.setFont(fontText);
jLabel2.setBounds(10,5,80,40);
JTextField jTextPane2 = new JTextField();
jTextPane2.setBorder(border);
jTextPane2.setBounds(90,10,200,30);
jTextPane2.setFont(fontText);
jPanel2.add(jLabel2);
jPanel2.add(jTextPane2);
container.add(jPanel2);
JPanel jPanel3 = new JPanel();
Font fontPassword = new Font("幼圆", Font.PLAIN, 20);
jPanel3.setLayout(null);
jPanel3.setBackground(Color.WHITE);
jPanel3.setBounds(5, 140, 300, 50);
JLabel jLabel3 = new JLabel("密 码:");
jLabel3.setFont(fontPassword);
jLabel3.setBounds(10,5,80,40);
JPasswordField jTextPane3 = new JPasswordField();
jTextPane3.setEchoChar('*');
jTextPane3.setBorder(border);
jTextPane3.setBounds(90,10,200,30);
jTextPane3.setFont(fontPassword);
jPanel3.add(jLabel3);
jPanel3.add(jTextPane3);
container.add(jPanel3);
JButton jButton = new JButton("登陆");
jButton.setBounds(80, 210, 160, 35);
jButton.setFont(fontPassword);
jButton.setBackground(Color.WHITE);
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent paramActionEvent) {
// TODO Auto-generated method stub
String box1 = jTextPane2.getText();
String box2 = new String(jTextPane3.getPassword());
if (box1.equals(USERNAME)&&box2.equals(PASSWORD)) {
dispose();
new PersonnelManagementArea();
}else {
JOptionPane.showMessageDialog(null, "用户名或密码错误", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
container.add(jButton);
this.setTitle("用户登陆");
this.setSize(320, 340);
this.setVisible(true);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setIconImage(Toolkit.getDefaultToolkit().createImage("F:\\下载\\登陆.png"));
}
}
主窗体构建
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.regex.Pattern;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.Border;
import javax.swing.border.MatteBorder;
/**
* 人员管理系统主窗体
*
* @author SUMMER
*/
public class PersonnelManagementArea extends JFrame {
/**
* serialVersionUID
*/
private static final long serialVersionUID = 6230023438297951930L;
/**
* 人员信息集合
*/
public static List<PersonMessage> personList = new ArrayList<PersonMessage>();
/**
* 实例对象序列化反序列化操作类
*/
FileOperate fileOperate = new FileOperate("C:\\Users\\42952\\Desktop\\test\\PersonMessageCaChe.txt");
/**
* 窗体主体
*/
public PersonnelManagementArea() {
/**
* 读取本地存储的人员信息
*/
this.personList = (List<PersonMessage>) fileOperate.read();
Container container = this.getContentPane();
container.setBackground(Color.WHITE);
container.setLayout(null);
JPanel jPanel1 = new JPanel();
JLabel lab = new JLabel("艾 盈 科 技", JLabel.CENTER);
JLabel label = new JLabel("人 员 管 理 系 统", JLabel.CENTER);
Font font = new Font("华文彩云", Font.PLAIN, 30);
lab.setFont(font);
label.setFont(font);
label.setForeground(Color.RED);
jPanel1.setLayout(new BorderLayout());
// jPanel1.setBackground(Color.blue);
jPanel1.setBackground(Color.WHITE);
jPanel1.setBounds(50, 5, 360, 70);
jPanel1.add(lab, BorderLayout.NORTH);
jPanel1.add(label, BorderLayout.SOUTH);
container.add(jPanel1);
/**
* 文本域
*/
JTextArea jTextArea = new JTextArea("系统运行中 ...");
JScrollPane jScrollPane = new JScrollPane(jTextArea);
Border borderi = new MatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY);
jTextArea.setLineWrap(true);// 激活自动换行功能
jTextArea.setWrapStyleWord(true); // 激活换行不断字
// jTextArea.setEnabled(false);//设置文本框不可输入
jTextArea.setFont(new Font("幼圆", font.PLAIN, 16));
jTextArea.setForeground(Color.BLACK);
jTextArea.setBackground(Color.WHITE);
jScrollPane.setBorder(borderi);
jScrollPane.setBounds(30, 85, 400, 200);
container.add(jScrollPane);
JPanel jPanel3 = new JPanel();// 提供对系统操作的菜单:添加人员信息,修改人员信息,浏览人员信息,退出系统
jPanel3.setBackground(Color.WHITE);
jPanel3.setBounds(30, 300, 400, 165);
Font fontButton = new Font("幼圆", Font.PLAIN, 22);
jPanel3.setLayout(new GridLayout(5, 1, 0, 5));
/**
* 按钮1
*/
JButton but1 = new JButton("添加人员信息");
but1.setFont(fontButton);
but1.setBackground(Color.WHITE);
but1.addActionListener(new ActionListener() {
/**
* 添加人员信息:添加人员是增加一个UUID,姓名不可重复
*/
@Override
public void actionPerformed(ActionEvent paramActionEvent) {
// TODO Auto-generated method stub
String name = null;
int age = 0;
double score = 0;
String box1 = null;
String box2 = null;
String box3 = null;
box1 = JOptionPane.showInputDialog("请输入存档人员姓名:");
if (box1 == null || box1.equals("")) {
JOptionPane.showMessageDialog(null, "姓名不可为空", "错误",
JOptionPane.ERROR_MESSAGE);
} else {
boolean flag = false;
for (int i = 0; i < personList.size(); i++) {
if (personList.get(i).getName().equals(box1)) {
flag = true;
break;
}
}
if (flag) {// 旗帜
JOptionPane.showMessageDialog(null, "当前信息档案已存在", "错误", JOptionPane.ERROR_MESSAGE);
} else {
name = box1;
box2 = JOptionPane.showInputDialog("请输入存档人员年龄:");
if (box2 == null || box1.equals("")) {
JOptionPane.showMessageDialog(null, "年龄不可为空", "错误",
JOptionPane.ERROR_MESSAGE);
} else {
if (Pattern.compile("^\\d+$").matcher(box2).matches()) {
age = Integer.parseInt(box2);
box3 = JOptionPane.showInputDialog("请输入存档人员分数:");
if (box3 == null || box1.equals("")) {
JOptionPane.showMessageDialog(null, "成绩不可为空", "错误",
JOptionPane.ERROR_MESSAGE);
} else {
if (Pattern.compile("^\\d+.?\\d+$").matcher(box3).matches()) {
score = Double.parseDouble(box3);
PersonMessage personMessage = new PersonMessage(name, age, score);
personMessage.setUuid(UUID.randomUUID());
personList.add(personMessage);
fileOperate.save(personList);
jTextArea.setText("正在存档信息 ...");
jTextArea.setText(jTextArea.getText() + "\n" + "当前人员信息保存成功!");
} else {
JOptionPane.showMessageDialog(null, "信息录入错误,请重新输入", "错误",
JOptionPane.ERROR_MESSAGE);
}
}
} else {
JOptionPane.showMessageDialog(null, "信息录入错误,请重新输入", "错误", JOptionPane.ERROR_MESSAGE);
}
}
}
}
}
});
jPanel3.add(but1);
/**
* 按钮2
*/
JButton but2 = new JButton("修改人员信息");
but2.setFont(fontButton);
but2.setBackground(Color.WHITE);
but2.addActionListener(new ActionListener() {
/**
* 更新人员信息:UUID不可更改,注意判断用户名重复
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String name = null;
int age = 0;
double score = 0;
String box0 = null;
String box1 = null;
String box2 = null;
String box3 = null;
box0 = JOptionPane.showInputDialog("请输入要修改人员的姓名");
boolean flag = false;
int ax = 0;
for (int i = 0; i < personList.size(); i++) {
if (box0.equals(personList.get(i).getName())) {
ax = i;
flag = true;
break;
}
}
if (flag) {
box1 = JOptionPane.showInputDialog("请输入修改后的姓名:");
if (box1 == null || box1.equals("")) {
JOptionPane.showMessageDialog(null, "姓名不可为空", "错误",
JOptionPane.ERROR_MESSAGE);
} else {
boolean flagUpDate = false;
for (int j = 0; j < personList.size(); j++) {
if (personList.get(j).getName().equals(box1)) {
flagUpDate = true;
}
}
if (flagUpDate) {
JOptionPane.showMessageDialog(null, "当前信息档案已存在", "错误", JOptionPane.ERROR_MESSAGE);
} else {
name = box1;
box2 = JOptionPane.showInputDialog("请输入修改后的年龄:");
if (box2 == null || box1.equals("")) {
JOptionPane.showMessageDialog(null, "年龄不可为空", "错误",
JOptionPane.ERROR_MESSAGE);
} else {
if (Pattern.compile("^\\d+$").matcher(box2).matches()) {
age = Integer.parseInt(box2);
box3 = JOptionPane.showInputDialog("请输入修改后的分数:");
if (box3 == null || box1.equals("")) {
JOptionPane.showMessageDialog(null, "分数不可为空", "错误",
JOptionPane.ERROR_MESSAGE);
} else {
if (Pattern.compile("^\\d+.?\\d+$").matcher(box3).matches()) {
score = Double.parseDouble(box3);
personList.get(ax).setName(name);
personList.get(ax).setAge(age);
personList.get(ax).setScore(score);
fileOperate.save(personList);
jTextArea.setText("正在存档信息 ...");
jTextArea.setText(jTextArea.getText() + "\n" + "当前人员信息保存成功!");
} else {
JOptionPane.showMessageDialog(null, "信息录入错误,请重新输入", "错误",
JOptionPane.ERROR_MESSAGE);
}
}
} else {
JOptionPane.showMessageDialog(null, "信息录入错误,请重新输入", "错误",
JOptionPane.ERROR_MESSAGE);
}
}
}
}
} else {
JOptionPane.showMessageDialog(null, "未检索到档案信息", "错误", JOptionPane.ERROR_MESSAGE);
}
}
});
jPanel3.add(but2);
/**
* 按钮5
*/
JButton but5 = new JButton("删除人员信息");
but5.setFont(fontButton);
but5.setBackground(Color.WHITE);
but5.addActionListener(new ActionListener() {
/**
* 删除人员信息:要求输入指定的用户名或则UUID,匹配成功后才可以进行删除
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String box = JOptionPane.showInputDialog("请输入被删除人员姓名:");
boolean flag = false;
if (box != null) {
for (int i = 0; i < personList.size(); i++) {
if (box.equals(personList.get(i).getName())) {
personList.remove(i);
flag = true;
break;
}
}
if (flag) {
jTextArea.setText("正在删除信息 ...");
jTextArea.setText(jTextArea.getText() + "\n" + "当前人员信息删除成功!");
} else {
JOptionPane.showMessageDialog(null, "未索引相关信息", "错误", JOptionPane.ERROR_MESSAGE);
}
fileOperate.save(personList);
}
}
});
jPanel3.add(but5);
/**
* 按钮3
*/
JButton but3 = new JButton("浏览人员信息");
but3.setFont(fontButton);
but3.setBackground(Color.WHITE);
but3.addActionListener(new ActionListener() {
/**
* 查找人员信息:浏览所有人员信息,将所有信息列出
*/
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
jTextArea.setText("存档信息浏览" + "\n");
// for (PersonMessage pm : personList) {
// jTextArea.setText(jTextArea.getText()+"-----------------"+"\n");
// jTextArea.setText(jTextArea.getText()+"UUID:"+pm.getUuid()+"\n");
// jTextArea.setText(jTextArea.getText()+"姓名:"+pm.getName()+"\n");
// jTextArea.setText(jTextArea.getText()+"年龄:"+pm.getAge()+"\n");
// jTextArea.setText(jTextArea.getText()+"成绩:"+pm.getScore()+"\n");
// jTextArea.setText(jTextArea.getText()+"-----------------"+"\n");
// }
// if (personList.size()==0) {
// jTextArea.setText("当前系统无存档信息");
// }
Iterator<PersonMessage> iterator = personList.iterator();
while (iterator.hasNext()) {
PersonMessage pm = iterator.next();
jTextArea.setText(jTextArea.getText() + "-----------------" + "\n");
// jTextArea.setText(jTextArea.getText()+"UUID:"+pm.getUuid()+"\n");
// jTextArea.setText(jTextArea.getText()+"姓名:"+pm.getName()+"\n");
// jTextArea.setText(jTextArea.getText()+"年龄:"+pm.getAge()+"\n");
// jTextArea.setText(jTextArea.getText()+"成绩:"+pm.getScore()+"\n");
jTextArea.setText(jTextArea.getText() + " 姓名:" + pm.getName() + ";");
jTextArea.setText(jTextArea.getText() + " 年龄:" + pm.getAge() + ";");
jTextArea.setText(jTextArea.getText() + " 成绩:" + pm.getScore() + ";\n");
// jTextArea.setText(jTextArea.getText()+"-----------------"+"\n");
}
if (personList.size() == 0) {
jTextArea.setText("当前系统无存档信息");
}
}
});
jPanel3.add(but3);
/**
* 按钮4
*/
JButton but4 = new JButton("退出管理系统");
but4.setFont(fontButton);
but4.setBackground(Color.WHITE);
but4.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(1);
}
});
jPanel3.add(but4);
container.add(jPanel3);
this.setTitle("人员管理系统");
this.setSize(460, 520);
this.setVisible(true);
this.setResizable(false);
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setIconImage(Toolkit.getDefaultToolkit().createImage("F:\\下载\\人员管理.png"));
}
}
主函数
public class PersonnelManagementMain {//人员管理系统
public static void main(String[] args) {
new LoginArea();
// new PersonnelManagementArea();
// new Menu();
}
}