package addresslistproject;
import java.awt.Toolkit;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import java.awt.Dimension;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class AddressList {
boolean packFrame = false;
/**
* Construct and show the application.
*/
public AddressList() {
MyFrame frame = new MyFrame();
// Validate frames that have preset sizes
// Pack frames that have useful preferred size info, e.g. from their layout
if (packFrame) {
frame.pack();
} else {
frame.validate();
}
// Center the window
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Dimension frameSize = frame.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
frame.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height) / 2);
frame.setVisible(true);
}
/**
* Application entry point.
*
* @param args String[]
*/
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
try {
UIManager.setLookAndFeel(UIManager.
getSystemLookAndFeelClassName());
} catch (Exception exception) {
exception.printStackTrace();
}
new AddressList();
}
});
}
}
package addresslistproject;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
class Information {
private String name;
private String code;
private String address;
private String Email;
private String telphone;
private String microphone;
public Information(String name,String code,String address,String Email,String telphone,String microphone){
this.name = name;
this.code =code;
this.address = address;
this.Email = Email;
this.telphone = telphone;
this.microphone = microphone;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setCode(String code){
this.code = code;
}
public String getCode(){
return code;
}
public void setAddress(String address){
this.address = address;
}
public String getAddress(){
return address;
}
public void setEmail(String Email){
this.Email = Email;
}
public String getEmail(){
return Email;
}
public void setTelphone(String telphone){
this.telphone = telphone;
}
public String getTelphone(){
return telphone;
}
public void setMicrophone(String microphone){
this.microphone = microphone;
}
public String getMicrophone(){
return microphone;
}
}
package addresslistproject;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JList;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.ListSelectionEvent;
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.List;
import java.util.Iterator;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import java.io.*;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2005</p>
*
* <p>Company: </p>
*
* @author not attributable
* @version 1.0
*/
public class MyFrame extends JFrame {
HashMap list = new HashMap();
JPanel contentPane;
DefaultListModel lItems=new DefaultListModel();
JList jList1 = new JList(lItems);
JPanel jPanelList = new JPanel(new BorderLayout());
JPanel jPanel1 = new JPanel();
GridLayout gridLayout1 = new GridLayout();
JPanel jPanel2 = new JPanel();
JPanel jPanel3 = new JPanel();
JPanel jPanel4 = new JPanel();
JPanel jPanel5 = new JPanel();
JPanel jPanel6 = new JPanel();
JPanel jPanel7 = new JPanel();
JLabel jLabel1 = new JLabel();
JTextField jTextName = new JTextField(30);
JLabel jLabel2 = new JLabel();
JTextField jTextCode = new JTextField();
JLabel jLabel3 = new JLabel();
JTextField jTextAddress = new JTextField();
JLabel jLabel4 = new JLabel();
JTextField jTextTel = new JTextField();
JLabel jLabel5 = new JLabel();
JTextField jTextMicro = new JTextField();
JLabel jLabel6 = new JLabel();
JTextField jTextEmail = new JTextField();
JPanel jPanel8 = new JPanel();
JButton btAdd = new JButton();
JButton btDel = new JButton();
JButton btConfirm = new JButton();
JButton btQuery = new JButton();
ListSelectionListener ll =
new ListSelectionListener() {
public void valueChanged(
ListSelectionEvent e) {
Object[] items=jList1.getSelectedValues();
if(items.length == 0)return;
String name = items[0].toString();
Information a = (Information)list.get(name);
jTextName.setText(a.getName());
jTextCode.setText(a.getCode());
jTextAddress.setText(a.getAddress());
jTextEmail.setText(a.getEmail());
jTextTel.setText(a.getTelphone());
jTextMicro.setText(a.getMicrophone());
}
};
JPanel jPanel9 = new JPanel();
JButton jButton1 = new JButton();
JButton jButton2 = new JButton();
public MyFrame() {
try {
setDefaultCloseOperation(EXIT_ON_CLOSE);
jbInit();
} catch (Exception exception) {
exception.printStackTrace();
}
}
/**
* Component initialization.
*
* @throws java.lang.Exception
*/
private void jbInit() throws Exception {
contentPane = (JPanel) getContentPane();
contentPane.setLayout(null);
setSize(new Dimension(500, 450));
setTitle("Frame Title");
jList1.setBounds(new Rectangle(10, 9, 142, 276));
jPanel1.setBounds(new Rectangle(165, 7, 328, 264));
jPanel1.setLayout(gridLayout1);
gridLayout1.setColumns(1);
gridLayout1.setRows(6);
jLabel1.setText("姓名:");
jTextName.setText("");
jLabel2.setText("邮编:");
jTextCode.setText("");
jTextCode.setColumns(30);
jLabel3.setText("住址:");
jTextAddress.setText("");
jTextAddress.setColumns(30);
jLabel4.setText("电话:");
jTextTel.setText("");
jTextTel.setColumns(30);
jLabel5.setText("手机:");
jTextMicro.setText("");
jTextMicro.setColumns(30);
jLabel6.setText("EMail:");
jTextEmail.setText("");
jTextEmail.setColumns(30);
jPanel8.setBounds(new Rectangle(155, 304, 325, 39));
btAdd.setText("添加");
btAdd.addActionListener(new MyFrame_btAdd_actionAdapter(this));
btDel.setText("删除");
btDel.addActionListener(new MyFrame_btDel_actionAdapter(this));
btConfirm.setText("查询");
btConfirm.addActionListener(new MyFrame_btConfirm_actionAdapter(this));
btQuery.setActionCommand("清空");
btQuery.setText("清空");
btQuery.addActionListener(new MyFrame_btQuery_actionAdapter(this));
jPanelList.setBounds(new Rectangle(14, 22, 147, 283));
jPanel9.setBounds(new Rectangle(203, 355, 218, 37));
jButton1.setText("保存");
jButton1.addActionListener(new MyFrame_jButton1_actionAdapter(this));
jButton2.setText("打开");
jButton2.addActionListener(new MyFrame_jButton2_actionAdapter(this));
contentPane.setPreferredSize(new Dimension(550, 430));
contentPane.add(jPanel1);
contentPane.add(jPanel8);
jPanel8.add(btAdd);
jPanel8.add(btDel);
jPanel8.add(btQuery);
jPanel8.add(btConfirm);
contentPane.add(jPanelList);
contentPane.add(jPanel9);
jPanel9.add(jButton1);
jPanel9.add(jButton2);
jPanelList.add(jList1);
jPanel1.add(jPanel2);
jPanel2.add(jLabel1);
jPanel2.add(jTextName);
jPanel1.add(jPanel3);
jPanel3.add(jLabel2);
jPanel3.add(jTextCode);
jPanel1.add(jPanel4);
jPanel4.add(jLabel3);
jPanel4.add(jTextAddress);
jPanel1.add(jPanel5);
jPanel5.add(jLabel4);
jPanel5.add(jTextTel);
jPanel1.add(jPanel6);
jPanel6.add(jLabel5);
jPanel6.add(jTextMicro);
jPanel1.add(jPanel7);
jPanel7.add(jLabel6);
jPanel7.add(jTextEmail);
jList1.addListSelectionListener(ll);
}
public void btAdd_actionPerformed(ActionEvent e) {
if(jTextName.getText().trim().equals("")){
JOptionPane.showMessageDialog(this, "请输入姓名。", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
if(list.containsKey(jTextName.getText().trim())){
JOptionPane.showMessageDialog(this, "此姓名已经存在。", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
Information a = new Information(jTextName.getText().trim(),jTextCode.getText(),
jTextAddress.getText(),
jTextEmail.getText(),
jTextTel.getText(),
jTextMicro.getText());
list.put(jTextName.getText().trim(),a);
lItems.add(0,jTextName.getText().trim());
}
public void btDel_actionPerformed(ActionEvent e) {
if(!list.containsKey(jTextName.getText().trim())){
JOptionPane.showMessageDialog(this, "此姓名不存在!", "提示",
JOptionPane.ERROR_MESSAGE);
return;
}
if(JOptionPane.showConfirmDialog(this,"确认删除?","infomation",
JOptionPane.OK_CANCEL_OPTION,
JOptionPane.INFORMATION_MESSAGE) == JOptionPane.OK_OPTION){
list.remove(jTextName.getText().trim());
int i=lItems.indexOf(jTextName.getText().trim());
System.out.println(""+i);
lItems.removeElement(jTextName.getText().trim());
jList1.clearSelection();
jTextName.setText("");
jTextCode.setText("");
jTextAddress.setText("");
jTextEmail.setText("");
jTextTel.setText("");
jTextMicro.setText("");
}
}
public void btConfirm_actionPerformed(ActionEvent e) {
if(!list.containsKey(jTextName.getText().trim())){
JOptionPane.showMessageDialog(this, "此姓名不存在。", "提示",
JOptionPane.INFORMATION_MESSAGE);
return;
}
String name = new String(jTextName.getText().trim());
Information a = (Information)list.get(name);
jTextName.setText(a.getName());
jTextCode.setText(a.getCode());
jTextAddress.setText(a.getAddress());
jTextEmail.setText(a.getEmail());
jTextTel.setText(a.getTelphone());
jTextMicro.setText(a.getMicrophone());
}
public void btQuery_actionPerformed(ActionEvent e) {
jTextName.setText("");
jTextCode.setText("");
jTextAddress.setText("");
jTextEmail.setText("");
jTextTel.setText("");
jTextMicro.setText("");
JOptionPane.showMessageDialog(this, "请输入要查询的人的姓名", "提示",
JOptionPane.INFORMATION_MESSAGE);
}
public void jButton1_actionPerformed(ActionEvent e) {
String filename;
JFileChooser fc = new JFileChooser();
int rVal = fc.showSaveDialog(MyFrame.this);
if(rVal == JFileChooser.CANCEL_OPTION)return;
filename = fc.getSelectedFile().getAbsolutePath();
Element root = new Element("AddressList");
Set entries = list.entrySet();
Iterator iter = entries.iterator();
while(iter.hasNext()){
Information a = (Information)((Map.Entry)iter.next()).getValue();
Element person = new Element("person");
person.setAttribute("name",a.getName());
root.addContent(person);
Element name = new Element("name");
name.setText(a.getName());
person.addContent(name);
Element code = new Element("code");
code.setText(a.getCode());
person.addContent(code);
Element email = new Element("E-mail");
email.setText(a.getEmail());
person.addContent(email);
Element address = new Element("address");
address.setText(a.getAddress());
person.addContent(address);
Element tel = new Element("telphone");
tel.setText(a.getTelphone());
person.addContent(tel);
Element micro = new Element("microphone");
micro.setText(a.getMicrophone());
person.addContent(micro);
}
Document doc = new Document(root);
Format f = Format.getPrettyFormat();
f.setEncoding("GBK");
XMLOutputter outputter=new XMLOutputter(f);
try{
outputter.output(doc, new FileOutputStream(filename));
}catch(Exception ex){
ex.printStackTrace();
}
}
public void jButton2_actionPerformed(ActionEvent e) {
JFileChooser fc = new JFileChooser();
/* fc.setFileFilter(new FileFilter(){
public boolean accept(File afile){
return true;
}
});*/
int rVal = fc.showOpenDialog(MyFrame.this);
if(rVal == JFileChooser.CANCEL_OPTION)return;
String filename = fc.getSelectedFile().getAbsolutePath();
list.clear();
lItems.clear();
SAXBuilder sb = new SAXBuilder();
Document doc = new Document();
try{
doc = sb.build(new FileInputStream(filename));
} catch(Exception ex){
ex.printStackTrace();
}
Element root = doc.getRootElement();
List people = root.getChildren();
Iterator iter = people.iterator();
while(iter.hasNext()){
Element person = (Element)iter.next();
Element name = person.getChild("name");
Element code = person.getChild("code");
Element email = person.getChild("E-mail");
Element address = person.getChild("address");
Element tel = person.getChild("telphone");
Element micro = person.getChild("microphone");
Information a = new Information(name.getText().trim(),code.getText(),
address.getText(),
email.getText(),
tel.getText(),
micro.getText());
list.put(person.getAttribute("name").getValue(),a);
lItems.add(0,person.getAttribute("name").getValue());
}
}
}
class MyFrame_jButton2_actionAdapter implements ActionListener {
private MyFrame adaptee;
MyFrame_jButton2_actionAdapter(MyFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton2_actionPerformed(e);
}
}
class MyFrame_jButton1_actionAdapter implements ActionListener {
private MyFrame adaptee;
MyFrame_jButton1_actionAdapter(MyFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.jButton1_actionPerformed(e);
}
}
class MyFrame_btQuery_actionAdapter implements ActionListener {
private MyFrame adaptee;
MyFrame_btQuery_actionAdapter(MyFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btQuery_actionPerformed(e);
}
}
class MyFrame_btConfirm_actionAdapter implements ActionListener {
private MyFrame adaptee;
MyFrame_btConfirm_actionAdapter(MyFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btConfirm_actionPerformed(e);
}
}
class MyFrame_btDel_actionAdapter implements ActionListener {
private MyFrame adaptee;
MyFrame_btDel_actionAdapter(MyFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btDel_actionPerformed(e);
}
}
class MyFrame_btAdd_actionAdapter implements ActionListener {
private MyFrame adaptee;
MyFrame_btAdd_actionAdapter(MyFrame adaptee) {
this.adaptee = adaptee;
}
public void actionPerformed(ActionEvent e) {
adaptee.btAdd_actionPerformed(e);
}
}