Java实现XML文件学生通讯录

java实现学生通讯录,使用XML文件作为数据存储,实现学生编号,姓名,年龄,住址,专业,生日等信息的添加,删除,修改,查找,备份,恢复,专业分类查找等功能。

项目需要导入dom4j.jar包。

/*
添加学生记录
*/

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
public class AddPerson extends JFrame {
  private JPanel contentPane;
  private JTextField jt1;
  private JTextField jt2;
  private JTextField jt3;
  private JTextField jt4;
  private JTextField jt5;
  private JTextField jt6;
  private JTextField jt7;
  public AddPerson() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 489);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(null);
    setContentPane(contentPane);

    JLabel jl1 = new JLabel("学生通讯录");
    jl1.setFont(new Font("宋体", Font.PLAIN, 16));
    jl1.setBounds(10, 0, 95, 29);
    contentPane.add(jl1);

    JLabel jl2 = new JLabel("学生编号");
    jl2.setFont(new Font("宋体", Font.PLAIN, 16));
    jl2.setBounds(51, 39, 65, 29);
    contentPane.add(jl2);

    JLabel jl3 = new JLabel("学生姓名");
    jl3.setFont(new Font("宋体", Font.PLAIN, 16));
    jl3.setBounds(51, 78, 65, 29);
    contentPane.add(jl3);

    JLabel jl4 = new JLabel("学生性别");
    jl4.setFont(new Font("宋体", Font.PLAIN, 16));
    jl4.setBounds(51, 117, 65, 29);
    contentPane.add(jl4);

    JLabel jl5 = new JLabel("学生年龄");
    jl5.setFont(new Font("宋体", Font.PLAIN, 16));
    jl5.setBounds(51, 163, 65, 29);
    contentPane.add(jl5);

    JLabel jl6 = new JLabel("出生日期");
    jl6.setFont(new Font("宋体", Font.PLAIN, 16));
    jl6.setBounds(51, 202, 65, 29);
    contentPane.add(jl6);

    JLabel jl7 = new JLabel("手机号码");
    jl7.setFont(new Font("宋体", Font.PLAIN, 16));
    jl7.setBounds(51, 250, 65, 29);
    contentPane.add(jl7);

    JLabel jl8 = new JLabel("电子邮箱");
    jl8.setFont(new Font("宋体", Font.PLAIN, 16));
    jl8.setBounds(51, 296, 65, 29);
    contentPane.add(jl8);

    JLabel jl9 = new JLabel("家庭住址");
    jl9.setFont(new Font("宋体", Font.PLAIN, 16));
    jl9.setBounds(51, 345, 65, 29);
    contentPane.add(jl9);

    JLabel jl10 = new JLabel("专业名称");
    jl10.setFont(new Font("宋体", Font.PLAIN, 16));
    jl10.setBounds(51, 389, 65, 29);
    contentPane.add(jl10);

    jt1 = new JTextField();
    jt1.setBounds(129, 38, 150, 34);
    contentPane.add(jt1);
    jt1.setColumns(10);

    jt2 = new JTextField();
    jt2.setColumns(10);
    jt2.setBounds(129, 83, 150, 34);
    contentPane.add(jt2);

    jt3 = new JTextField();
    jt3.setColumns(10);
    jt3.setBounds(129, 163, 150, 34);
    contentPane.add(jt3);

    jt4 = new JTextField();
    jt4.setColumns(10);
    jt4.setBounds(129, 201, 150, 34);
    contentPane.add(jt4);

    jt5 = new JTextField();
    jt5.setColumns(10);
    jt5.setBounds(129, 250, 150, 34);
    contentPane.add(jt5);

    jt6 = new JTextField();
    jt6.setColumns(10);
    jt6.setBounds(129, 301, 150, 34);
    contentPane.add(jt6);

    jt7 = new JTextField();
    jt7.setColumns(10);
    jt7.setBounds(129, 350, 150, 34);
    contentPane.add(jt7);

    JRadioButton radioButton = new JRadioButton("男",true);
    radioButton.setBounds(139, 121, 65, 23);
    contentPane.add(radioButton);

    JRadioButton radioButton_1 = new JRadioButton("女");
    radioButton_1.setBounds(221, 121, 121, 23);
    contentPane.add(radioButton_1);
    ButtonGroup bb=new ButtonGroup();
    bb.add(radioButton);bb.add(radioButton_1);
