文档库

最新最全的文档下载
当前位置:文档库 > 通讯录管理系统源代码

通讯录管理系统源代码

//package TongXunLu;//package tongxunlu;

import java.awt.*;

import java.awt.event.*;

import java.io.*;

import javax.swing.JApplet;

import javax.swing.JOptionPane;

public class TongXunLu extends Frame implements ActionListener {

private MenuItem noteAdd = new MenuItem("添加");

private MenuItem noteDel = new MenuItem("删除");

private MenuItem noteEdit = new MenuItem("修改");

private MenuItem noteSearch = new MenuItem("查找");

private MenuItem noteout = new MenuItem("导入/统计");

private MenuItem noteExit = new MenuItem("退出");

private List noteList = new List();

private String[][] message = new String[1000][4];

public Info notes = new Info();

public TongXunLu()

{

super("通讯录");

Menu note = new Menu("编辑");

note.add(noteAdd);

note.add(noteDel);

note.add(noteEdit);

note.add(noteSearch);

note.add(noteout);

note.addSeparator();

note.add(noteExit);

MenuBar bar = new MenuBar();

bar.add(note);

setMenuBar(bar);

add(noteList);

noteAdd.addActionListener(this);

noteDel.addActionListener(this);

noteEdit.addActionListener(this);

noteExit.addActionListener(this);

noteSearch.addActionListener(this);

noteout.addActionListener(this);

noteList.addActionListener(this);

addWindowListener(new WindowCloser());

setSize(400, 600);

setVisible(true);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==noteout)

{

String [] str=new String[500];

try

{

FileInputStream fr =new FileInputStream("tongxunlu.txt");

BufferedReader bw = new BufferedReader(new InputStreamReader(fr));

int i=0;

while((str[i]=bw.readLine())!=null)

{

i++;

noteList.add(str[i-1]);

}

noteList.add("人数总个数:"+i);

fr.close();

}catch (IOException e1)

{

e1.printStackTrace();

}

}

if (e.getSource() == noteAdd)

{

notes.mail = null;

http://www.wendangku.net/doc/11a75be387c24028915fc3f9.html = null;

notes.address = null;

notes.telephone = null;

int index = noteList.getItemCount();

EditDialog dlg = new EditDialog(this, "通讯录", notes, false);

if (dlg.isOk)

{

message[index][0] = dlg.textName.getText();

message[index][1] = dlg.textAddress.getText();

message[index][2] = dlg.textEmail.getText();

message[index][3] = dlg.textTelephone.getText();

if (message[index][0] != null)

{

String s = "姓名:" + message[index][0] + " 地址:"

+ message[index][1] + " E-mail:"

+ message[index][2] + " 电话:"

+ message[index][3];

noteList.add(s);

}

}

File file = new File("tongxunlu.txt");

FileWriter fw;

try

{

fw = new FileWriter(file,true);

BufferedWriter bw = new BufferedWriter(fw);

bw.write("姓名:"+message[index][0]+" ");

bw.write("地址:"+message[index][1]+" ");

bw.write("email:"+message[index][2]+" ");

bw.write("电话:"+message[index][3]);

bw.newLine();

bw.close();

} catch (IOException e1)

{

// TODO Auto-generated catch block

e1.printStackTrace();

}

dlg.dispose();

} else if (e.getSource() == noteEdit || e.getSource() == noteList)

{

int i = noteList.getSelectedIndex();

if (i != -1)

{

http://www.wendangku.net/doc/11a75be387c24028915fc3f9.html = message[i][0];

notes.address = message[i][1];

notes.mail = message[i][2];

notes.telephone = message[i][3];

EditDialog dlg = new EditDialog(this, "修改通讯录", notes, false);

if (dlg.isOk)

{

message[i][0] = dlg.textName.getText();

message[i][1] = dlg.textAddress.getText();

message[i][2] = dlg.textEmail.getText();

message[i][3] = dlg.textTelephone.getText();

String s = "姓名:" + message[i][0] + " 地址:"

+ message[i][1] + " E-mail:"

+ message[i][2] + " 电话:" + message[i][3];

noteList.replaceItem(s, i);

}

dlg.dispose();

}

} else if (e.getSource() == noteDel)

{

ConfirmDialog cd = new ConfirmDialog(this, "确认删除?","确认要删除这条通讯录信息吗?");

if (cd.close)

{

int index = noteList.getSelectedIndex();

for (int i = index; i < noteList.getItemCount() - 1; i++)

{

for (int j = 0; j < 4; j++)

message[i][j] = message[i + 1][j];

}

noteList.remove(index);

}

} else if (e.getSource() == noteSearch)

{

EditDialog dlg = new EditDialog(this, "查找通讯录", notes, true);

if (dlg.isOk)

{

for (int i = 0; i < noteList.getItemCount(); i++)

{

String thisname = noteList.getItem(i).toString().split(" ")[0]

.split(":")[1];

String thistelephone = noteList.getItem(i).toString()

.split(" ")[30].split(":")[1];

if ((http://www.wendangku.net/doc/11a75be387c24028915fc3f9.html.equals(thisname) && dlg.telephone

.equals(thistelephone))

|| (http://www.wendangku.net/doc/11a75be387c24028915fc3f9.html.equals(thisname) && (dlg.telephone

.equals("")))

|| ((http://www.wendangku.net/doc/11a75be387c24028915fc3f9.html.equals("")) && dlg.telephone

.equals(thistelephone)))

{

String show = "查找成功!信息为:"

+ noteList.getItem(i).toString();

JOptionPane.showMessageDialog(null, show);

return;

}

}

JOptionPane.showMessageDialog(null, "无此人信息!");

}

} else if (e.getSource() == noteExit)

{

System.exit(0);

}

}

