个人通讯录精编.docx
《个人通讯录精编.docx》由会员分享,可在线阅读,更多相关《个人通讯录精编.docx(22页珍藏版)》请在冰豆网上搜索。
个人通讯录精编
JAVA程序设计
开放性实验报告
专业:
计算机科学与技术
班级:
2010级1班
学号:
20100205001
姓名:
张军
实验1(4学时)
1.实验内容
(1)设计可视化界面,创建用户通讯录软件基本界面。
(2)设计软件能进行好友信息的录入和保存;并可依据个人爱好或其他特征对通讯录个人信息进行查询、排序和分组等操作。
(3)设计的软件完成基本的通讯录功能。
2.预习内容
GUI图形界面编程,事件处理;数据库编程,多媒体音频播放,图片加载显示等。
3.实验类型
综合性
4.实验目的
(1)了解通讯录系统基本功能,熟悉Java数据库编程,能进行数据库的设计与数据库表的操作,熟悉SQL查询语句;
(2)熟悉Java的GUI编程,能创建通讯录系统所需求的界面和相关组件,并能对窗体上的组件进行事件编程完成通讯录的数据录入,查询,修改等操作;
(3)熟悉Java中的多媒体技术如:
音频文件的播放控制。
熟悉Java中的绘图操作,可以使用相关函数实现图片的加载
5.实验要求
(1)创建Java项目,完成软件设计与实现;
(2)将自己设计和实现过程在实验步骤中总结归纳;
(3)完成实验报告相关内容。
6.实验过程(分析设计、源文件、执行结果)
源码:
packagecombook;
importjava.awt.*;
importjava.awt.event.*;
importjava.io.*;
importjava.util.*;
importjava.text.*;
importjavax.swing.*;
publicclassMyAddBook{
publicstaticJFrameframe;
publicMyAddBook(){
frame=newJFrame("个人通讯录");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
JMenuBarmenubar=newJMenuBar();
JMenuedit=newJMenu("编辑");
JMenuItemedit1=newJMenuItem("录入");
JMenuItemedit2=newJMenuItem("查询");
JMenuItemedit3=newJMenuItem("删除");
JMenuItemedit4=newJMenuItem("修改");
JMenuItemedit5=newJMenuItem("排序");
edit1.addActionListener(newTypein());
JMenushow=newJMenu("显示信息");
JMenuItemshow1=newJMenuItem("同学");
JMenuItemshow2=newJMenuItem("同事");
JMenuItemshow3=newJMenuItem("朋友");
JMenuItemshow4=newJMenuItem("亲戚");
JMenuItemshow5=newJMenuItem("全体人员");
Containerc=frame.getContentPane();
JPanelpane=newJPanel();
c.add(pane);
pane.add(menubar);
menubar.add(edit);
edit.add(edit1);edit.add(edit2);edit.add(edit3);
edit.add(edit4);edit.add(edit5);
menubar.add(show);
show.add(show1);show.add(show2);show.add(show3);
show.add(show4);show.add(show5);
frame.setSize(300,100);
//查询修改信息
classSearch{
JDialogdialog=newJDialog(frame,"查询对话框",true);
publicSearch(Stringstr,intn){
dialog.setSize(250,200);
Containerc=dialog.getContentPane();
dialog.setLayout(newGridLayout(2,1,5,5));
JLabelLsearch=newJLabel("请输入要"+str+"人员的名字");
finalJTextFieldTname=newJTextField(10);
JButtoncertain=newJButton("确定");
JButtoncancel=newJButton("取消");
//finalStringin=Tname.getText();
JPanelpane1=newJPanel();
JPanelpane2=newJPanel();
c.add(pane1);c.add(pane2);
pane1.add(Lsearch);pane1.add(Tname);
pane2.add(certain);pane2.add(cancel);
dialog.setDefaultCloseOperation(dialog.DISPOSE_ON_CLOSE);
if(n==2){
certain.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
try{
FileReaderfile=newFileReader("D:
\\AddreseBook.txt");
Scannerfind=newScanner(file);
while(find.hasNext()){
if(find.next().equals(Tname.getText())){
dialog.dispose();
Typeinfd=newTypein();
fd.frame=newJFrame("查询结果如下");
fd.Tname.setText(Tname.getText());
fd.Cgroup.select(find.next());
fd.Cbirthyear.select(find.next());
fd.Cbirthmonth.select(find.next());
fd.Cbirthday.select(find.next());
fd.Tphone.setText(find.next());
fd.Temail.setText(find.next());
fd.Tother.setText(find.next());
fd.typein();
}
}
file.close();find.close();
}
catch(FileNotFoundExceptione1){
System.out.println("未找到文件");
}
catch(IOExceptione2){
System.out.println("未找到该人员");
}
}
});
}
if(n==3){
certain.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
try{
Filefile=newFile("D:
\\AddressBook.txt");
Scannerfind=newScanner(file);
FileWriterfile1=newFileWriter("D:
\\AddressBook1.txt",true);
PrintWriterwrite=newPrintWriter(file1);
while(find.hasNext()){
Strings=find.next();
if(!
(s.equals(Tname.getText()))){
write.print(s+'\t');
write.print(find.next()+'\t');
write.print(find.next()+'');
write.print(find.next()+'');
write.print(find.next()+'\t');
write.print(find.next()+'\t');
write.print(find.next()+'\t');
write.println(find.next());
}
else{
find.next();find.next();find.next();
find.next();find.next();
}
}
find.close();file.delete();
write.close();file1.close();
Filefile2=newFile("D:
\\AddressBook1.txt");
file2.renameTo(newFile("D:
\\AddressBook.txt"));
JOptionPane.showMessageDialog(null,"删除成功","删除结果",
JOptionPane.INFORMATION_MESSAGE);
}
catch(FileNotFoundExceptione1){
JOptionPane.showMessageDialog(null,"未找到文件");
}
catch(IOExceptione2){
System.out.print("未找到该人员");
}
}
});
}
if(n==4){
certain.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
try{
Filefile=newFile("D:
\\AddressBook.txt");
Scannerfind=newScanner(file);
FileWriterfile1=newFileWriter("D:
\\AddressBook1.txt",true);
PrintWriterwrite=newPrintWriter(file1);
while(find.hasNext()){
Strings=find.next();
if(!
(s.equals(Tname.getText()))){
write.print(s+'\t');
write.print(find.next()+'\t');
write.print(find.next()+'');
write.print(find.next()+'');
write.print(find.next()+'\t');
write.print(find.next()+'\t');
write.print(find.next()+'\t');
write.println(find.next());
}
else{
dialog.dispose();
Typeinfd=newTypein();
Typein.y=1;
fd.frame=newJFrame("查询结果如下");
fd.Tname.setText(Tname.getText());
Strings1=find.next();
fd.Cgroup.select(s1);
Strings2=find.next();
fd.Cbirthyear.select(s2);
Strings3=find.next();
fd.Cbirthmonth.select(s3);
Strings4=find.next();
fd.Cbirthday.select(s4);
Strings5=find.next();
fd.Tphone.select(s5);
Strings6=find.next();
fd.Temail.select(s6);
Strings7=find.next();
fd.Tother.select(s7);
fd.typein();
if(Typein.z==1){
write.print(Tname.getText()+'\t');
write.print(s1+'\t');write.print(s2+'');
write.print(s3+'');write.print(s4+'\t');
write.print(s5+'\t');write.print(s6+'\t');
write.println(s7);
Typein.z=2;
}
}
}
find.close();file.delete();write.close();
file1.close();Filefile2=newFile("D:
\\AddressBook1.txt");
file2.renameTo(newFile("D:
\\AddressBook.txt"));
}
catch(FileNotFoundExceptione1){
System.out.print("未找到该文件");
}
catch(IOExceptione2){
System.out.print("未找到该人员");
}
}
});
}
cancel.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
System.out.println("fdas");
}
});
}
}
edit2.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
newSearch("查询",2).dialog.setVisible(true);
}//监听
});
edit3.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
newSearch("删除",3).dialog.setVisible(true);
}//监听
});
edit4.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente){
newSearch("修改",4).dialog.setVisible(true);
}//监听
});
//输出类
classPrint{
publicJTextAreaarea;
publicPrint(Stringst,intn){
JFrameframe=newJFrame(st+"信息如下");
frame.setSize(800,400);
area=newJTextArea();
frame.add(area);
frame.setVisible(true);
if(n==2)
try{
//排序
inti,j,k;
String[]all;
all=newString[1000];
BufferedReaderread=newBufferedReader(newFileReader("D:
\\AddressBook.txt"));
area.append("姓名"+'\t'+"组别"+'\t'+"生日"+'\t'+"电话"+
'\t'+"Email"+'\t'+"其他"+'\n');
intz=1,count=0;
while(z==1){
for(i=0;i<1000;i++){
Stringstr=read.readLine();
if(str!
=null){
all[i]=str;count++;
}
elsez=0;
}
}
String[]bll;
bll=newString[count];
for(i=0;ibll[i]=all[i];
getSortOfChinese(bll);
for(i=0;iarea.append(bll[i]+'\n');
read.close();
}
catch(FileNotFoundExceptione1){
e1.printStackTrace();
}
catch(IOExceptione2){
e2.printStackTrace();
}
if(n==1)
try{
FileReaderfile=newFileReader("D:
\\AddressBook.txt");
Scannerfind=newScanner(file);
area.append("姓名"+'\t'+"组别"+'\t'+"生日"+'\t'+"电话"+
'\t'+"Email"+'\t'+"其他"+'\n');
while(find.hasNext()){
Stringstr1=find.next();
Stringstr2=find.next();
if(str2.equals(st)){
area.append(str1+'\t');
area.append(str2+'\t');
area.append(find.next()+'');
area.append(find.next()+'');
area.append(find.next()+'\t');
area.append(find.next()+'\t');
area.append(find.next()+'\t');
area.append(find.next()+'\n');
}
else{
find.next();find.next();find.next();
find.next();find.next();
}
}
file.close();find.close();
}
catch(FileNotFoundExceptione1){
e1.printStackTrace();