JButton jb1 = new JButton("添加");
jb1.setFont(new Font("宋体", Font.PLAIN, 16));
jb1.setBounds(314, 78, 110, 61);
contentPane.add(jb1);
JButton jb2 = new JButton("关闭");
jb2.setFont(new Font("宋体", Font.PLAIN, 16));
jb2.setBounds(314, 167, 110, 61);
contentPane.add(jb2);
JButton jb3 = new JButton("清除");
jb3.setFont(new Font("宋体", Font.PLAIN, 16));
jb3.setBounds(314, 265, 110, 61);
contentPane.add(jb3);

String str1[]= {"计算机科学与技术","信息安全","电子信息科学与技术","网络工程"};
JComboBox comboBox=new JComboBox(str1); ;
comboBox.setBounds(126, 400, 155, 29);
contentPane.add(comboBox);

    jb1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
      PersonOperatorDome ppp=new PersonOperatorDome();
      if(ppp.SerachPersonbool(jt1.getText())) {
        JOptionPane.showMessageDialog(null, "此ID已存在,请勿重复添加");
      }
      else {
        Person pp=new Person();
        PersonOperatorDome person=new PersonOperatorDome();
        String sex;
        if(radioButton.isSelected()) {
           sex=radioButton.getText();
        }
        else {
           sex=radioButton_1.getText();
        }
        pp.setId(jt1.getText());
        pp.setName(jt2.getText());
        pp.setSex(sex);
        pp.setBirthday(jt4.getText());
        pp.setAge(jt3.getText());
        pp.setPhone(jt5.getText());
        pp.setEmail(jt6.getText());
        pp.setAddress(jt7.getText());
        pp.setMajor(String.valueOf(comboBox.getSelectedItem()));
        person.addPerson(pp);
        JOptionPane.showMessageDialog(null, "添加成功");
      }
    }});
    jb3.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent arg0) {
      // TODO Auto-generated method stub
      jt1.setText("");
      jt2.setText("");
      jt3.setText("");
      jt4.setText("");
      jt5.setText("");
      jt6.setText("");
      jt7.setText("");

    }

    });
    jb2.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
      // TODO Auto-generated method stub
      dispose();
    }
    });
  }
}
/*
人员信息类
*/
public class Person {
  private String id;
  private String name;
  private String sex;
  private String age;
  private String birthday;
  private String phone;
  private String email;
  private String address;
  private String major;
  public String getId() {
    return id;
  }
  public void setId(String id) {
    this.id = id;
  }
  public String getName() {
    return name;
  }
  public void setName(String name) {
    this.name = name;
  }
  public String getSex() {
    return sex;
  }
  public void setSex(String sex) {
    this.sex = sex;
  }
  public String getAge() {
    return age;
  }
  public void setAge(String age) {
    this.age = age;
  }
  public String getBirthday() {
    return birthday;
  }
  public void setBirthday(String birthday) {
    this.birthday = birthday;
  }
  public String getPhone() {
    return phone;
  }
  public void setPhone(String phone) {
    this.phone = phone;
  }
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public String getAddress() {
    return address;
  }
  public void setAddress(String address) {
    this.address = address;
  }
  public String getMajor() {
    return major;
  }
  public void setMajor(String major) {
    this.major = major;
  }

}
/*
人员操作接口
*/
import org.dom4j.Document;

