/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author HLC
*/
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.*;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.util.Collections;
class PersonInfoList extends JFrame implements ActionListener,
ListSelectionListener {
private static final long serialVersionUID = 1264438906669937392L;// 这些是电脑自己生成的我不懂
public String message[][] = new String[100][5];
//static String message1[][] = new String[100][5];
// public String stringarray[][];s
// stringarray=new String[100][5]
static int count = 0;
JButton addB;
JButton deleteB;
JButton searchB;
JButton bckB;
JButton exitB;
JButton editB;
JButton saveB;
JButton clearB;
DefaultListModel text;
JList list;
JTextField firstNameJtf;
JTextField lastNameJtf;
JTextField addressJtf;
JTextField zipJtf;
JTextField phoneNumberJtf;
JLabel firstNameLb;
JLabel lastNameLb;
JLabel addressLb;
JLabel zipLb;
JLabel phoneNoLb;
public PersonInfoList() {
super("AddressBook");
// message=new String[100][5];
// stringarray=new String[100][5];
text = new DefaultListModel();
list = new JList(text);
list.setVisibleRowCount(14);
list.addListSelectionListener(this);
addB = new JButton("Add");
addB.addActionListener(this);
deleteB = new JButton("Delete");
deleteB.addActionListener(this);
searchB = new JButton("Search");
searchB.addActionListener(this);
bckB = new JButton("Backup");
bckB.addActionListener(this);
editB=new JButton("Edit");
editB.addActionListener(this);
saveB=new JButton("Save");
saveB.addActionListener(this);
saveB.setEnabled(false);
clearB = new JButton("Clear");
clearB.addActionListener(this);
exitB = new JButton("Exit");
exitB.addActionListener(this);
firstNameJtf = new JTextField(10);
firstNameLb = new JLabel("First Name:");
lastNameJtf = new JTextField(10);
lastNameLb = new JLabel("Last Name:");
addressJtf = new JTextField(10);
addressLb = new JLabel("Address:");
zipJtf = new JTextField(10);
zipLb = new JLabel("Zip Code:");
phoneNumberJtf = new JTextField(20);
phoneNoLb = new JLabel("Phone No.:");
JPanel panela = new JPanel();
JPanel panelb = new JPanel();
panela.add(new JScrollPane(list));
panelb.add(firstNameLb);
panelb.add(firstNameJtf);
panelb.add(lastNameLb);
panelb.add(lastNameJtf);
panelb.add(addressLb);
panelb.add(addressJtf);
panelb.add(zipLb);
panelb.add(zipJtf);
panelb.add(phoneNoLb);
panelb.add(phoneNumberJtf);
panelb.add(addB);
panelb.add(deleteB);
panelb.add(searchB);
panelb.add(bckB);
panelb.add(editB);
panelb.add(saveB);
panelb.add(clearB);
panelb.add(exitB);
panelb.setLayout(new GridLayout(9, 1));
add(panela, BorderLayout.WEST);
add(panelb, BorderLayout.EAST);
setVisible(true);
setResizable(false);
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String cmd = e.getActionCommand();
if (cmd.equals("Add")) {
message[count][0] = firstNameJtf.getText();
message[count][1] = lastNameJtf.getText();
message[count][2] = addressJtf.getText();
message[count][3] = zipJtf.getText();
message[count][4] = phoneNumberJtf.getText();
try {
String filename1 = "AddressBook.txt";
FileWriter writer = new FileWriter(filename1, true);
writer.write("Last Name:" + message[count][1] + " First Name:"
+ message[count][0] + " \t Address:" + message[count][2]
+ " \tZipcode:" + message[count][3] + " \tPhone Number:"
+ message[count][4]);
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
String info = "Last Name:" + message[count][1]
+ " \tFirst Name:" + message[count][0] + " \t Address:"
+ message[count][2] + " \tZipcode:" + message[count][3]
+ " \tPhone Number:" + message[count][4];
text.addElement(info);
//Collections.s.sort(text);
//list.updateUI();
count++;
firstNameJtf.setText("");
lastNameJtf.setText("");
addressJtf.setText("");
zipJtf.setText("");
phoneNumberJtf.setText("");
}
if (cmd.equals("Delete")) {
if (!list.isSelectionEmpty())
text.removeElementAt(list.getSelectedIndex());
}
if (cmd.equals("Search")) {
boolean exist=false;
for (int i = 0; i <= count; i++) {
if (firstNameJtf.getText().equals(message[i][0]) && lastNameJtf.getText().equals(message[i][1]))
{
firstNameJtf.setText(message[i][0]);
lastNameJtf.setText(message[i][1]);
addressJtf.setText(message[i][2]);
zipJtf.setText(message[i][3]);
phoneNumberJtf.setText(message[i][4]);
exist = true;
}
}
if (exist == false){
JOptionPane.showMessageDialog(this, "Did not exist, please input again!", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
if (cmd.equals("Backup")){
File fileOld = new File("AddressBook.txt");
File fileNew = new File("AddressBookbak.txt");
if(fileOld.exists()){
try {
FileInputStream fis = new FileInputStream(fileOld);
FileOutputStream fos = new FileOutputStream(fileNew);
int read = 0;
while ((read = fis.read()) != -1) {
fos.write(read);
fos.flush();
}
fos.close();
fis.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
if (cmd.equals("Edit")){
int j;
for(j=0;j<count;j++){
if( list.isSelectedIndex(j)){
firstNameJtf.setText(message[j][0]);
lastNameJtf.setText(message[j][1]);
addressJtf.setText(message[j][2]);
zipJtf.setText(message[j][3]);
phoneNumberJtf.setText(message[j][4]);
firstNameJtf.setEditable(false);
lastNameJtf.setEditable(false);
saveB.setEnabled(true);
}
}
}
if (cmd.equals("Save")) {
int n=JOptionPane.showConfirmDialog(null, "Are you sure to edit?", "Attention:", JOptionPane.OK_CANCEL_OPTION);
if(n==JOptionPane.YES_OPTION){
int k;
for(k=0;k<count;k++){