package cff.docx
《package cff.docx》由会员分享,可在线阅读,更多相关《package cff.docx(14页珍藏版)》请在冰豆网上搜索。
![package cff.docx](https://file1.bdocx.com/fileroot1/2023-3/18/3e3b440b-aa8e-488c-9d69-4f3bfff90b23/3e3b440b-aa8e-488c-9d69-4f3bfff90b231.gif)
packagecff
packagecff;
importjava.awt.*;
importjava.awt.event.*;
importjavax.swing.*;
importjava.io.*;
importjava.util.*;
classMessageimplementsjava.io.Serializable{
Stringnumber,name,mail,personaddress,gender,qq;
publicMessage(){};
publicvoidsetNumber(Stringnumber){this.number=number;}
publicStringgetNumber(){returnnumber;}
publicvoidsetName(Stringname){this.name=name;}
publicStringgetName(){returnname;}
publicvoidsetMail(Stringmail){this.mail=mail;}
publicStringgetMail(){returnmail;}
publicvoidsetQQ(Stringqq){this.qq=qq;}
publicStringgetQQ(){returnqq;}
publicvoidsetPersonaddress(Stringpersonaddress){this.personaddress=personaddress;}
publicStringgetPersonaddress(){returnpersonaddress;}
publicvoidsetGender(Stringgender){this.gender=gender;}
publicStringgetGender(){returngender;}
}
publicclassTXLextendsJFrame{
JTextFieldPhoneNumber,PhoneName,PhoneEmail,QQ,PhoneAddress,Gender;
JTextAreashow=newJTextArea(10,48);
JButtonInsert,Inquire,Delete,Alter,Display;
JPanelp1,p2,p3,p4,p5,p6,pv,ph,ps;
JLabelJ1,J2,J3,J4,J5,J6;
MessagePerson=null;
HashtablePersonHashtable=null;
StringTips=("\n\n"+"〖温馨提示〗:
"+"\n"+"☆添加联系人前请先输入联系人的电话号码"+"\n\n"+
"☆查询和删除都是以姓名为关键字进行操作"+"\n\n"+
"☆修改是对查询的信息进行修改以后的保存"+"\n\n");
Filefile=null;
FileInputStreaminOne=null;
ObjectInputStreaminTwo=null;
FileOutputStreamoutOne=null;
ObjectOutputStreamoutTwo=null;
publicTXLqin(){
super("java通讯录陈菲菲");
show.setEditable(false);
show.setText(Tips);
show.setForeground(Color.green);
//show.setBackground(Color.blue);
PhoneNumber=newJTextField(10);
PhoneName=newJTextField(10);
PhoneEmail=newJTextField(10);
PhoneAddress=newJTextField(10);
QQ=newJTextField(10);
Gender=newJTextField(10);
J1=newJLabel("姓名:
");
J2=newJLabel("性别:
");
J3=newJLabel("电话号码:
");
J4=newJLabel("QQ号码:
");
J5=newJLabel("电子邮件:
");
J6=newJLabel("地址:
");
J1.setForeground(Color.green);
J2.setForeground(Color.green);
J3.setForeground(Color.green);
J4.setForeground(Color.green);
J5.setForeground(Color.green);
J6.setForeground(Color.green);
PhoneNumber.setForeground(Color.green);
PhoneName.setForeground(Color.green);
PhoneEmail.setForeground(Color.green);
PhoneAddress.setForeground(Color.green);
QQ.setForeground(Color.green);
Gender.setForeground(Color.green);
//group=newButtonGroup();
Insert=newJButton("添加");
Insert.setForeground(Color.green);
Inquire=newJButton("查询");
Inquire.setForeground(Color.green);
Alter=newJButton("修改");
Alter.setForeground(Color.green);
Delete=newJButton("删除");
Delete.setForeground(Color.green);
Display=newJButton("显示");
Display.setForeground(Color.green);
Insert.addActionListener(newInputAct());
Inquire.addActionListener(newInquestAct());
Alter.addActionListener(newModifyAct());
Display.addActionListener(newShowAct());
Delete.addActionListener(newDeleteAct());
Alter.setEnabled(false);
p1=newJPanel();
p1.add(J1,J1.CENTER);
p1.add(PhoneName);
p2=newJPanel();
p2.add(J2,J2.CENTER);
p2.add(Gender);
p3=newJPanel();
p3.add(J3,J3.CENTER);
p3.add(PhoneNumber);
p4=newJPanel();
p4.add(J4,J4.CENTER);
p4.add(QQ);
p5=newJPanel();
p5.add(J5,J5.CENTER);
p5.add(PhoneEmail);
p6=newJPanel();
p6.add(J6,J6.CENTER);
p6.add(PhoneAddress);
pv=newJPanel();
pv.setLayout(newGridLayout(7,1));
pv.add(p1);
pv.add(p2);
pv.add(p3);
pv.add(p4);
pv.add(p5);
pv.add(p6);
ps=newJPanel();
ps.add(show);
ph=newJPanel();
ph.add(Insert);
ph.add(Inquire);
ph.add(Alter);
ph.add(Delete);
ph.add(Display);
file=newFile("PersonMessage.txt");
PersonHashtable=newHashtable();
if(!
file.exists()){
try{
FileOutputStreamout=newFileOutputStream(file);
ObjectOutputStreamobjectOut=newObjectOutputStream(out);
objectOut.writeObject(PersonHashtable);
objectOut.close();
out.close();
}
catch(IOExceptione){}
}
Containercon=getContentPane();
con.setLayout(newBorderLayout());
con.add(pv,BorderLayout.WEST);
con.add(ps,BorderLayout.EAST);
con.add(ph,BorderLayout.SOUTH);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setBounds(100,100,750,300);
setVisible(true);
}
classInputActimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
Alter.setEnabled(false);
Stringnumber="";
number=PhoneName.getText();
if(number.length()>0){
try{
inOne=newFileInputStream(file);
inTwo=newObjectInputStream(inOne);
PersonHashtable=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exceptionee){System.out.println("创建散列表出现问题!
");}
if(PersonHashtable.containsKey(number)){
Stringwarning="该联系人信息已存在,请到修改页面修改!
";
show.setText(Tips);
JOptionPane.showMessageDialog(null,warning,"警告",
JOptionPane.WARNING_MESSAGE);
}//endif1
else{
Stringm="该联系人信息将被添加!
";
intok=JOptionPane.showConfirmDialog(null,m,"确认",
JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE);
if(ok==JOptionPane.YES_OPTION){
Stringname=PhoneName.getText();
Stringnumbe=PhoneNumber.getText();
Stringmail=PhoneEmail.getText();
Stringpersonaddress=PhoneAddress.getText();
Stringqq=QQ.getText();
Stringgender=Gender.getText();
Person=newMessage();
Person.setName(name);
Person.setGender(gender);
Person.setNumber(numbe);
Person.setQQ(qq);
Person.setMail(mail);
Person.setPersonaddress(personaddress);
try{
outOne=newFileOutputStream(file);
outTwo=newObjectOutputStream(outOne);
PersonHashtable.put(number,Person);
outTwo.writeObject(PersonHashtable);
outTwo.close();
outOne.close();
}
catch(Exceptionee){System.out.println("输出散列表出现问题!
");}
PhoneNumber.setText(null);
PhoneName.setText(null);
PhoneEmail.setText(null);
PhoneAddress.setText(null);
QQ.setText(null);
Gender.setText(null);
}
}
}
else{
Stringwarning="必须输入电话号码!
";
show.setText(Tips);
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
}
classInquestActimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
Stringnumber="";
number=PhoneName.getText();
if(number.length()>0){
try{
inOne=newFileInputStream(file);
inTwo=newObjectInputStream(inOne);
PersonHashtable=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exceptionee){System.out.println("散列表有问题!
");}
if(PersonHashtable.containsKey(number)){
Alter.setEnabled(true);
Messagestu=(Message)PersonHashtable.get(number);
PhoneName.setText(stu.getName());
Gender.setText(stu.getGender());
PhoneNumber.setText(stu.getNumber());
QQ.setText(stu.getQQ());
PhoneEmail.setText(stu.getMail());
PhoneAddress.setText(stu.getPersonaddress());
show.setText("姓名:
"+stu.getName()+"\n"+"性别:
"+stu.getGender()+"\n"+
"电话号码:
"+stu.getNumber()+"\n"+"QQ号码:
"+stu.getQQ()+"\n"+"电子邮件:
"+
stu.getMail()+"\n"+"地址:
"+stu.getPersonaddress());
}
else{
Alter.setEnabled(false);
Stringwarning="该联系人不存在!
";
show.setText(Tips);
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
else{
Alter.setEnabled(false);
Stringwarning="必须输入姓名!
";
show.setText(Tips);
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}
}
classModifyActimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
Stringnumber=PhoneNumber.getText();
Stringname=PhoneName.getText();
Stringmail=PhoneEmail.getText();
Stringpersonaddress=PhoneAddress.getText();
Stringqq=QQ.getText();
Stringgender=Gender.getText();
MessagePerson=newMessage();
Person.setNumber(number);
Person.setName(name);
Person.setMail(mail);
Person.setPersonaddress(personaddress);
Person.setQQ(qq);
Person.setGender(gender);
try{
outOne=newFileOutputStream(file);
outTwo=newObjectOutputStream(outOne);
PersonHashtable.put(name,Person);
outTwo.writeObject(PersonHashtable);
outTwo.close();
outOne.close();
PhoneNumber.setText(null);
PhoneName.setText(null);
PhoneEmail.setText(null);
PhoneAddress.setText(null);
QQ.setText(null);
Gender.setText(null);
}
catch(Exceptionee){
System.out.println("添加修改出现异常!
");
Alter.setEnabled(false);
}
}
}
classDeleteActimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
Alter.setEnabled(false);
Stringnumber=PhoneName.getText();
if(number.length()>0){
try{
inOne=newFileInputStream(file);
inTwo=newObjectInputStream(inOne);
PersonHashtable=(Hashtable)inTwo.readObject();
inOne.close();
inTwo.close();
}
catch(Exceptionee){}
if(PersonHashtable.containsKey(number)){
Messagestu=(Message)PersonHashtable.get(number);
PhoneName.setText(stu.getName());
PhoneEmail.setText(stu.getMail());
PhoneNumber.setText(stu.getNumber());
PhoneAddress.setText(stu.getPersonaddress());
QQ.setText(stu.getQQ());
Gender.setText(stu.getGender());
Stringm="确定要删除该联系人的信息吗?
";
intok=JOptionPane.showConfirmDialog(null,m,"确认",
JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
if(ok==JOptionPane.YES_OPTION){
PersonHashtable.remove(number);
try{
outOne=newFileOutputStream(file);
outTwo=newObjectOutputStream(outOne);
outTwo.writeObject(PersonHashtable);
outTwo.close();
outOne.close();
PhoneNumber.setText(null);
PhoneName.setText(null);
PhoneEmail.setText(null);
PhoneAddress.setText(null);
QQ.setText(null);
Gender.setText(null);
show.setText(Tips);
}
catch(Exceptionee){System.out.println(ee);}
}
elseif(ok==JOptionPane.NO_OPTION){
PhoneNumber.setText(null);
PhoneName.setText(null);
PhoneEmail.setText(null);
PhoneAddress.setText(null);
QQ.setText(null);
Gender.setText(null);
}}
else{
Stringwarning="该联系人不存在!
";
show.setText(Tips);
JOptionPane.showMessageDialog(null,warning,
"警告",JOptionPane.WARNING_MESSAGE);
}
}