public interface PersonOperator {
  public void addPerson(Person person);
  public void updatePerson(Person person);
  public void deletePerson(String id);
  public Person SerachPerson(String id);
  public boolean SerachPersonbool(String id);
  public void beifen();
  public void huifu();
}
import java.io.File;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class PersonOperatorDome {
      public void addPerson(Person person) {
        try {
          File file = new File("E:/person.xml");
          Document doc = null;
          Element rootElem = null;
          if(!file.exists()) {
            doc = DocumentHelper.createDocument();
            rootElem = doc.addElement("personlist");
          } else {
            doc = new SAXReader().read(file);
            rootElem = doc.getRootElement();
          }
          Element personElem = rootElem.addElement("person");
          personElem.addAttribute("id", person.getId());
          personElem.addElement("name").setText(person.getName());
          personElem.addElement("sex").setText(person.getSex());
          personElem.addElement("age").setText(person.getAge());
          personElem.addElement("birthday").setText(person.getBirthday());
          personElem.addElement("phone").setText(person.getPhone());
          personElem.addElement("email").setText(person.getEmail());
          personElem.addElement("address").setText(person.getAddress());
          personElem.addElement("major").setText(person.getMajor());
          XML.Writexml(doc);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    public void UpdataPerson(Person person) {
    Document doc = XML.getDocument();
    Element root=doc.getRootElement();
    List persons=root.elements();
    for(int i=0;i<persons.size();i++) {
      Element test=(Element)persons.get(i);
      if(person.getId().equals(test.attributeValue("id"))) {
        for(Iterator it =test.elementIterator();it.hasNext();) {
          Element node=(Element)it.next();
          String type=node.getName();
          if("name".equals(type)) {
            node.setText(person.getName());
          }
          if("sex".equals(type)) {
            node.setText(person.getSex());
          }
          if("age".equals(type)) {
            node.setText(person.getAge());
          }
          if("birthday".equals(type)) {
            node.setText(person.getBirthday());
          }
          if("phone".equals(type)) {
            node.setText(person.getPhone());
          }
          if("email".equals(type)) {
            node.setText(person.getEmail());
          }
          if("address".equals(type)) {
            node.setText(person.getAddress());
          }
          if("major".equals(type)) {
            node.setText(person.getMajor());
          }
        }
       }
      }
      XML.Writexml(doc);
    }

    public void beifen() {
      Document doc1 = XML.getDocument();
      try {
        File file = new File("E:/persondome.xml");
        Document doc = null;
        Element rootElem = null;
        if(!file.exists()) {
          doc = DocumentHelper.createDocument();
          rootElem = doc.addElement("personlist");
        } else {
          doc = new SAXReader().read(file);
          rootElem = doc.getRootElement();
        }
      }catch (Exception e) {
        e.printStackTrace();
       }
       try {
        //3.把Document写到XML文件
        FileOutputStream out = new FileOutputStream("e:/persondome.xml");
        //书写格式(优美的格式)
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter writer = new XMLWriter(out,format);
        writer.write(doc1);
        writer.close();
      } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException();
      }
    }
    public void huifu() {
      Document doc1 = XML.getDocument1();
      try {
        FileOutputStream out = new FileOutputStream("e:/person.xml");
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter writer = new XMLWriter(out,format);
        writer.write(doc1);
        writer.close();
      } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException();
      }
    }

    public void deletePerson(String id) {
    Document doc = XML.getDocument();
    Element root=doc.getRootElement();
    List persons=root.elements();
    for(int i=0;i<persons.size();i++) {
      Element test =(Element)persons.get(i);
      if(id.equals(test.attributeValue("id"))) {
        test.detach();
        break;
      }
    }
    XML.Writexml(doc);
    }

    public boolean SerachPersonbool(String id) {
      Document doc = XML.getDocument();
      Element root=doc.getRootElement();
      List persons=root.elements();
      Person person=new Person();
      for(int i=0;i<persons.size();i++) {
        Element test =(Element)persons.get(i);
        if(id.equals(test.attributeValue("id"))) {
          return true;
        }
      }
      return false;
    }

    public Person SerachPerson(String id) {
    Document doc = XML.getDocument();
    Element root=doc.getRootElement();
    List persons=root.elements();
    Person person=new Person();
    for(int i=0;i<persons.size();i++) {
      Element test =(Element)persons.get(i);
      if(id.equals(test.attributeValue("id"))) {
        for(Iterator it =test.elementIterator();it.hasNext();) {
          Element node=(Element)it.next();
          String type=node.getName();
          if("name".equals(type)) {
            person.setName(node.getText());
          }
          if("sex".equals(type)) {
            person.setSex(node.getText());
          }
          if("age".equals(type)) {
            person.setAge(node.getText());
          }
          if("birthday".equals(type)) {
            person.setBirthday(node.getText());
          }
          if("phone".equals(type)) {
            person.setPhone(node.getText());
          }
          if("email".equals(type)) {
            person.setEmail(node.getText());
          }
          if("address".equals(type)) {
            person.setAddress(node.getText());
          }
          if("major".equals(type)) {
            person.setMajor(node.getText());
          }
        }
      }
    }
    return person;
    }

}
/*
查找
*/

