import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.*;
import java.io.Serializable;
import java.net.URISyntaxException;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.JScrollPane;
import javax.swing.JButton;
class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String birth;
private String tel;
private String email;
private String note;
// 无参构造方法
public Person() {
}
// 带全部属性的有参构造方法
public Person(String name, String birth, String tel, String email, String note) {
this.name = name;
this.birth = birth;
this.tel = tel;
this.email = email;
this.note = note;
}
// Getters和Setters方法
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
}
/**
* @author yq
* @since JDK1.8
* @version 1.0
*/
public class AddressBook extends JFrame implements ActionListener, ListSelectionListener, FocusListener {
private static final long serialVersionUID = 1L;
private JPanel contentPane, infoPanel;
private JLabel nameLabel;
private JTextField nameText;
private JLabel prompt_name;
private JLabel birthLabel;
private JTextField birthText;
private JLabel prompt_birth;
private JLabel telLabel;
private JTextField telText;
private JLabel emailLabel;
private JTextField emailText;
private JLabel prompt_email;
private JLabel noteLabel;
private JTextField noteText;
private JList<String> list;
private JScrollPane scrollPane;
private JButton newBtn, modifyBtn, saveBtn, delBtn, exitBtn;
private Vector<String> names = new Vector<String>();
private Hashtable<String, Person> ht = new Hashtable<String, Person>();
private int mode = 0;
/**
* 构造方法
*/
public AddressBook() {
setTitle("通讯录");
setSize(618, 439); // 设置窗口大小
setResizable(false); // 设置窗口不可改变大小
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
initUI();
add(contentPane, BorderLayout.CENTER);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
exit();
}
});
setVisible(true);
init();
}
/**
* 初始化界面
*/
public void initUI() {
contentPane = new JPanel();
contentPane.setLayout(null);
// 个人信息容器
infoPanel = new JPanel();
infoPanel.setBorder(new TitledBorder(null, "个人信息", TitledBorder.LEADING, TitledBorder.TOP, null, null));
infoPanel.setBounds(10, 21, 582, 190);
contentPane.add(infoPanel);
infoPanel.setLayout(null);
nameLabel = new JLabel("姓名");
nameLabel.setBounds(21, 27, 39, 15);
infoPanel.add(nameLabel);
nameText = new JTextField(10);
nameText.setBounds(82, 24, 222, 21);
infoPanel.add(nameText);
prompt_name = new JLabel("姓名不能为空,不能重复,必须唯一");
prompt_name.setBounds(327, 27, 222, 15);
infoPanel.add(prompt_name);
birthLabel = new JLabel("出生日期");
birthLabel.setBounds(21, 58, 54, 15);
infoPanel.add(birthLabel);
birthText = new JTextField(10);
birthText.setBounds(82, 55, 126, 21);
infoPanel.add(birthText);
prompt_birth = new JLabel("日期格式:xxxx-xx-xx");
prompt_birth.setBounds(229, 58, 131, 15);
infoPanel.add(prompt_birth);
telLabel = new JLabel("手机");
telLabel.setBounds(21, 89, 39, 15);
infoPanel.add(telLabel);
telText = new JTextField(10);
telText.setBounds(82, 86, 367, 21);
infoPanel.add(telText);
emailLabel = new JLabel("电子邮件");
emailLabel.setBounds(21, 120, 54, 15);
infoPanel.add(emailLabel);
emailText = new JTextField(10);
emailText.setBounds(82, 117, 278, 21);
infoPanel.add(emailText);
prompt_email = new JLabel("邮件格式:xxxx@xxxx.xxx");
prompt_email.setBounds(379, 120, 170, 15);
infoPanel.add(prompt_email);
noteLabel = new JLabel("备注");
noteLabel.setBounds(21, 151, 39, 15);
infoPanel.add(noteLabel);
noteText = new JTextField(10);
noteText.setBounds(82, 148, 490, 21);
infoPanel.add(noteText);
list = new JList<String>(names);
list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
list.addListSelectionListener(this);
scrollPane = new JScrollPane(list);
scrollPane.setBounds(10, 221, 365, 169);
contentPane.add(scrollPane);
newBtn = new JButton("新建");
newBtn.setBounds(385, 221, 207, 23);
contentPane.add(newBtn);
modifyBtn = new JButton("修改");
modifyBtn.setBounds(385, 254, 207, 23);
contentPane.add(modifyBtn);
saveBtn = new JButton("保存");
saveBtn.setBounds(385, 287, 207, 23);
contentPane.add(saveBtn);
delBtn = new JButton("删除");
delBtn.setBounds(385, 320, 207, 23);
contentPane.add(delBtn);
exitBtn = new JButton("退出");
exitBtn.setBounds(385, 353, 207, 23);
contentPane.add(exitBtn);
}
/**
* 除界面初始化之外的其他工作,给组件添加监听器,方便构造方法调用等
*/
public void init() {
newBtn.addActionListener(this);
modifyBtn.addActionListener(this);
saveBtn.addActionListener(this);
delBtn.addActionListener(this);
exitBtn.addActionListener(this);
nameText.addFocusListener(this);
birthText.addFocusListener(this);
telText.addFocusListener(this);
emailText.addFocusListener(this);
String path = "";
try {
path = AddressBook.class.getResource("/").toURI().getPath() + "address.ini";
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
File file = new File(path);
if(file.exists() && file.length() != 0) { // 文件存在并且长度不为0就进行反序列化
load();
if(names.isEmpty()) {
JOptionPane.showMessageDialog(null, "通讯录当前为空,请添加联系人!", "提示", JOptionPane.INFORMATION_MESSAGE);
mode = 1;
setMode(mode);
nameText.requestFocus();
}else {
setMode(mode);
if(names.size() > 0) {
list.setListData(names);
list.setSelectedIndex(0);
String name = list.getSelectedValue();
Person person = ht.get(name);
setInfo(person);
}
}
}else{
JOptionPane.showMessageDialog(null, "通讯录当前为空,请添加联系人!", "提示", JOptionPane.INFORMATION_MESSAGE);
mode = 1;
setMode(mode);
nameText.requestFocus();
}
}
/**
* 设置模式
* 浏览模式:mode=0 增加模式:mode=1 修改模式:mode=2
* @param mode 要设置的模式
*/
public void setMode(int mode) {
// 浏览模式
if