本文實例為大家分享了Java實現學生管理系統的具體代碼,供大家參考,具體內容如下
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
package BookDemo_1; import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Test { public static void main(String[] args) { StudentSys stuSys= new StudentSys( "學生管理系統" ); stuSys.initWin(); } } class StudentSys extends JFrame{ private JPanel p1,p2,p3,combop; private JTabbedPane tab; private Container container; private JButton b1,b2; private Listener listener; private Label nameLabel; private Label gradeLabel; private Label showLabel; private JTextField textName; private JTextField textGrade; private TextArea showGradeArea; /* * 查找 * */ private Label searchLabel; private JTextField searchText; private JButton sBut; private JTextField resultText; private String[] name; private String[] grade; /* * 排序 * */ private TextArea showTextArea; private JButton sortBut; private int countNum=0; private JButton clearBut; public StudentSys(String str){ super(str); this.name=new String[100]; this.grade=new String[100]; listener = new Listener(); tab = new JTabbedPane(JTabbedPane.TOP); //容器 container = this.getLayeredPane(); //對象化面板 combop = new JPanel(); p1 = new JPanel(); p2 = new JPanel(); p3 = new JPanel(); b1 =new JButton("確認添加"); b2 =new JButton("撤回添加"); nameLabel =new Label("姓名"); gradeLabel =new Label("成績"); showLabel=new Label("當前記錄為零! "); textName =new JTextField(15); textGrade =new JTextField(15); showGradeArea=new TextArea(); /* * 查找 * */ searchLabel=new Label("請輸入姓名:"); searchText=new JTextField(15); sBut=new JButton("確認查找"); resultText=new JTextField(15); /* * 排序 * */ showTextArea=new TextArea(); sortBut=new JButton("成績排序"); clearBut=new JButton("清空數據"); } public void initWin(){ this.setBounds(300, 300, 500, 400); this.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { super.windowClosing(e); System.exit(0); }}); layoutWin(); this.setVisible(true); } private void layoutWin(){ tab.add(p1,"成績輸入"); tab.add(p2,"成績查詢"); tab.add(p3,"成績排序"); combop.add(new JLabel("學生信息管理系統")); container.setLayout(new BorderLayout()); container.add(combop,BorderLayout.NORTH); container.add(tab,BorderLayout.CENTER); Container con1=new Container(); con1.setLayout(new FlowLayout()); con1.add(nameLabel); con1.add(textName); con1.add(gradeLabel); con1.add(textGrade); p1.add(con1,BorderLayout.NORTH); p1.add(con1); p1.add(showGradeArea); Container con2=new Container(); con2.setLayout(new FlowLayout()); con2.add(b1); con2.add(b2); con2.add(showLabel); p1.add(con2); b1.addActionListener(listener); b2.addActionListener(listener); /* * 查找布局 * */ Container con3=new Container(); con3.setLayout(new FlowLayout()); con3.add(searchLabel); con3.add(searchText); con3.add(sBut); p2.add(con3,BorderLayout.NORTH); sBut.addActionListener(listener); p2.add(resultText); /* * 排序布局 * */ p3.add(showTextArea); p3.add(sortBut); p3.add(clearBut); sortBut.addActionListener(listener); clearBut.addActionListener(listener); } /* * java內部類實現ActionListener接口 * */ class Listener implements ActionListener{ @Override public void actionPerformed(ActionEvent e) { if (e.getSource()==b1){ if ((textName.getText().equals( "" ))||(textGrade.getText().equals( "" ))){ showLabel.setText( "添加失敗(姓名,成績不能有空)!" ); } else { name[countNum]=textName.getText(); grade[countNum]=textGrade.getText(); countNum++; String area= "添加成功,當前有" +countNum+ "條記錄" ; showLabel.setText(area); sortMess( false ); textName.setText( "" ); textGrade.setText( "" ); } } if (e.getSource()==b2){ if (countNum> 0 ){ countNum--; String area= "撤回成功,當前有" +countNum+ "條記錄" ; showLabel.setText(area); sortMess( false ); } } if (e.getSource()==sBut){ if (!searchText.getText().equals( "" )){ searchMess(searchText.getText()); } } if (e.getSource()==sortBut){ sortMess( true ); } if (e.getSource()==clearBut){ if (!showTextArea.getText().equals( "" )){ showTextArea.setText( "" ); } } } public void sortMess( boolean sign) { // TODO Auto-generated method stub if (sign){ for ( int i= 0 ;i<countNum;i++){ for ( int j=i+ 1 ;j<countNum;j++){ if (Integer.parseInt(grade[i])<Integer.parseInt(grade[j])){ String s1,s2; s1=name[i]; s2=grade[i]; name[i]=name[j]; grade[i]=grade[j]; name[j]=s1; grade[j]=s2; } } } } else { if (!showGradeArea.getText().equals( "" )){ showGradeArea.setText( "" ); } } for ( int i= 0 ;i<countNum;i++){ String content= "姓名:" +name[i]+ "\t" + "成績" +grade[i]; if (sign)showTextArea.append(content+ "\n" ); else showGradeArea.append(content+ "\n" ); } } public void searchMess(String n) { // TODO Auto-generated method stub for ( int i= 0 ;i<countNum;i++){ if (name[i].equals(n)){ String content= "姓名:" +name[i]+ "," + "成績" +grade[i]; resultText.setText(content); return ; } } resultText.setText( "未找到該學生!" ); } } } |
以上就是本文的全部內容,希望對大家學習Java程序設計有所幫助。