import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JTextField;
import javax.swing.JRadioButton;
import javax.swing.JButton;
import javax.swing.JComboBox;
public class SerachPerson extends JFrame {
  private JPanel contentPane;
  private JTextField jt1;
  private JTextField jt2;
  private JTextField jt3;
  private JTextField jt4;
  private JTextField jt5;
  private JTextField jt6;
  private JTextField jt7;
  public SerachPerson() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 489);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(null);
    setContentPane(contentPane);

    JLabel jl1 = new JLabel("学生通讯录");
    jl1.setFont(new Font("宋体", Font.PLAIN, 16));
    jl1.setBounds(10, 0, 95, 29);
    contentPane.add(jl1);

    JLabel jl2 = new JLabel("学生编号");
    jl2.setFont(new Font("宋体", Font.PLAIN, 16));
    jl2.setBounds(51, 39, 65, 29);
    contentPane.add(jl2);

    JLabel jl3 = new JLabel("学生姓名");
    jl3.setFont(new Font("宋体", Font.PLAIN, 16));
    jl3.setBounds(51, 78, 65, 29);
    contentPane.add(jl3);

    JLabel jl4 = new JLabel("学生性别");
    jl4.setFont(new Font("宋体", Font.PLAIN, 16));
    jl4.setBounds(51, 117, 65, 29);
    contentPane.add(jl4);

    JLabel jl5 = new JLabel("学生年龄");
    jl5.setFont(new Font("宋体", Font.PLAIN, 16));
    jl5.setBounds(51, 163, 65, 29);
    contentPane.add(jl5);

    JLabel jl6 = new JLabel("出生日期");
    jl6.setFont(new Font("宋体", Font.PLAIN, 16));
    jl6.setBounds(51, 202, 65, 29);
    contentPane.add(jl6);

    JLabel jl7 = new JLabel("手机号码");
    jl7.setFont(new Font("宋体", Font.PLAIN, 16));
    jl7.setBounds(51, 250, 65, 29);
    contentPane.add(jl7);

    JLabel jl8 = new JLabel("电子邮箱");
    jl8.setFont(new Font("宋体", Font.PLAIN, 16));
    jl8.setBounds(51, 296, 65, 29);
    contentPane.add(jl8);

    JLabel jl9 = new JLabel("家庭住址");
    jl9.setFont(new Font("宋体", Font.PLAIN, 16));
    jl9.setBounds(51, 345, 65, 29);
    contentPane.add(jl9);

    JLabel jl10 = new JLabel("专业名称");
    jl10.setFont(new Font("宋体", Font.PLAIN, 16));
    jl10.setBounds(51, 389, 65, 29);
    contentPane.add(jl10);

    jt1 = new JTextField();
    jt1.setBounds(129, 38, 150, 34);
    contentPane.add(jt1);
    jt1.setColumns(10);

    jt2 = new JTextField();
    jt2.setColumns(10);
    jt2.setBounds(129, 83, 150, 34);
    contentPane.add(jt2);

    jt3 = new JTextField();
    jt3.setColumns(10);
    jt3.setBounds(129, 163, 150, 34);
    contentPane.add(jt3);

    jt4 = new JTextField();
    jt4.setColumns(10);
    jt4.setBounds(129, 201, 150, 34);
    contentPane.add(jt4);

    jt5 = new JTextField();
    jt5.setColumns(10);
    jt5.setBounds(129, 250, 150, 34);
    contentPane.add(jt5);

    jt6 = new JTextField();
    jt6.setColumns(10);
    jt6.setBounds(129, 301, 150, 34);
    contentPane.add(jt6);

    jt7 = new JTextField();
    jt7.setColumns(10);
    jt7.setBounds(129, 350, 150, 34);
    contentPane.add(jt7);

    JRadioButton radioButton = new JRadioButton("男",true);
    radioButton.setBounds(139, 121, 65, 23);
    contentPane.add(radioButton);

    JRadioButton radioButton_1 = new JRadioButton("女");
    radioButton_1.setBounds(221, 121, 79, 23);
    contentPane.add(radioButton_1);
    ButtonGroup bb=new ButtonGroup();
    bb.add(radioButton);bb.add(radioButton_1);
