您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)java中學(xué)生信息管理系統(tǒng)MVC架構(gòu)的示例分析的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
一、項(xiàng)目結(jié)構(gòu)
學(xué)生信息管理系統(tǒng)分三層進(jìn)行實(shí)現(xiàn)。student.java主要提供數(shù)據(jù),cotroller.java的功能是綁定試圖和計(jì)算數(shù)據(jù)。Stuview.java用于單一的用來(lái)顯示數(shù)據(jù)。
二、源碼
1.1、Student 類
/* * @FileName: Student.class * @version:1.0 * @author:nazi * 描述:模型層 * */ import java.io.Serializable; /* * Summary: Student類實(shí)現(xiàn)序列化接口,用于對(duì)象的保存 * @author:nazi * @version:1.0 * */ 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+"學(xué)號(hào):"+num+"性別:"+sex+"年齡:"+age+"成績(jī):"+grade; } }
1.2、Cotroller類
/* * 文件名: Cotroller.java * 描述:mvc中的c,用來(lái)管理模型層的數(shù)據(jù) * @authur:Nazi * function :增、刪、改、查、保存、更新 * */ 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; /* * Cotroller類集中對(duì)ArrayList<Student>進(jìn)行操作 * @Author nazi * @version 1.0 * */ public class Cotroller { //student數(shù)據(jù)集合 private ArrayList<Student> list; public Cotroller(ArrayList<Student> l){ this.list =l; } /* * rturn a ArrayList<Student> * */ public ArrayList<Student> getList() { return list; } /* * 初始化Student數(shù)組 * */ public void setList(ArrayList<Student> list) { this.list = list; } /* * add a student to the List * */ public void add(Student s) { list.add(s); } /* * remove the student from list * */ public void remove(int id) { for(Iterator<Student> iter = list.iterator(); iter.hasNext();) { Student s = iter.next(); if(s.getNum() == id) { list.remove(s); } } } /* * print the specific student * */ public String printAll(int i) { return list.get(i).toString(); } /* * 功能簡(jiǎn)述:將實(shí)現(xiàn)序列化后的對(duì)象寫(xiě)入到文件中。 * 文件輸出地址:"/home/nazi/2.txt" 文件地址可以更改 * */ 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(); } } /* * function: 從指定路徑讀取文件,然后將對(duì)象狀態(tài)進(jìn)行賦值使用 * * */ @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(); } } }
1.3、StuView類
/* * FileName:StuView.class * 描述:以特定的方式展示數(shù)據(jù) * @Atuthor:nazi * @version:1.0 * */ 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; /* * StuView 類用于展示數(shù)據(jù) * @author:nazi * @version:1.0 * */ public class StuView { private static Cotroller cotroller; public static void main(String args[]){ //創(chuàng)建管理者 cotroller = new Cotroller(new ArrayList<Student>()); //界面 initFrame(); } /* * InitFrame()中含有各種類型的控件,以及控件所對(duì)應(yīng)的事件處理步驟 * */ protected static void initFrame(){ JFrame frame = new JFrame("學(xué)生信息管理系統(tǒng)"); 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("學(xué)號(hào):"); 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("成績(jī):"); label_4.setBounds(12, 226, 70, 13); frame.getContentPane().add(label_4); //添加學(xué)生 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); } }
三、運(yùn)行效果(初始界面、添加界面、刷新界面)
感謝各位的閱讀!關(guān)于“java中學(xué)生信息管理系統(tǒng)MVC架構(gòu)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。