package com.frame.project;//主窗口 import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.Reader; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JOptionPane; import com.lovo.netCRM.component.LovoButton; import com.lovo.netCRM.component.LovoTable; public class MainFrame extends JFrame { private LovoTable table = new LovoTable(this, new String[] { "学号", "姓名", "性别", "学历" }, new String[] { "code", "name", "sex", "edu" }, null); public static List<StudentBean> list=FilePress.read(); public MainFrame() { this.setLayout(null); table.setSizeAndLocation(50, 50, 600, 300); table.updateLovoTable(list); LovoButton addButton=new LovoButton("添加", 50, 400, this); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { MainFrame.this.dispose(); new addFrame(); } }); LovoButton delButton=new LovoButton("删除", 150, 400, this); delButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { int row=table.getSelectedRow(); if(row==-1) { JOptionPane.showMessageDialog(null, "请选择行"); return ; } list.remove(row); FilePress.write(list); table.updateLovoTable(list); } }); LovoButton updateButton=new LovoButton("修改", 250, 400, this); updateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { int row=table.getSelectedRow(); if(row==-1) { JOptionPane.showMessageDialog(null, "请选择行"); return ; } //根据下标找到对象 StudentBean student=list.get(row); MainFrame.this.dispose(); UpdateFrema x=new UpdateFrema(student); } }); this.setSize(800, 600); this.setVisible(true); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(3); } public static void main(String[] args) { MainFrame m = new MainFrame(); } } \添加窗口 package com.frame.project; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.List; import javax.swing.JFrame; import com.lovo.netCRM.component.LovoButton; import com.lovo.netCRM.component.LovoTxt; public class addFrame extends JFrame { private LovoTxt codeTxt=new LovoTxt("学号", 50, 20, this); private LovoTxt nameTxt=new LovoTxt("姓名", 50, 70, this); private LovoTxt sexTxt=new LovoTxt("性别", 50, 120, this); private LovoTxt eduTxt=new LovoTxt("学历", 50, 170, this); public addFrame() { this.setLayout(null); LovoButton addButton=new LovoButton("添加", 100, 200, this); addButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // 文本款封装学生对象 StudentBean student=new StudentBean(); student.setCode(Integer.parseInt(codeTxt.getText())); student.setName(nameTxt.getText()); student.setSex(sexTxt.getText()); student.setEdu(eduTxt.getText()); MainFrame.list.add(student); FilePress.write(MainFrame.list); addFrame.this.dispose(); new MainFrame(); } }); this.setSize(400, 300); this.setVisible(true); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(3);; } } \修改窗口 package com.frame.project; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import com.lovo.netCRM.component.LovoButton; import com.lovo.netCRM.component.LovoLabel; import com.lovo.netCRM.component.LovoTxt; public class UpdateFrema extends JFrame { private LovoLabel codeLable = new LovoLabel("学号", 50, 20, this); private LovoLabel nameLable = new LovoLabel("姓名", 50, 60, this); private LovoLabel sexLable = new LovoLabel("性别", 50, 100, this); private LovoTxt eduTxt = new LovoTxt("学历", 50, 140, this); private StudentBean student; public UpdateFrema(StudentBean bean) { this.setLayout(null); this.student = bean; LovoButton updateButton = new LovoButton("修改", 100, 200, this); init(student); updateButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { student.setEdu(eduTxt.getText()); FilePress.write(MainFrame.list); UpdateFrema.this.dispose(); new MainFrame(); } }); this.setSize(400, 300); this.setVisible(true); this.setLocationRelativeTo(null); this.setDefaultCloseOperation(3); } private void init(StudentBean s) { codeLable.setText(student.getCode() + ""); nameLable.setText(student.getName()); sexLable.setText(student.getSex()); } }
package com.frame.project; //学生类 public class StudentBean { /**学号*/ private int code; /**姓名*/ private String name; /**性别*/ private String sex; /**学历*/ private String edu; public StudentBean() { super(); // TODO Auto-generated constructor stub } public StudentBean(int code, String name, String sex, String edu) { super(); this.code = code; this.name = name; this.sex = sex; this.edu = edu; } public int getCode() { return code; } public void setCode(int code) { this.code = code; } 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 getEdu() { return edu; } public void setEdu(String edu) { this.edu = edu; } @Override public String toString() { return "StudentBean [code=" + code + ", name=" + name + ", sex=" + sex + ", edu=" + edu + "]"; } }
package com.frame.project; 文件读写操作 import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Reader; import java.io.Writer; import java.util.ArrayList; import java.util.List; public class FilePress { public static List<StudentBean> read() { List<StudentBean> list = new ArrayList<StudentBean>(); Reader r = null; BufferedReader br = null; try { r = new FileReader("student.txt"); br = new BufferedReader(r); String str; while ((str = br.readLine()) != null) { String[] stu = str.split("\s+"); StudentBean s = new StudentBean(); s.setCode(Integer.parseInt(stu[0])); s.setName(stu[1]); s.setSex(stu[2]); s.setEdu(stu[3]); list.add(s); } } catch (Exception e) { e.printStackTrace(); } finally { try { br.close(); r.close(); } catch (IOException e) { e.printStackTrace(); } } return list; } public static void write(List<StudentBean> list) { Writer w = null; try { w = new FileWriter("student.txt"); for (StudentBean s : list) { w.write(s.getCode() + " " + s.getName() + " " + s.getSex() + " " + s.getEdu()+" "); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { w.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void main(String[] args) { // FilePress f = new FilePress(); // List<StudentBean> list = f.read(); // System.out.println(list); } }
此工程用到了文件的读取和写入,集合和对象多种java中重要的概念,及算法,熟练地掌握并且了解此工程的每一步对今后的学习有着深刻的意义