JButton jb1 = new JButton("查找");
jb1.setFont(new Font("宋体", Font.PLAIN, 16));
jb1.setBounds(314, 78, 110, 61);
contentPane.add(jb1);
JButton jb2 = new JButton("关闭");
jb2.setFont(new Font("宋体", Font.PLAIN, 16));
jb2.setBounds(314, 280, 110, 61);
contentPane.add(jb2);
JButton jb3 = new JButton("清除");
jb3.setFont(new Font("宋体", Font.PLAIN, 16));
jb3.setBounds(314, 357, 110, 61);
contentPane.add(jb3);
JButton jb4 = new JButton("修改");
jb4.setFont(new Font("宋体", Font.PLAIN, 16));
jb4.setBounds(314, 174, 110, 61);
contentPane.add(jb4);
String str1[]= {"计算机科学与技术","信息安全","电子信息科学与技术","网络工程"};
JComboBox comboBox = new JComboBox(str1);
comboBox.setBounds(126, 394, 153, 29);
contentPane.add(comboBox);
    jb1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
      PersonOperatorDome person=new PersonOperatorDome();
      String id=jt1.getText();
      Person pp=person.SerachPerson(id);
      jt2.setText(pp.getName());
      if(radioButton.getText().equals(pp.getSex())) {
        radioButton.setSelected(true);
      }else {
        radioButton_1.setSelected(true);
      }
      jt3.setText(pp.getAge());
      jt4.setText(pp.getBirthday());
      jt5.setText(pp.getPhone());
      jt6.setText(pp.getEmail());
      jt7.setText(pp.getAddress());
      if(pp.getMajor().equals("计算机科学与技术")) comboBox.setSelectedIndex(0);
      if(pp.getMajor().equals("信息安全")) comboBox.setSelectedIndex(1);
      if(pp.getMajor().equals("电子信息科学与技术")) comboBox.setSelectedIndex(2);
      if(pp.getMajor().equals("网络工程")) comboBox.setSelectedIndex(3);
    }
    });
    jb3.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent e) {
      // TODO Auto-generated method stub
      jt1.setText("");
      jt2.setText("");
      jt3.setText("");
      jt4.setText("");
      jt5.setText("");
      jt6.setText("");
      jt7.setText("");
    }
    });
    jb2.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
      // TODO Auto-generated method stub
      dispose();
    }
    });
    jb4.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent arg0) {
      Person pp=new Person();
      PersonOperatorDome person=new PersonOperatorDome();
      String sex;
      if(radioButton.isSelected()) {
         sex=radioButton.getText();
      }
      else {
         sex=radioButton_1.getText();
      }
      pp.setId(jt1.getText());
      pp.setName(jt2.getText());
      pp.setSex(sex);
      pp.setBirthday(jt4.getText());
      pp.setAge(jt3.getText());
      pp.setPhone(jt5.getText());
      pp.setEmail(jt6.getText());
      pp.setAddress(jt7.getText());
      pp.setMajor(String.valueOf(comboBox.getSelectedItem()));
      person.UpdataPerson(pp);
      JOptionPane.showMessageDialog(null, "修改成功");
    }});
  }
}
import java.io.File;
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class XML {
   public static Document getDocument() {
      try {
        Document doc = new SAXReader().read(new File("e:/person.xml"));
        return doc;
      } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
      }
    }
   public static Document getDocument1() {
      try {
        Document doc = new SAXReader().read(new File("e:/persondome.xml"));
        return doc;
      } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException(e);
      }
    }
    /*
     * 写出XML文件
     */
    public static void Writexml(Document doc) {
      try {
        //3.把Document写到XML文件
        FileOutputStream out = new FileOutputStream("e:/person.xml");
        //书写格式(优美的格式)
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter writer = new XMLWriter(out,format);
        writer.write(doc);
        writer.close();
      } catch (Exception e) {
        e.printStackTrace();
        throw new RuntimeException();
      }
    }
}
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JTextArea;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.dom4j.Document;
import org.dom4j.Element;
public class MainContral extends JFrame {

