java和jdom实现的一个地址簿源码

原创 2005年05月25日 02:29:00

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);
    }
}

地址簿信息管理系统-VB源码

  • 2012年06月13日 13:28
  • 949KB
  • 下载

一个简单的python 地址簿程序

用python实现 #!/usr/bin/python menu = ''' Address Book menu q - Quit a - Add a new...

QT 地址簿 源码

  • 2011年12月09日 15:51
  • 940KB
  • 下载

地址簿源码(struts编写)

  • 2008年09月06日 10:22
  • 1.84MB
  • 下载

python编程:电话簿TeleAddressBook和邮箱地址簿EmailAddressBook两个文件,合并为一个完整的AddressBook文件

有电话簿TeleAddressBook和邮箱地址簿EmailAddressBook两个文件,使用字典结构将其合并为一个完整的AddressBook文件 。 变为: 代码如下: ...
  • Homewm
  • Homewm
  • 2017年10月26日 08:19
  • 131

Qt入门编写一个地址簿

  • 2012年06月23日 15:29
  • 1.99MB
  • 下载

java地址簿管理程序

  • 2017年10月28日 22:57
  • 4KB
  • 下载

IceWarp 集成 LDAP 服务 同步管理用户,实现客户端地址簿查找

LDAP是轻量目录访问协议,英文全称是Lightweight Directory Access Protocol,一般都简称为LDAP。它是基于X.500标准的,但是更简单并且可以根据需要定制。与X....

天启地址簿 2.0.0001

  • 2007年10月31日 15:59
  • 891KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:java和jdom实现的一个地址簿源码
举报原因:
原因补充:

(最多只允许输入30个字)