这篇文章给大家分享的是有关java中学生信息管理系统MVC架构的示例分析的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。
一、项目结构
学生信息管理系统分三层进行实现。student.java主要提供数据,cotroller.java的功能是绑定试图和计算数据。Stuview.java用于单一的用来显示数据。
二、源码
1、Student 类
import java.io.Serializable; public class Student implements Serializable { //序列化id private static final long serialVersionUID = 9088453456517873574L; int num; String name; String sex; int age; float grade; public Student(int num ,String nameString,String sexString,int g,float f){ this.num =num; name = nameString; sex =sexString; age =g; grade =f; } public int getNum(){ return num; } public String getName(){ return name; } public String getSex(){ return sex; } public int getAge(){ return age; } public float getGrades(){ return grade; } public String toString(){ return "姓名:"+name+"学号:"+num+"性别:"+sex+"年龄:"+age+"成绩:"+grade; } }
2、Cotroller类
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.Iterator; public class Cotroller { //student数据集合 private ArrayList<Student> list; public Cotroller(ArrayList<Student> l){ this.list =l; } public ArrayList<Student> getList() { return list; } public void setList(ArrayList<Student> list) { this.list = list; } public void add(Student s) { list.add(s); } public void remove(int id) { for(Iterator<Student> iter = list.iterator(); iter.hasNext();) { Student s = iter.next(); if(s.getNum() == id) { list.remove(s); } } } public String printAll(int i) { return list.get(i).toString(); } public void fileOt() throws FileNotFoundException{ FileOutputStream fo = new FileOutputStream("/home/nazi/2.txt"); try { ObjectOutputStream so = new ObjectOutputStream(fo); so.writeObject(list); so.close(); } catch (IOException e) { e.printStackTrace(); } } @SuppressWarnings("unchecked") public void fileIn() throws FileNotFoundException{ FileInputStream fi = new FileInputStream("/home/nazi/2.txt"); try { ObjectInputStream si = new ObjectInputStream(fi); list = (ArrayList<Student>) si.readObject(); si.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
3、StuView类
import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.FileNotFoundException; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JTextArea; import javax.swing.JTextField; public class StuView { private static Cotroller cotroller; public static void main(String args[]){ //创建管理者 cotroller = new Cotroller(new ArrayList<Student>()); //界面 initFrame(); } protected static void initFrame(){ JFrame frame = new JFrame("学生信息管理系统"); frame.setSize(600,600); frame.setLocation(500, 100); frame.setLayout(null); //生成组件 final JTextField name = new JTextField(); name.setBounds(79, 10, 103, 25); final JTextField num = new JTextField(); num.setBounds(79, 53, 103, 25); final JTextField sex = new JTextField(); sex.setBounds(79, 101, 103, 25); final JTextField age = new JTextField(); age.setBounds(79, 161, 103, 25); final JTextField g1 = new JTextField(); g1.setBounds(79, 216, 103, 25); final JTextArea show = new JTextArea(); show.setBounds(194, 12, 388, 274); frame.add(show); show.setFont(new Font("Serif",Font.BOLD,18)); frame.add(show); frame.add(name); frame.add(num); frame.add(sex); frame.add(age); frame.add(g1); frame.add(show); JLabel label = new JLabel("学号:"); label.setBounds(12, 55, 63, 13); frame.getContentPane().add(label); JLabel label_1 = new JLabel("姓名:"); label_1.setBounds(12, 10, 63, 13); frame.getContentPane().add(label_1); JLabel label_2 = new JLabel("性别:"); label_2.setBounds(12, 110, 63, 13); frame.getContentPane().add(label_2); JLabel label_3 = new JLabel("年龄:"); label_3.setBounds(12, 167, 63, 13); frame.getContentPane().add(label_3); JLabel label_4 = new JLabel("成绩:"); label_4.setBounds(12, 226, 70, 13); frame.getContentPane().add(label_4); //添加学生 JButton btnAdd =new JButton("添加"); btnAdd.setBounds(12, 362, 104, 23); frame.add(btnAdd); btnAdd.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { Student s1 = new Student(Integer.parseInt(num.getText()),name.getText(), sex.getText(),Integer.parseInt(age.getText()),Integer.parseInt(g1.getText())); //放到集合 cotroller.getList().add(s1); //打印 for(int i = 0;i<cotroller.getList().size();i++){ show.append("\n"); show.append(cotroller.printAll(i)); } } }); //保存为文件 JButton btnSave =new JButton("保存");; btnSave.setBounds(478, 362, 104, 23); frame.add(btnSave); btnSave.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { try { cotroller.fileOt(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }); //刷新 JButton btnRefresh = new JButton("刷新"); btnRefresh.setBounds(327, 362, 104, 23); frame.add(btnRefresh); btnRefresh.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { try { cotroller.fileIn(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } //打印 for(int i = 0;i<cotroller.getList().size();i++){ show.append("\n"); show.append(cotroller.printAll(i)); } } }); //删除 JButton button_1 = new JButton("删除"); button_1.setBounds(169, 362, 104, 23); button_1.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent arg0) { // TODO Auto-generated method stub } }); frame.add(button_1); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } }
三、运行效果(初始界面、添加界面、刷新界面)
感谢各位的阅读!关于“java中学生信息管理系统MVC架构的示例分析”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!