多平台统一管理软件接口,如何实现多平台统一管理软件接口
460
2023-03-17
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
* @Author nazi
* @version 1.0
* */
public class Cotroller {
//student数据集合
private ArrayList
public Cotroller(ArrayList
this.list =l;
}
/*
* rturn a ArrayList
* */
public ArrayList
{
return list;
}
/*
* 初始化Student数组
* */
public void setList(ArrayList
{
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 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
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
//界面
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 show.appenFEaMliFd("\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 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); } } 三、运行效果(初始界面、添加界面、刷新界面)
show.appenFEaMliFd("\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 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); } } 三、运行效果(初始界面、添加界面、刷新界面)
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);
}
}
三、运行效果(初始界面、添加界面、刷新界面)
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~