大二用java开发的一个不用数据库支持的通讯录

在大二第一学期的时候,在学习了JAVA中的输入输出流后老师要我们做一个通讯录。其功能可以添加删除和在关闭时保存信息,保存和导入数据用的技术是纯粹的IO流,没有设计到数据库,呵呵,功能是实现出来了,但效率可能不怎么好,希望高手们看完后给小弟点意见,我会改进的,OK,以下是我的原代码和试用方法说明:

代码:

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.io.*;
import javax.swing.border.*;
import java.util.*;
class People implements Serializable
{
static int j;
static int j2=0;
static int j3=0;
static int munber=0; //这个就是整个程序的计数器


private static final long serialVersionUID = 3757263881374182471L;
String ID;
String Name;
String Address;
String Phone;
String Work;
protected Object length;
People(String ID,String Name,String Address,String Phone,String Work)
{
this.ID=ID;
this.Name=Name;
this.Address=Address;
this.Phone=Phone;
this.Work=Work;


}

String getID()
{

return ID;

}

String getName()
{
return Name;


}

String getAddress()
{
return Phone;

}

String getWork()
{

return Work;
}

static public String id()//变数据为字符
{
int jj=j;
Integer id=new Integer(2);
id=jj;
String iD=id.toString();

return iD;

}
static public String id2()//添加自加功能的方法,显示在文本框上
{
int j1=j+1;
Integer id2=new Integer(1);
id2=j1;
String jd=id2.toString();
return jd;



}

public String getValue()
{
String value="ID号:"+ this.ID+" "+"姓名:"+this.Name+" "+"地址:"+this.Address+" "+"电话:"+this.Phone+" "+"单位:"+this.Work+'\n';
return value;


}
}


