java学生信息管理系统MVC架构详解

 更新时间:2017年11月14日 14:05:28   作者:那兹  
这篇文章主要为大家详细介绍了java学生信息管理系统MVC架构的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了java学生信息管理系统MVC架构,供大家参考,具体内容如下

一、项目结构

        学生信息管理系统分三层进行实现。student.java主要提供数据,cotroller.java的功能是绑定试图和计算数据。Stuview.java用于单一的用来显示数据。

二、源码

1.1、Student 类

/* 
 * @FileName: Student.class 
 * @version:1.0 
 * @author:nazi 
 * 描述:模型层 
 * */ 
import java.io.Serializable; 
 
/* 
 * Summary: Student类实现序列化接口,用于对象的保存 
 * @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+"学号:"+num+"性别:"+sex+"年龄:"+age+"成绩:"+grade; 
     
  } 
 
} 

1.2、Cotroller类

/* 
 * 文件名: Cotroller.java 
 * 描述:mvc中的c,用来管理模型层的数据 
 * @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>进行操作 
 * @Author nazi 
 * @version 1.0 
 * */ 
public class Cotroller { 
   
  //student数据集合 
  private ArrayList<Student> list; 
   
  public Cotroller(ArrayList<Student> l){ 
    this.list =l; 
  } 
   
  /* 
   * rturn a ArrayList<Student> 
   * */ 
  public ArrayList<Student> getList() 
  { 
    return list; 
  } 
   
  /* 
   * 初始化Student数组 
   * */ 
  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(); 
  } 
   
  /* 
   * 功能简述:将实现序列化后的对象写入到文件中。 
   * 文件输出地址:"/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: 从指定路径读取文件,然后将对象状态进行赋值使用 
   * 
   * */ 
  @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 
 * 描述:以特定的方式展示数据 
 * @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 类用于展示数据 
 * @author:nazi 
 * @version:1.0 
 * */ 
public class StuView {  
  private static Cotroller cotroller; 
  public static void main(String args[]){ 
    //创建管理者 
    cotroller = new Cotroller(new ArrayList<Student>()); 
    //界面 
    initFrame(); 
  } 
   
  /* 
   * 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);  
    } 
 
} 

三、运行效果(初始界面、添加界面、刷新界面)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Spring整合redis(jedis)实现Session共享的过程

    Spring整合redis(jedis)实现Session共享的过程

    这篇文章主要介绍了Spring整合redis(jedis)实现Session共享,需要的朋友可以参考下
    2018-06-06
  • Java9新特性中的模块化详解

    Java9新特性中的模块化详解

    今天介绍一个Java 9的功能,模块化(Modular),这可能使Java有史以来最大的Feature,对Java9模块化相关知识感兴趣的朋友一起看看吧
    2022-03-03
  • JavaWeb页面中防止点击Backspace网页后退情况

    JavaWeb页面中防止点击Backspace网页后退情况

    当键盘敲下后退键(Backspace)后怎么防止网页后退情况呢?今天小编通过本文给大家详细介绍下,感兴趣的朋友一起看看吧
    2016-11-11
  • SpringBoot开发实战系列之动态定时任务

    SpringBoot开发实战系列之动态定时任务

    在我们日常的开发中,很多时候,定时任务都不是写死的,而是写到数据库中,从而实现定时任务的动态配置,下面这篇文章主要给大家介绍了关于SpringBoot开发实战系列之动态定时任务的相关资料,需要的朋友可以参考下
    2021-08-08
  • Java 函数编程详细介绍

    Java 函数编程详细介绍

    这篇文章主要介绍了Java函数式编程,lambda表达式可以被认为是一个匿名函数,可以在函数接口的上下文中使用。函数接口是只指定一个抽象方法的接口,下面来看文章的详细内容,需要的朋友可以参考下
    2021-11-11
  • springboot2.3 整合mybatis-plus 高级功能及用法详解

    springboot2.3 整合mybatis-plus 高级功能及用法详解

    这篇文章主要介绍了springboot2.3 整合mybatis-plus 高级功能,本文通过图文并茂的形式给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-09-09
  • 解决Spring中@Value注解取值为null问题

    解决Spring中@Value注解取值为null问题

    近期应用中因业务迭代需要接入 user 客户端,接入后总是启动失败,报注册 user bean 依赖的配置属性为 null,所以接下来小编就和大家一起排查分析这个问题,感兴趣的小伙伴跟着小编一起来看看吧
    2023-08-08
  • 面试官:怎么做JDK8的垃圾收集器的调优(面试常问)

    面试官:怎么做JDK8的垃圾收集器的调优(面试常问)

    这篇文章主要介绍了面试官:怎么做JDK8的垃圾收集器的调优,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-08-08
  • Java处理延时任务的常用几种解决方案

    Java处理延时任务的常用几种解决方案

    本文主要介绍了Java处理延时任务的常用几种解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • 使用filter实现url级别内存缓存示例

    使用filter实现url级别内存缓存示例

    这篇文章主要介绍了使用filter实现url级别内存缓存示例,只需要一个静态类,在filter中调用,也可以全部写到filt里面。可以根据查询参数分别缓存,需要的朋友可以参考下
    2014-03-03

最新评论