大一下学期java的一个小系统,刚拿到问题觉得很棘手,但是写了一周,反复提炼之后,发现并不困难,还是贴出来给以后的自己看看,回忆回忆吧。如果需要完整的还原,还需要截一下我的博客头像放到代码里面。
import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.TextArea;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusListener;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
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.JTextField;
public class HY extends JFrame{
/*人事信息主要包括:员工编号、所在部门、职务、薪资等级、薪资、联系方式等内容。*/
class Staff{
String name,id,xj;
String zw,tel,xz;
public String getName() {
return name;
}
public void setName(String name){
this.name=name;
}
public String getZw() {
return zw;
}
public void setZw(String zw){
this.zw=zw;
}
public String getTel() {
return tel;
}
public void setTel(String tel){
this.tel=tel;
}
public String getXz() {
return xz;
}
public void setXz(String xz){
this.xz=xz;
}
public String getId() {
return id;
}
public void setId(String id){
this.id=id;
}
public String getXj() {
return xj;
}
public void setXj(String xj){
this.xj=xj;
}
public String toString(){
return "姓名:"+this.name+"\t编号:"+this.id+"\t薪级:"+this.xj+"\t职务:"+this.zw+"\t电话:"+this.tel+"\t薪资:"+this.xz+"\n";
}
}
List<Staff> list=new ArrayList<Staff>();
String b[]=new String[100];
final JComboBox zw= new JComboBox();
// String a[]={"总裁","总经理","经理","职员","----其他职务----"};
int length=5;
class Find extends JFrame{/*Find功能*/
Container s3;
JLabel Ftitle=new JLabel("-----------------查找成员信息-----------------");
JLabel czbh1=new JLabel("编号");
JTextField czbh2=new JTextField(10);
JButton czbh3=new JButton("查找");
JLabel czxx=new JLabel("-----------------成员信息-----------------");
TextArea nr=new TextArea(5,25);
public Find(){
this.setTitle("查找信息");
s3=getContentPane();
s3.setLayout(new FlowLayout());
s3.add(Ftitle);
s3.add(czbh1);
s3.add(czbh2);
s3.add(czbh3);
s3.add(czxx);
s3.add(nr);
czbh3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Iterator<Staff> it = list.iterator();
String xs="";
boolean flag=true;
while (it.hasNext()) {
Staff value = it.next();
if (czbh2.getText().equals(value.id)) {
String message = "Find";//这句为你要显示的值
xs+=value;flag=false;
JOptionPane.showMessageDialog(rootPane, message);
break;
}
}
if(flag){
String message = "未找到该成员";//这句为你要显示的值
JOptionPane.showMessageDialog(rootPane, message);
}
nr.setText(xs);
}
});
setSize(250,300);
setLocation(200,200);
setVisible(true);
}
}
class Add extends JFrame{/*Add方法*/
JComboBox zw1 = new JComboBox();
Container s1;
JLabel Atitle=new JLabel("-----------------添加成员信息-----------------");
JLabel Aname=new JLabel("姓名");
JLabel Abh=new JLabel("编号");
JLabel Abm=new JLabel("---职务选择---:");
JLabel Axj=new JLabel("薪级");
JLabel Axz=new JLabel("薪资");
JLabel Atel=new JLabel("手机");
JTextField inname=new JTextField(15);
JTextField intel=new JTextField(15);
JTextField inbh=new JTextField(15);
JTextField inbm=new JTextField(15);
JTextField inxj=new JTextField(15);
JTextField inxz=new JTextField(15);
JButton correct =new JButton("确定");
public Add(){
this.setTitle("添加信息");
for(int i=0;i<length;i++)
zw1.addItem(b[i]);
s1=getContentPane();
s1.setLayout(new FlowLayout());
s1.add(Atitle);
s1.add(Aname);
s1.add(inname);
s1.add(Abh);
s1.add(inbh);
s1.add(Abm);
s1.add(zw1);
s1.add(Axj);
s1.add(inxj);
s1.add(Axz);
s1.add(inxz);
s1.add(Atel);
s1.add(intel);
s1.add(correct);
/*确定监听,加入新内容*/
correct.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
boolean flag=true;
Iterator<Staff> it=list.iterator();
while(it.hasNext()){
Staff value=it.next();
if(value.id.equals(inbh.getText()))
flag=false;
}
Staff tp =new Staff();
tp.name=inname.getText();
tp.id=inbh.getText();
tp.tel=intel.getText();
tp.xj=inxj.getText();
tp.xz=inxz.getText();
Object a=zw1.getSelectedItem();
tp.zw=(String) a;
if(flag){
list.add(tp);
String message = tp.name+"添加成功";//这句为你要显示的值
JOptionPane.showMessageDialog(rootPane, message);
}
else{
String message = "编号已存在";//这句为你要显示的值
JOptionPane.showMessageDialog(rootPane, message);
}
}
});
setSize(250,300);
setLocation(200,200);
setVisible(true);
}
}
class Set extends JFrame{/*Set方法*/
Container s1;
JLabel Atitle=new JLabel("-----------------修改成员信息-----------------");
JLabel Abh=new JLabel("清输入要修改的员工编号");
JLabel Abm=new JLabel("---职务选择---:");
JLabel Axj=new JLabel("薪级");
JLabel Axz=new JLabel("薪资");
JLabel Atel=new JLabel("手机");
JLabel Aname=new JLabel("姓名");
JButton Acz=new JButton("查找");
JTextField intel=new JTextField(15);
JTextField inbh=new JTextField(12);
JTextField inbm=new JTextField(15);
JTextField inname=new JTextField(15);
JTextField inxj=new JTextField(15);
JTextField inxz=new JTextField(15);
JButton correct =new JButton("确定修改");
JComboBox zw2=new JComboBox();
public Set(){
this.setTitle("修改信息");
for(int i=0;i<length;i++)
zw2.addItem(b[i]);
s1=getContentPane();
s1.setLayout(new FlowLayout());
s1.add(Atitle);
s1.add(Abh);s1.add(inbh);s1.add(Acz);
s1.add(Aname);s1.add(inname);
s1.add(Abm);s1.add(zw2);
s1.add(Axj);s1.add(inxj);
s1.add(Axz);s1.add(inxz);
s1.add(Atel);s1.add(intel);
s1.add(correct);
Acz.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Iterator<Staff> it = list.iterator();
boolean flag=true;
while (it.hasNext()) {
Staff value = it.next();
if (inbh.getText().equals(value.id)) {
String message = "查找成功";//这句为你要显示的值
flag=false;
JOptionPane.showMessageDialog(rootPane, message);
inxj.setText(value.xj);
inxz.setText(value.xz);
inname.setText(value.name);
intel.setText(value.tel);
break;
}
}
if(flag){
String message = "未找到该成员";//这句为你要显示的值
JOptionPane.showMessageDialog(rootPane, message);
}
}
});
correct.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Iterator<Staff> it = list.iterator();
while (it.hasNext()) {
Staff value = it.next();
if (inbh.getText().equals(value.id)){
Staff tp =new Staff();
tp.name=inname.getText();
tp.id=inbh.getText();
tp.tel=intel.getText();
tp.xj=inxj.getText();
tp.xz=inxz.getText();
Object a=zw2.getSelectedItem();
tp.zw=(String) a;
it.remove();
list.add(tp);
String message = tp.name+"修改成功";//这句为你要显示的值
JOptionPane.showMessageDialog(rootPane, message);
break;
}
}
}
});
setSize(250,300);
setLocation(200,200);
setVisible(true);
}
}
class SetZw extends JFrame{
Container c;
JLabel ps=new JLabel("请输入职务名称");
JTextField in =new JTextField(10);
JButton tj=new JButton("添加");
JButton sc=new JButton("删除");
public SetZw(){
this.setTitle("修改职务信息");
c=getContentPane();
c.setLayout(new FlowLayout());
c.add(ps);
c.add(in);
c.add(tj);
c.add(sc);
setSize(250,300);
setLocation(200,200);
setVisible(true);
tj.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
boolean flag=false;
for(int i=0;i<length;i++){
if(in.getText().equals(b[i])) flag=true;
}
if(flag){
String message = "职务存在";
JOptionPane.showMessageDialog(rootPane, message);
}
else {
b[length]=in.getText();
length++;
zw.addItem(b[length-1]);
String message = "添加成功";
JOptionPane.showMessageDialog(rootPane, message);
}
}
});/*添加职务*/
sc.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
boolean flag=false;
int mark = 0;
for(int i=0;i<length;i++){
if(in.getText().equals(b[i])){
flag=true;
mark=i;
break;
}
}
if(flag){
for(int i=0;i<length;i++){//主框先删除所有选项
zw.removeItem(b[i]);
}
for(int i=mark;i<length;i++){
b[i]=b[i+1];
}
length--;
for(int i=0;i<length;i++){//然后重新添加
zw.addItem(b[i]);
}
String message = "删除职务成功";
JOptionPane.showMessageDialog(rootPane, message);
}
else{
String message = "然而职务并不存在";
JOptionPane.showMessageDialog(rootPane, message);
}
}
});/*删除职务*/
}
}
class Delete extends JFrame{
String a;
Container s2;
JLabel title=new JLabel("-----------------删除成员信息-----------------");
JLabel debh=new JLabel("编号");
JTextField indebh=new JTextField(15);
JButton btbh=new JButton("删除");
public Delete(){
this.setTitle("删除信息");
s2=getContentPane();
s2.setLayout(new FlowLayout());
s2.add(debh);
s2.add(indebh);
s2.add(btbh);
setSize(250,300);
setLocation(200,200);
setVisible(true);
btbh.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Iterator<Staff> it = list.iterator();
boolean flag=true;
while (it.hasNext()) {
Staff value = it.next();
if (indebh.getText().equals(value.id)) {
it.remove();
String message = "删除成功";flag=false;//这句为你要显示的值
JOptionPane.showMessageDialog(rootPane, message);break;
}
}
if(flag){
String message = "未找到该成员";//这句为你要显示的值
JOptionPane.showMessageDialog(rootPane, message);
}
}
});
}
}
JLabel kg=new JLabel(" ");
JLabel title=new JLabel("Manager System");
JLabel author=new JLabel("Author:韩雨",JLabel.RIGHT);
JLabel fun=new JLabel("作业名称:人事管理系统 ");
JButton Find=new JButton("查询");
JButton bmcz=new JButton("按照职务查找");
TextArea xsqb=new TextArea(15,80);
JLabel lab=new JLabel();
JButton FindAll=new JButton("查询全部");
JButton Add=new JButton("添加");
JButton Set=new JButton("修改");
JButton SetZw=new JButton("修改职务信息");
JButton Delete=new JButton("删除");
JButton End=new JButton("结束");
JTextField xh=new JTextField(10);
Container c;
int len=0;
public void init(){
//FindAll.setBackground(Color.red);
for(int i=0;i<length;i++)
zw.addItem(b[i]);
c=this.getContentPane();
c.setLayout(new FlowLayout());
this.setTitle("人事管理系统");
c.setBackground(Color.LIGHT_GRAY);
ImageIcon im = new ImageIcon("013.jpg");
JLabel l1 = new JLabel(im);
c.add(l1);
title.setFont(new Font("",1,50));
title.setForeground(Color.BLUE);
author.setFont(new Font("",1,20));
author.setForeground(Color.RED);
c.add(title);
c.add(author);
c.add(fun);
c.add(Add);
c.add(Delete);
c.add(Find);
c.add(Set);
c.add(FindAll);
c.add(kg);c.add(SetZw);
c.add(zw);c.add(bmcz);
c.add(End);
c.add(xsqb);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(750,520);
setLocation(200,200);
setVisible(true);
/*Add监听*/
Add.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Add ad=new Add();
}
});
/*Delete监听*/
Delete.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Delete de=new Delete();
}
});
/*Find监听*/
Find.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Find fd=new Find();
}
});
/*End监听*/
End.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int n = JOptionPane.showConfirmDialog(null, "确认退出吗,退出不保留任何信息?", "退出", JOptionPane.YES_NO_OPTION);
if (n == JOptionPane.YES_OPTION) {
Iterator<Staff> it = list.iterator();
String hy="";
while(it.hasNext()){
Staff value = it.next();
hy+=value;
}
char[] c1=hy.toCharArray();//将字符串内容装入字符数组
char[] c2=new char[1024];
FileWriter fwOut;
try {
fwOut = new FileWriter("out2.txt");
fwOut.write(c1);
fwOut.close();
} catch (IOException e1) {
System.out.print("wrong");
e1.printStackTrace();
}
System.exit(0);
} else if (n == JOptionPane.NO_OPTION) {
}
// System.exit(0);
}
});
/*Set监听*/
Set.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
Set xg=new Set();
}
});
/*查询全部监听*/
FindAll.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
if(list.size()==0){
String message = "Not Find";
JOptionPane.showMessageDialog(rootPane, message);
xsqb.setText("");
}
else{
Iterator<Staff> it = list.iterator();
String qb="";
while (it.hasNext()) {
Staff value = it.next();
qb=qb+value;
}
String message = "查找完成";
JOptionPane.showMessageDialog(rootPane, message);
// xsqb.setFont(new Font("",1,20));
xsqb.setText(qb);
}
}
});
/*按职务查找*/
bmcz.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
Iterator<Staff> it = list.iterator();
boolean flag=true;
String s="";
while (it.hasNext()) {
Staff value = it.next();
if (zw.getSelectedItem().equals(value.zw)) {
flag=false;
s=s+value;
}
}
xsqb.setText(s);
if(flag){
String message = "未找到该成员";//这句为你要显示的值
JOptionPane.showMessageDialog(rootPane, message);
}
}
});
/*修改职务信息*/
SetZw.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
SetZw xgzw=new SetZw();
}
});
}
public HY(){
b[0]="总裁";
b[1]="总经理";
b[2]="经理";
b[3]="职员";
b[4]="----其他职务---";
init();
}
public static void main(String args[]){
HY test=new HY();
}
}