public class tongxunlu extends JFrame
{

private static final long serialVersionUID = 1057782438609006743L;
//初始化窗口实现类
JTextField ID;
JTextField Name;
JTextField Address;
JTextField Phone;
JTextField Work;

JTextField IDc;
JTextField Namec;
JTextField Addressc;
JTextField Phonec;
JTextField Workc;

JButton Add;
JButton Clear;
JButton Load;
JButton Save;
JButton Delect;
JButton Check;
JPanel P1;
JPanel P2;
JTextArea Show;
JTextArea Show2;
JTabbedPane p;
Box baseBox,baseBox2,boxV1,boxV2,boxV3,boxV4,boxV5;

FileOutputStream fileOut;
ObjectOutputStream objectOut;
FileInputStream fileIn;
ObjectInputStream objectIn;
FileOutputStream fileOut2;
FileInputStream fileIn2;
DataInputStream dataInput;
DataOutputStream dataOutput;
FileWriter out;
FileReader in;

People p1[]=new People[100]; //构造存储数组
People personLoad[]=new People[100];
People personSave[]=new People[100];
People person[]=new People[100]; //定义person为保存对象的数组,容量是100个对象


tongxunlu (String s)
{ super(s);
Container con=getContentPane();
Show=new JTextArea();
Show2=new JTextArea();
p=new JTabbedPane();
P1=new JPanel();
P2=new JPanel();

ID=new JTextField(4);
Name=new JTextField(6);
Address=new JTextField(20);
Phone=new JTextField(8);
Work=new JTextField(18);

IDc=new JTextField(4);
Namec=new JTextField(6);
Addressc=new JTextField(20);
Phonec=new JTextField(8);
Workc=new JTextField(18);

Add=new JButton("添加");
Clear=new JButton("清空");
Load=new JButton("导入资料");
Delect=new JButton("删除");
Save=new JButton("保存");
Check=new JButton("查询");

boxV1=Box.createVerticalBox();
boxV1.add(new JLabel("输入您的ID:"));
boxV1.add(Box.createVerticalStrut(31));
boxV1.add(new JLabel("请输入您的姓名:"));
boxV1.add(Box.createVerticalStrut(31));
boxV1.add(new JLabel("请输入您的地址:"));
boxV1.add(Box.createVerticalStrut(31));
boxV1.add(new JLabel("请输入您的电话:"));
boxV1.add(Box.createVerticalStrut(31));
boxV1.add(new JLabel("请输入您的单位:"));
boxV2=Box.createVerticalBox();
boxV2.add(ID);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(Name);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(Address);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(Phone);
boxV2.add(Box.createVerticalStrut(8));
boxV2.add(Work);
baseBox=Box.createHorizontalBox();
baseBox.add(boxV1);
baseBox.add(Box.createHorizontalStrut(10));
baseBox.add(boxV2);

boxV3=Box.createVerticalBox();
boxV3.add(new JLabel("输入您要查找或删除的ID:"));
boxV3.add(Box.createVerticalStrut(8));
boxV4=Box.createVerticalBox();
boxV4.add(Box.createVerticalStrut(100));
boxV4.add(IDc);
boxV4.add(Box.createVerticalStrut(110));

boxV5=Box.createVerticalBox();
boxV5.add(Check);
boxV5.add(Box.createVerticalStrut(8));
boxV5.add(Delect);
boxV5.add(Box.createVerticalStrut(8));

baseBox2=Box.createHorizontalBox();
baseBox2.add(boxV3);
baseBox2.add(Box.createHorizontalStrut(10));
baseBox2.add(boxV4);
baseBox2.add(Box.createHorizontalStrut(10));
baseBox2.add(boxV5);




p.add("添加信息", baseBox);
p.add("查询或删除", baseBox2);
p.add("显示",new JScrollPane(Show));
p.add("资料库",new JScrollPane(Show2));
p.validate();

P2.add(Add);
P2.add(Clear);
P2.add(Save);
P2.add(Load);
con.add(p,BorderLayout.CENTER);
con.add(P2,BorderLayout.SOUTH);
con.validate();
setBounds(0,0,333,333);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);



Add.addActionListener(new ActionListener(){ //至此完成了窗口的设置.以下开始功能实现了
public void actionPerformed(ActionEvent e){
String iid=ID.getText();
People.j=Integer.parseInt(iid);
ID.setText(People.id2());
String name=Name.getText();
String address=Address.getText();
String phone=Phone.getText();
String work=Work.getText();
int count=People.j-1; //记录添加人数下标
try{
personSave[count]=new People(People.id(),name,address,phone,work);
String value=personSave[count].getValue();
String value2="您刚刚添加的信息是:"+value;
Show.insert(value2,Show.getDocument().getLength());//可在当前显示插入信息

}
catch(Exception a){System.out.println(a);}
}
});

Save.addActionListener(new ActionListener(){ //此方法可以把保存数组里面的所有当前对象写入文件
public void actionPerformed(ActionEvent e2){
try{
File f=new File("a.txt");
fileOut=new FileOutputStream(f);
objectOut=new ObjectOutputStream(fileOut);
}
catch(Exception a)
{
System.out.println(a);
}
for(int i2=0;i2<=People.j-1;i2++)
{

try {

objectOut.writeObject(personSave[i2]);



}
catch (IOException e22) {

e22.printStackTrace();
}

}

}

});


Load.addActionListener(new ActionListener(){ //此方法可以把文件里面的保存对象读入
public void actionPerformed(ActionEvent e2){

try{
int n=0;
fileIn=new FileInputStream("a.txt");
objectIn=new ObjectInputStream(fileIn);
while(( personSave[n]=(People)objectIn.readObject())!=null)
{ Show2.insert(personSave[n].getValue(),Show2.getDocument().getLength());
People.munber=n; //计数器计数
n=n+1;

}


}

catch(Exception a)
{System.out.println(a.getMessage()); }
}});


Clear.addActionListener(new ActionListener(){ //此方法可以清楚文本框,方便填写
public void actionPerformed(ActionEvent e){
Address.setText("");
Name.setText("");
Work.setText("");
Phone.setText("");
}
});


Check.addActionListener(new ActionListener(){ //此方法运用数据结构方法在链表里进行查找
public void actionPerformed(ActionEvent e){
LinkedList<People> list=new LinkedList<People>();
for(int k=0;k<=People.munber;k++)
{
list.add(personSave[k]);

}
int f=0;
String iid=IDc.getText(); //把文本框的字符转为可以参加运算的整型数字
f=Integer.parseInt(iid);
People find=list.get(f-1);
String message="您要查找的人的信息是:"+">"+find.getValue()+"\n";
Show.insert(message,Show.getDocument().getLength());
}
});



Delect.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
LinkedList<People> list=new LinkedList<People>();
for(int k=0;k<=People.munber;k++)
{
list.add(personSave[k]);

}
int d=0;
String iid=IDc.getText();
d=Integer.parseInt(iid);
People delect=list.remove(d-1);
String message="您已经删除了的人的信息是:"+">"+delect.getValue()+"\n";
Show.insert(message,Show.getDocument().getLength());
// People replace=list.set(d-1, new People("", "", "", "", ""));

try{ //通过覆盖达到利用数据结构删除文件信息的功能
File f=new File("a.txt");
fileOut=new FileOutputStream(f);
objectOut=new ObjectOutputStream(fileOut);
}
catch(Exception a)
{
System.out.println(a);
}
for(int i2=0;i2<=People.munber;i2++)
{
personSave[i2]=list.get(i2);


try {

objectOut.writeObject(personSave[i2]);



}
catch (IOException e22) {

e22.printStackTrace();
}

}




}
});


}


public static void main(String args[])
{

new tongxunlu("通讯录");

}




}
  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值