import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class Note implements Serializable{
public String name;
public String address;
public String telephone;
public Note(){}
}
public class AddressListSystem extends JFrame{
JLabel title=new JLabel("我的通讯录管理");
JLabel name=new JLabel("姓名");
JLabel address=new JLabel("地址");
JLabel telephone=new JLabel("电话");
JTextField jtxtname=new JTextField();
JTextField jtxtaddr=new JTextField();
JTextField jtxttel=new JTextField();
Font font=new Font("TimersRoman",Font.BOLD,30);
JButton jbtadd=new JButton("添加");
JButton jbtfind=new JButton("查找");
JButton jbtclear=new JButton("清空");
JButton jbtexit=new JButton("退出");
ArrayList al=new ArrayList();
AddressListSystem(String s){
super(s);
Container cp=getContentPane();
cp.setBackground(Color.PINK);
cp.setLayout(null);
title.setFont(font);
title.setForeground(Color.BLUE);
title.setBounds(130,20,300,60);
name.setBounds(50,150,75,25);
address.setBounds(50,220,75,25);
telephone.setBounds(50,310,75,25);
jtxtname.setBounds(150,150,100,25);
jtxtaddr.setBounds(150,220,100,25);
jtxttel.setBounds(150,310,200,25);
jbtadd.setBounds(50,400,75,25);
jbtfind.setBounds(150,400,75,25);
jbtclear.setBounds(250,400,75,25);
jbtexit.setBounds(350,400,75,25);
jbtadd.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(jtxtname.getText().equalsIgnoreCase("")){
JOptionPane.showMessageDialog(null,"无法添加名字为空的记录","错误提示",JOptionPane.INFORMATION_MESSAGE);
jtxtname.setText("");
jtxtaddr.setText("");
jtxttel.setText("");
return;
}
Note note=new Note();
note.name=jtxtname.getText();
note.address=jtxtaddr.getText();
note.telephone=jtxttel.getText();
try{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("note.dat"));
al=(ArrayList)in.readObject();
in.close();
}catch(Exception ex){
System.out.println( "添加信息时文件输入有误哦");
}
try{
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("note.dat",true));
Note temp=new Note();
int i;
for(i=0;i<al.size();i++){
temp=(Note)al.get(i);
if(temp.name.equalsIgnoreCase(jtxtname.getText()))
break;
}
if(!al.isEmpty()&&i!=al.size()){
JOptionPane.showMessageDialog(null,"已经存在此记录","错误提示",JOptionPane.INFORMATION_MESSAGE);
}
else{
al.add(note);
out.writeObject(al);
}
out.close();
}catch(Exception ex){
System.out.println("添加信息时文件输出有误哦");
}
jtxtname.setText("");
jtxtaddr.setText("");
jtxttel.setText("");
}
});
jbtfind.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ObjectInputStream in=new ObjectInputStream(new FileInputStream("note.dat"));
al=(ArrayList)in.readObject();
in.close();
}catch(Exception ex){
System.out.println("查询时文件打开有误哦");
}
Note temp=new Note();
int i;
for(i=0;i<al.size();i++){
temp=(Note)al.get(i);
if(temp.name.equalsIgnoreCase(jtxtname.getText()))
break;
}
if(!al.isEmpty()&&i!=al.size()){
jtxtaddr.setText(temp.address);
jtxttel.setText(temp.telephone);
}
else{
jtxtname.setText("");
jtxtaddr.setText("");
jtxttel.setText("");
JOptionPane.showMessageDialog(null,"无此记录哦","温馨提示",JOptionPane.INFORMATION_MESSAGE);
}
}
});
jbtclear.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("note.dat"));
al.clear();
out.close();
}catch(Exception ex){
System.out.println("清除文件时有误哦");
}
jtxtname.setText("");
jtxtaddr.setText("");
jtxttel.setText("");
}
});
jbtexit.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
try{
System.exit(1);
}catch(Exception ex){
System.out.println("退出系统时出错啦");
}
}
});
cp.add(title);
cp.add(name);
cp.add(address);
cp.add(telephone);
cp.add(jtxtname);
cp.add(jtxtaddr);
cp.add(jtxttel);
cp.add(jbtadd);
cp.add(jbtfind);
cp.add(jbtclear);
cp.add(jbtexit);
}
public static void main(String[] args) {
AddressListSystem als=new AddressListSystem("我的通讯录管理系统");
als.addWindowListener(new WindowAdapter(){
@Override
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
als.setSize(500,550);
als.setVisible(true);
}
}