  private JPanel contentPane;
  private JTable table;
  public MainContral() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 1200, 392);
    contentPane = new JPanel();
    contentPane.setLayout(null);
    setContentPane(contentPane);

    JButton jb1 = new JButton("添加");
    jb1.setFont(new Font("宋体", Font.PLAIN, 16));
    jb1.setBounds(23, 10, 101, 48);
    contentPane.add(jb1);

    JButton jb3 = new JButton("删除");
    jb3.setFont(new Font("宋体", Font.PLAIN, 16));
    jb3.setBounds(134, 10, 101, 48);
    contentPane.add(jb3);

    JButton jb4 = new JButton("\u67E5\u627E\u4E0E\u4FEE\u6539");
    jb4.setFont(new Font("宋体", Font.PLAIN, 16));
    jb4.setBounds(245, 10, 139, 48);
    contentPane.add(jb4);

    JButton jb5 = new JButton("显示全部");
    jb5.setFont(new Font("宋体", Font.PLAIN, 16));
    jb5.setBounds(394, 10, 126, 48);
    contentPane.add(jb5);

    JRadioButton radioButton = new JRadioButton("计算机科学与技术");
    radioButton.setFont(new Font("宋体", Font.PLAIN, 16));
    radioButton.setBounds(23, 89, 161, 23);
    contentPane.add(radioButton);

    JRadioButton radioButton_1 = new JRadioButton("信息安全");
    radioButton_1.setFont(new Font("宋体", Font.PLAIN, 16));
    radioButton_1.setBounds(186, 89, 101, 23);
    contentPane.add(radioButton_1);

    JRadioButton radioButton_2 = new JRadioButton("电子信息科学与技术");
    radioButton_2.setFont(new Font("宋体", Font.PLAIN, 16));
    radioButton_2.setBounds(289, 89, 174, 23);
    contentPane.add(radioButton_2);

    JRadioButton radioButton_3 = new JRadioButton("网络工程");
    radioButton_3.setFont(new Font("宋体", Font.PLAIN, 16));
    radioButton_3.setBounds(465, 89, 101, 23);
    contentPane.add(radioButton_3);

    JRadioButton radioButton_4 = new JRadioButton("全部");
    radioButton_4.setFont(new Font("宋体", Font.PLAIN, 16));
    radioButton_4.setBounds(568, 89, 121, 23);
    contentPane.add(radioButton_4);

    ButtonGroup bb=new ButtonGroup();
    bb.add(radioButton);
    bb.add(radioButton_3);
    bb.add(radioButton_2);
    bb.add(radioButton_1);
    bb.add(radioButton_4);

    table = new JTable();
    table.setFont(new Font("宋体", Font.PLAIN, 16));
    table.setModel(new DefaultTableModel(
      new Object[][] {
      },
      new String[] {
        "\u7F16\u53F7", "\u59D3\u540D", "\u6027\u522B", "\u5E74\u9F84", "\u751F\u65E5", "\u7535\u8BDD", "\u90AE\u7BB1", "\u5730\u5740", "\u4E13\u4E1A"
      }
    ));
    DefaultTableModel model = (DefaultTableModel) table.getModel();
    Object[] rowData= {"编号","姓名","性别","年龄","生日","电话","邮箱","地址","专业"};
     model.addRow(rowData);
     //JScrollPane scroll = new JScrollPane(table);

    table.setBounds(23, 131, 1100, 212);
    contentPane.add(table);

    JButton jb6 = new JButton("备份");
    jb6.setFont(new Font("宋体", Font.PLAIN, 16));
    jb6.setBounds(530, 10, 101, 48);
    contentPane.add(jb6);

    JButton jb7 = new JButton("恢复");
    jb7.setFont(new Font("宋体", Font.PLAIN, 16));
    jb7.setBounds(641, 10, 101, 48);
    contentPane.add(jb7);
    jb6.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        PersonOperatorDome p=new PersonOperatorDome();
        p.beifen();
        JOptionPane.showMessageDialog(null, "备份成功");
      }

    });
    jb7.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        PersonOperatorDome p=new PersonOperatorDome();
        p.huifu();
        JOptionPane.showMessageDialog(null, "恢复成功");
      }

    });
    jb1.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
         AddPerson test=new AddPerson();
         test.setVisible(true);
      }

    });
    jb3.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        int count1=table.getSelectedRow();
        String id= table.getValueAt(count1, 0).toString();
        PersonOperatorDome p=new PersonOperatorDome();
        p.deletePerson(id);
        JOptionPane.showMessageDialog(null, "删除成功");
      }
    });
    jb4.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        SerachPerson test=new SerachPerson();
        test.setVisible(true);
      }

    });
    jb5.addActionListener(new ActionListener() {

      @Override
      public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        PersonOperatorDome person=new PersonOperatorDome();
        Document doc = XML.getDocument();
        Element root=doc.getRootElement();
        List persons=root.elements();
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        while(model.getRowCount()>1){
           model.removeRow(model.getRowCount()-1);
        }
        for(int i=0;i<persons.size();i++) {
          Element test=(Element)persons.get(i);
          Person person1=new Person();
          person1.setId(test.attributeValue("id"));
          person1.setName(test.elementText("name"));
          person1.setSex(test.elementText("sex"));
          person1.setAge(test.elementText("age"));
          person1.setPhone(test.elementText("phone"));
          person1.setEmail(test.elementText("email"));
          person1.setBirthday(test.elementText("birthday"));
          person1.setAddress(test.elementText("address"));
          person1.setMajor(test.elementText("major"));
          Object[] rowData = {person1.getId(), person1.getName(),person1.getSex(),person1.getAge(), person1.getBirthday(), person1.getPhone(), person1.getEmail(),person1.getAddress(),person1.getMajor()};
          model.addRow(rowData);
        }
          radioButton_4.setSelected(true);
      }

    });
    radioButton.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        Document doc = XML.getDocument();
        Element root=doc.getRootElement();
        List persons=root.elements();
        String str=radioButton.getText();
        Person person1=new Person();
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        while(model.getRowCount()>1){
           model.removeRow(model.getRowCount()-1);
        }
        for(int i=0;i<persons.size();i++) {
          Element test =(Element)persons.get(i);
            if(str.equals(test.element("major").getText())) {
              person1.setId(test.attributeValue("id"));
              person1.setName(test.elementText("name"));
              person1.setSex(test.elementText("sex"));
              person1.setAge(test.elementText("age"));
              person1.setPhone(test.elementText("phone"));
              person1.setEmail(test.elementText("email"));
              person1.setBirthday(test.elementText("birthday"));
              person1.setAddress(test.elementText("address"));
              person1.setMajor(test.elementText("major"));
            Object[] rowData = {person1.getId(), person1.getName(),person1.getSex(),person1.getAge(), person1.getBirthday(), person1.getPhone(), person1.getEmail(),person1.getAddress(),person1.getMajor()};
            model.addRow(rowData);
            }
        }

      }
    });
    radioButton_1.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        Document doc = XML.getDocument();
        Element root=doc.getRootElement();
        List persons=root.elements();
        String str=radioButton_1.getText();
        Person person1=new Person();
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        while(model.getRowCount()>1){
           model.removeRow(model.getRowCount()-1);
        }
        for(int i=0;i<persons.size();i++) {
          Element test =(Element)persons.get(i);
            if(str.equals(test.element("major").getText())) {
              person1.setId(test.attributeValue("id"));
              person1.setName(test.elementText("name"));
              person1.setSex(test.elementText("sex"));
              person1.setAge(test.elementText("age"));
              person1.setPhone(test.elementText("phone"));
              person1.setEmail(test.elementText("email"));
              person1.setBirthday(test.elementText("birthday"));
              person1.setAddress(test.elementText("address"));
              person1.setMajor(test.elementText("major"));
            Object[] rowData = {person1.getId(), person1.getName(),person1.getSex(),person1.getAge(), person1.getBirthday(), person1.getPhone(), person1.getEmail(),person1.getAddress(),person1.getMajor()};
            model.addRow(rowData);
            }
        }

      }
    });
    radioButton_2.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        Document doc = XML.getDocument();
        Element root=doc.getRootElement();
        List persons=root.elements();
        String str=radioButton_2.getText();
        Person person1=new Person();
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        while(model.getRowCount()>1){
           model.removeRow(model.getRowCount()-1);
        }
        for(int i=0;i<persons.size();i++) {
          Element test =(Element)persons.get(i);
            if(str.equals(test.element("major").getText())) {
              person1.setId(test.attributeValue("id"));
              person1.setName(test.elementText("name"));
              person1.setSex(test.elementText("sex"));
              person1.setAge(test.elementText("age"));
              person1.setPhone(test.elementText("phone"));
              person1.setEmail(test.elementText("email"));
              person1.setBirthday(test.elementText("birthday"));
              person1.setAddress(test.elementText("address"));
              person1.setMajor(test.elementText("major"));

            Object[] rowData = {person1.getId(), person1.getName(),person1.getSex(),person1.getAge(), person1.getBirthday(), person1.getPhone(), person1.getEmail(),person1.getAddress(),person1.getMajor()};
            model.addRow(rowData);
            }
        }
      }
    });
    radioButton_3.addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub
        Document doc = XML.getDocument();
        Element root=doc.getRootElement();
        List persons=root.elements();
        String str=radioButton_3.getText();
        Person person1=new Person();
        DefaultTableModel model = (DefaultTableModel) table.getModel();
        while(model.getRowCount()>1){
           model.removeRow(model.getRowCount()-1);
        }
        for(int i=0;i<persons.size();i++) {
          Element test =(Element)persons.get(i);
            if(str.equals(test.element("major").getText())) {
              person1.setId(test.attributeValue("id"));
              person1.setName(test.elementText("name"));
              person1.setSex(test.elementText("sex"));
              person1.setAge(test.elementText("age"));
              person1.setPhone(test.elementText("phone"));
              person1.setEmail(test.elementText("email"));
              person1.setBirthday(test.elementText("birthday"));
              person1.setAddress(test.elementText("address"));
              person1.setMajor(test.elementText("major"));

            Object[] rowData = {person1.getId(), person1.getName(),person1.getSex(),person1.getAge(), person1.getBirthday(), person1.getPhone(), person1.getEmail(),person1.getAddress(),person1.getMajor()};
            model.addRow(rowData);
            }
        }
      }
    });
  }
  public static void main(String[] args) {
    MainContral test1=new MainContral();
      test1.setVisible(true);
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

您可能感兴趣的文章:

  • 图书管理系统java版
  • Java+Mysql学生管理系统源码
  • 一个简陋的java图书管理系统
  • 简单实现Java版学生管理系统
  • java学生信息管理系统设计
  • java联系人管理系统简单设计
  • 相册管理系统(Java表单+xml数据库存储)
  • java实现简单控制台通讯录
  • java使用集合实现通讯录功能
  • java实现电话本管理系统
(0)

相关推荐

  • java学生信息管理系统设计

    本例的学生信息添加进入数据库的事务(可以提交事务,事务回滚,用本地线程完善) 主页面index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <htm

  • java实现简单控制台通讯录

    通过主菜单对各级子菜单进行控制,并实现添加记录,查找记录,删除记录,修改记录,排序记录,以及退出系统功能的实现.一共六部分的功能模块. 上面的图就是每个模块具有的功能,而且还用到了正则表达式判断输入的是否符合规则,这个系统体现了MVC的设计模式,分工调用很清晰. 主要包含了下面几个类: APP类:程序的主函数类,程序开始的入口 Menu类:里面都是简单页面显示的效果,没有功能的实现. TelNoteRegex类:正则表达式,判断是否输入合格,都在这个方法中. Operate类:业务逻辑类,全部功

  • 简单实现Java版学生管理系统

    本文实例为大家分享了Java实现学生管理系统的具体代码,供大家参考,具体内容如下 package BookDemo_1; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Test { public static void main(String[] args) { StudentSys stuSys=new StudentSys("学生管理系统"); stuSys.initWi

  • Java+Mysql学生管理系统源码

    最近正在学java和数据库,想起以前写的学生管理系统,都是从网上下载,敷衍了事.闲来无事,也就自己写了一个,不过功能实现的不是很多. 开发语言:java: 开发环境:Mysql, java: 开发工具:eclipse 开发此案例,首先得在电脑上有java开发环境和Mysql, java开发环境与Mysql的搭建,就不再叙述了,如果需要,请联系我最下面的联系方式:dingyelf@aliyun.com 此次系统比较简易:数据库中只有一个表:stu;功能:能够对学生增加.删除.修改. 开发步骤:  

  • java联系人管理系统简单设计

    本文实例为大家分享了java联系人管理系统毕业设计,供大家参考,具体内容如下 要求:  请使用XML保存数据,完成一个联系人管理系统.       用户必须经过认证登录后方可以使用系统.       注册.增加.删除.查看联系人功能.        分模块进行设计. 两层框架-用户交互层,Dao层.             其他支持层-数据封装层.             工具类-加密,工厂Bean. 开发步骤: 第一步:设计数据结构-XML. 第一步:设计数据结构-XML. 第三步:准备资源并

  • 相册管理系统(Java表单+xml数据库存储)

    功能文件的上传,下载和管理 技术:1.用xml当做数据库存储信息(dom4j,xpath) 2.Java表单的文件上传和下载 3.文件目录的打散 ( Hash目录是一种优化文件存储性能的方法) 需要的jar包: commons-fileupload-1.2.2.jar.commons-io-2.1.jar.dom4j-1.6.1.jar和jaxen-1.1-beta-6.jar -----------------------------------------------------------

  • java使用集合实现通讯录功能

    本文实例为大家分享了java使用集合实现通讯录的具体代码,供大家参考,具体内容如下 代码有些繁琐,只适合初学者. 项目1 java通讯录 方法(声明方法的类) 代码块 代码块语法遵循标准markdown代码,例如: package com.lanou.homwork; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.Scanner; impor

  • 图书管理系统java版

    本文的目的就是通过图书管理系统掌握数据库编程技术,能正确连接数据库,能对数据库中信息进行查询.插入.删除.修改. 内容:在数据库中创建一张书目信息表,包括书名.作者.出版社.出版日期.书号.价格字段.设计一个GUI界面进行书目管理.在该界面上有四个选项卡,分别是查询.插入.删除.修改.点击查询选项卡,出现的界面上有书名.作者.出版社.书号四个文本框,一个按钮和一个只读文本区.文本框内容可以为空,输入相应的查询信息后(例如根据书名查询可以仅输入书名),点击界面上的"查询"按钮,可以在界面

  • java实现电话本管理系统

    用JAVA实现一个简单的电话本管理系统,能存储100个人的姓名.性别.年龄.电话等信息,并能对此系统进行增删改查的操作. 部分代码如下: public class Phbook { public static void main(String[] args) { Person personarray[] = new Person[100]; int index = 0; for (int i = 0; i < 100; i++) { System.out.println("电话本管理系统&

  • 一个简陋的java图书管理系统

    本文代码为原创一个简陋的管理系统,只做功能的测试.并没有去完善所有应有的功能,只做了输入输出查找,仅供参考! 菜单部分: import java.util.Scanner; public class Menu { int Min = 1; int Max = 3; public void getMenu(){ System.out.println("1.显示/2.输入/3.查找"); } public void getFindMenu(){ System.out.println(&qu

随机推荐