java學(xué)生信息管理系統(tǒng)MVC架構(gòu)詳解
本文實例為大家分享了java學(xué)生信息管理系統(tǒng)MVC架構(gòu),供大家參考,具體內(nèi)容如下
一、項目結(jié)構(gòu)
學(xué)生信息管理系統(tǒng)分三層進(jìn)行實現(xiàn)。student.java主要提供數(shù)據(jù),cotroller.java的功能是綁定試圖和計算數(shù)據(jù)。Stuview.java用于單一的用來顯示數(shù)據(jù)。
二、源碼
1.1、Student 類
/* * @FileName: Student.class * @version:1.0 * @author:nazi * 描述:模型層 * */ import java.io.Serializable; /* * Summary: Student類實現(xiàn)序列化接口,用于對象的保存 * @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é)號:"+num+"性別:"+sex+"年齡:"+age+"成績:"+grade; } }
1.2、Cotroller類
/* * 文件名: Cotroller.java * 描述:mvc中的c,用來管理模型層的數(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類集中對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(); } /* * 功能簡述:將實現(xiàn)序列化后的對象寫入到文件中。 * 文件輸出地址:"/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: 從指定路徑讀取文件,然后將對象狀態(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()中含有各種類型的控件,以及控件所對應(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é)號:"); 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); //添加學(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); } }
三、運行效果(初始界面、添加界面、刷新界面)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Boot/Angular整合Keycloak實現(xiàn)單點登錄功能
Keycloak新的發(fā)行版命名為Quarkus,專為GraalVM和OpenJDK HotSpot量身定制的一個Kurbernetes Native Java框架,計劃2019年底正式發(fā)布。這篇文章主要介紹了Spring Boot/Angular整合Keycloak實現(xiàn)單點登錄,需要的朋友可以參考下2019-10-10org.slf4j.Logger中info()方法的使用詳解
這篇文章主要介紹了org.slf4j.Logger中info()方法的使用詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12Java使用lambda自定義Arrays.sort排序規(guī)則說明
這篇文章主要介紹了Java使用lambda自定義Arrays.sort排序規(guī)則說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-05-05關(guān)于spring data jpa一級緩存的問題
這篇文章主要介紹了關(guān)于spring data jpa一級緩存的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11SpringBoot權(quán)限認(rèn)證-Sa-Token的使用詳解
Sa-Token是一款輕量級Java權(quán)限認(rèn)證框架,它簡化了權(quán)限管理,提高了開發(fā)效率,本文通過實例介紹了Sa-Token的基本概念、與其他框架的比較、基本語法和高級用法,并探討了其核心原理和實際應(yīng)用場景,感興趣的朋友一起看看吧2024-09-09java中如何實現(xiàn) zip rar 7z 壓縮包解壓
這篇文章主要介紹了java中如何實現(xiàn) zip rar 7z 壓縮包解壓問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07