本文实例为大家分享了java学生信息管理系统mvc架构,供大家参考,具体内容如下
一、项目结构
学生信息管理系统分三层进行实现。student.java主要提供数据,cotroller.java的功能是绑定试图和计算数据。stuview.java用于单一的用来显示数据。
二、源码
1.1、student 类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
/*
* @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类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
/*
* 文件名: 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类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
/*
* 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 );
}
}
|
三、运行效果(初始界面、添加界面、刷新界面)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://blog.csdn.net/weixin_36571185/article/details/61614723