您好,登錄后才能下訂單哦!
java實現(xiàn)學生通訊錄,使用XML文件作為數(shù)據(jù)存儲,實現(xiàn)學生編號,姓名,年齡,住址,專業(yè),生日等信息的添加,刪除,修改,查找,備份,恢復,專業(yè)分類查找等功能。
項目需要導入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("專業(yè)名稱"); 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[]= {"計算機科學與技術","信息安全","電子信息科學與技術","網(wǎng)絡工程"}; 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"); //書寫格式(優(yōu)美的格式) 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("專業(yè)名稱"); 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[]= {"計算機科學與技術","信息安全","電子信息科學與技術","網(wǎng)絡工程"}; 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("網(wǎng)絡工程")) 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"); //書寫格式(優(yōu)美的格式) 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("網(wǎng)絡工程"); 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= {"編號","姓名","性別","年齡","生日","電話","郵箱","地址","專業(yè)"}; 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); } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持億速云。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。