1.首先建立一个LoginJFrame 类代码如下:
package java大作业终极版;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.GridBagLayout;
import java.awt.Window.Type;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class LoginJFrame extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
LoginJFrame frame = new LoginJFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public LoginJFrame() {
setTitle("登录界面");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 381, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("用户名:");
label.setFont(new Font("宋体", Font.PLAIN, 16));
label.setBounds(78, 71, 74, 29);
contentPane.add(label);
JLabel label_1 = new JLabel("密 码:");
label_1.setFont(new Font("宋体", Font.PLAIN, 16));
label_1.setBounds(78, 124, 74, 29);
contentPane.add(label_1);
textField = new JTextField();
textField.setBounds(150, 76, 136, 21);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(150, 129, 136, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
JButton button = new JButton("登录");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
new DisplayJFrame().setVisible(true);
}
});
button.setFont(new Font("宋体", Font.PLAIN, 16));
button.setBounds(219, 197, 67, 29);
contentPane.add(button);
JButton button_1 = new JButton("退出");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
button_1.setFont(new Font("宋体", Font.PLAIN, 16));
button_1.setBounds(78, 197, 67, 29);
contentPane.add(button_1);
}
}
2.建立一个类名为DisplayJFrame
package java大作业终极版;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import java.awt.Window.Type;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JTextArea;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.DropMode;
public class DisplayJFrame extends JFrame {
private JPanel contentPane;
private JTextField textField;
private JTextField textField_1;
private JTextField textField_2;
private JTextField textField_3;
private JTextField textField_4;
private JTextField textField_5;
private JTextField textField_6;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
DisplayJFrame frame = new DisplayJFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public DisplayJFrame() {
setType(Type.UTILITY);
setTitle("主界面");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
setBounds(100, 100, 450, 500);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel label = new JLabel("学生姓名:");
label.setFont(new Font("宋体", Font.PLAIN, 14));
label.setBounds(30, 20, 87, 26);
contentPane.add(label);
JLabel label_1 = new JLabel("学生性别:");
label_1.setFont(new Font("宋体", Font.PLAIN, 14));
label_1.setBounds(30, 60, 87, 26);
contentPane.add(label_1);
JLabel label_2 = new JLabel("学生年龄:");
label_2.setFont(new Font("宋体", Font.PLAIN, 14));
label_2.setBounds(30, 100, 87, 26);
contentPane.add(label_2);
JLabel label_3 = new JLabel("高考考号:");
label_3.setFont(new Font("宋体", Font.PLAIN, 14));
label_3.setBounds(30, 140, 87, 26);
contentPane.add(label_3);
JLabel label_4 = new JLabel("身份证号:");
label_4.setFont(new Font("宋体", Font.PLAIN, 14));
label_4.setBounds(30, 180, 87, 26);
contentPane.add(label_4);
JLabel label_5 = new JLabel("高考分数:");
label_5.setFont(new Font("宋体", Font.PLAIN, 14));
label_5.setBounds(30, 247, 87, 26);
contentPane.add(label_5);
textField = new JTextField();
textField.setBounds(127, 23, 250, 21);
contentPane.add(textField);
textField.setColumns(10);
textField_1 = new JTextField();
textField_1.setBounds(127, 63, 250, 21);
contentPane.add(textField_1);
textField_1.setColumns(10);
textField_2 = new JTextField();
textField_2.setBounds(127, 103, 250, 21);
contentPane.add(textField_2);
textField_2.setColumns(10);
textField_3 = new JTextField();
textField_3.setBounds(127, 143, 250, 21);
contentPane.add(textField_3);
textField_3.setColumns(10);
textField_4 = new JTextField();
textField_4.setBounds(127, 183, 250, 21);
contentPane.add(textField_4);
textField_4.setColumns(10);
textField_5 = new JTextField();
textField_5.setBounds(127, 250, 250, 21);
contentPane.add(textField_5);
textField_5.setColumns(10);
textField_6 = new JTextField();
textField_6.setBounds(127, 219, 250, 21);
contentPane.add(textField_6);
textField_6.setColumns(10);
JTextArea textArea = new JTextArea();
textArea.setLineWrap(true);
textArea.setBounds(30, 327, 377, 112);
contentPane.add(textArea);
JButton button = new JButton("添加");
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
//Scanner input=new Scanner(System.in);
//Stirng name =input.next();
String name=textField.getText();
String sex=textField_1.getText();
String age=textField_2.getText();
String examid=textField_3.getText();
String idcard=textField_4.getText();
String grade=textField_5.getText();
String location=textField_6.getText();
Student student = new Student();
student.setExamid(examid);
student.setGrade(Double.parseDouble(grade));
student.setIdcard(idcard);
student.setLocation(location);
student.setName(name);
student.setAge(Integer.parseInt(age));
student.setSex(sex);
StudentDao dao = new StudentDao();
dao.add(student);
//弹出一个添加成功的窗体
textArea.setText("添加成功!");
}catch(Exception ex){
textArea.setText("添加失败!\n"+ex.getMessage());
}
}
});
button.setFont(new Font("宋体", Font.PLAIN, 14));
button.setBounds(30, 283, 63, 34);
contentPane.add(button);
JButton button_1 = new JButton("查询");
button_1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String examid=textField_3.getText();
String idcard=textField_4.getText();
StudentDao dao = new StudentDao();
Student student = dao.find(examid,idcard);
if(student==null) {
textArea.setText("查询失败!");
}
else {
//弹出窗体,输出学生信息
textArea.setText("姓名:"+student.getName()+",性别:"+student.getSex()
+",年龄:"+student.getAge()
+",身份证号:" +student.getIdcard()
+",准考证号:"+student.getExamid()+"\n"+",籍贯:"+student.getLocation()
+",高考成绩:"+student.getGrade());
}
}
});
button_1.setFont(new Font("宋体", Font.PLAIN, 14));
button_1.setBounds(131, 283, 68, 34);
contentPane.add(button_1);
JButton button_2 = new JButton("删除");
button_2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try{
String name=textField.getText();
StudentDao dao = new StudentDao();
dao.delete(name);
textArea.setText("删除成功!");
}catch(Exception ex) {
textArea.setText("删除失败!\n"+ex.getMessage());
}
}
});
button_2.setFont(new Font("宋体", Font.PLAIN, 14));
button_2.setBounds(237, 283, 68, 34);
contentPane.add(button_2);
JButton btnNewButton = new JButton("修改");
btnNewButton.setFont(new Font("宋体", Font.PLAIN, 14));
btnNewButton.setBounds(338, 283, 69, 34);
contentPane.add(btnNewButton);
JLabel label_6 = new JLabel("地 址:");
label_6.setFont(new Font("宋体", Font.PLAIN, 14));
label_6.setBounds(30, 216, 87, 21);
contentPane.add(label_6);
}
}
3.建立student类
package java大作业终极版;
public class Student {
private String idcard;
private String examid;
private String name;
private String location;
private Double grade;
private String sex;
private int age;
public String getIdcard() {
return idcard;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public void setIdcard(String idcard) {
this.idcard = idcard;
}
public String getExamid() {
return examid;
}
public void setExamid(String examid) {
this.examid = examid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public double getGrade() {
return grade;
}
public void setGrade(double grade) {
this.grade = grade;
}
}
4.建立StudentDao类
package java大作业终极版;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class StudentDao {
//添加
public void add(Student student){
try{
Document document = XmlUtils.getDocument();
Element student_node = document.createElement("student");
student_node.setAttribute("examid", student.getExamid());
student_node.setAttribute("idcard", student.getIdcard());
Element name = document.createElement("name");
name.setTextContent(student.getName());
Element location = document.createElement("location");
location.setTextContent(student.getLocation());
Element grade = document.createElement("grade");
grade.setTextContent(student.getGrade()+"");
Element age=document.createElement("age");
age.setTextContent(student.getAge()+"");
Element sex=document.createElement("sex");
sex.setTextContent(student.getSex()+"");
student_node.appendChild(name);
student_node.appendChild(location);
student_node.appendChild(grade);
student_node.appendChild(age);
student_node.appendChild(sex);
//得到exam节点,并把student挂上去
document.getElementsByTagName("exam").item(0).appendChild(student_node);
XmlUtils.write2Xml(document);
}catch(Exception e){
throw new RuntimeException(e);
}
}
//删除
public void delete(String name){
try{
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("name");
for(int i = 0;i<list.getLength();i++){
if(list.item(i).getTextContent().equals(name)){
list.item(i).getParentNode().getParentNode().removeChild(list.item(i).getParentNode());
XmlUtils.write2Xml(document);
return;
}
}
throw new RuntimeException("对不起,您要删除的学生不存在!");//异常也是一种返回值
}catch(Exception e){
throw new RuntimeException(e);
}
}
//查找
public Student find(String examid,String idcard){
try{
Document document = XmlUtils.getDocument();
NodeList list = document.getElementsByTagName("student");
Student student = new Student();
for(int i = 0;i<list.getLength();i++){
Element student_node = (Element) list.item(i);
if(student_node.getAttribute("examid").equals(examid)&&
student_node.getAttribute("idcard").equals(idcard)){
Student s = new Student();
s.setExamid(examid);
s.setIdcard(idcard);
student.setExamid(student_node.getAttribute("examid"));
student.setIdcard(student_node.getAttribute("idcard"));
s.setName(student_node.getElementsByTagName("name").item(0).getTextContent());
s.setGrade(Double.parseDouble(student_node.getElementsByTagName("grade").item(0).getTextContent()));
s.setLocation(student_node.getElementsByTagName("location").item(0).getTextContent());
s.setAge(Integer.parseInt(student_node.getElementsByTagName("age").item(0).getTextContent()));
s.setSex(student_node.getElementsByTagName("sex").item(0).getTextContent());
return s;
}
}
return null;
}catch(Exception e){
throw new RuntimeException(e);
}
}
}
5.建立XmlUtils类,代码如下:
package java大作业终极版;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class XmlUtils {
public static Document getDocument()
throws ParserConfigurationException, SAXException, IOException{
//定义API的一家工厂,它使应用程序能够获得从xml文档生成DOM对象树的分析器基类。
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();//获得一个解析器实例
DocumentBuilder builder = factory.newDocumentBuilder();
//返回一个新的DOM对象
return builder.parse(new File("src/studentXML.xml"));
}
public static void write2Xml(Document document) throws TransformerException{
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(document), new StreamResult(new File("src/studentXML.xml")));
}
}
6.建立一个名为studentXML文件
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<exam>
</exam>