private class WindowCloser extends WindowAdapter

{

public void windowClosing(WindowEvent we)

{

System.exit(0);

}

}

public static void main(String[] args)

{

new TongXunLu();

}

}

class EditDialog extends Dialog implements ActionListener

{

private Button ok = new Button("确定");

private Button cancel = new Button("取消");

private Label labelName = new Label(" 姓名", Label.LEFT);

private Label labelAddress = new Label(" 地址", Label.LEFT);

private Label labelEmail = new Label("E-mail", Label.LEFT);

private Label labelTelephone = new Label(" 电话", Label.LEFT);

public String name = null;

public String telephone = null;

TextField textName = new TextField(20);

TextField textAddress = new TextField(20);

TextField textEmail = new TextField(20);

TextField textTelephone = new TextField(20);

public boolean isOk = false;

private Info notes = new Info();

public EditDialog(Frame parent, String title, Info nt, boolean search) {

super(parent, title, true);

notes = nt;

Panel pName = new Panel();

pName.setLayout(new FlowLayout());

pName.add(labelName);

pName.add(textName);

Panel pTelephone = new Panel();

pTelephone.setLayout(new FlowLayout());

pTelephone.add(labelTelephone);

pTelephone.add(textTelephone);

Panel pInfo = new Panel();

if (!search)

{

Panel pAddress = new Panel();

pAddress.setLayout(new FlowLayout());

pAddress.add(labelAddress);

pAddress.add(textAddress);

Panel pEmail = new Panel();

pEmail.setLayout(new FlowLayout());

pEmail.add(labelEmail);

pEmail.add(textEmail);

pInfo.setLayout(new GridLayout(4, 1));

pInfo.add(pName);

pInfo.add(pAddress);

pInfo.add(pEmail);

pInfo.add(pTelephone);

} else

{

pInfo.setLayout(new GridLayout(2, 1));

pInfo.add(pName);

pInfo.add(pTelephone);

}

Panel pButton = new Panel();

pButton.setLayout(new FlowLayout());

pButton.add(ok);

pButton.add(cancel);

add("Center", pInfo);

add("South", pButton);

textName.setText(http://www.wendangku.net/doc/11a75be387c24028915fc3f9.html);

textAddress.setText(notes.address);

textEmail.setText(notes.mail);

textTelephone.setText(notes.telephone);

ok.addActionListener(this);

cancel.addActionListener(this);

addWindowListener(new WindowCloser());

pack();

setVisible(true);

}

public void actionPerformed(ActionEvent ae)

{

if (ae.getSource() == ok)

{

isOk = true;

name = textName.getText();

telephone = textTelephone.getText();

setVisible(false);

}

if (ae.getSource() == cancel)

{

isOk = false;

setVisible(false);

}

}

private class WindowCloser extends WindowAdapter

{

public void windowClosing(WindowEvent ae)

{

isOk = false;

EditDialog.this.setVisible(false);

}

}

}

class ConfirmDialog extends Dialog implements ActionListener

{

private Button okay = new Button("确定");

private Button cancel = new Button("取消");

private Label label = new Label();

public boolean close = false;

public ConfirmDialog(Frame parent, String title, String question)

{

super(parent, title, true);

label.setText(question);

Panel buttons = new Panel();

buttons.setLayout(new FlowLayout());

buttons.add(okay);

buttons.add(cancel);

setLayout(new BorderLayout());

add("Center", label);

add("South", buttons);

okay.addActionListener(this);

cancel.addActionListener(this);

addWindowListener(new WindowCloser());

pack();

setVisible(true);

}

private class WindowCloser extends WindowAdapter {

public void wndowClosing(WindowEvent we)

{

ConfirmDialog.this.close = false;

ConfirmDialog.this.setVisible(false);

}

}

public void actionPerformed(ActionEvent ae)

{

close = (ae.getSource() == okay);

setVisible(false);

}

}

class Info

{

public String name;

public String address;

public String mail;

public String telephone;

}