本文實例為大家分享了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