欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java实现XML文件学生通讯录

程序员文章站 2022-05-18 09:22:18
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);
  }
}

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