|
该用户从未签到
|
- import java.applet.Applet;
- t$ ?. N u) o4 B9 |$ A9 X - import java.awt.*;
: h5 T6 Y9 B, @ - import java.awt.event.*;
) {. T) B$ Q/ ]8 Q/ Y; H - import java.util.*;
+ s+ A9 w e$ k/ s - import java.lang.*;
& W/ K% x* C& v' |
' K% S, p/ r$ A2 W6 N0 ^- public class Students extends Applet implements ActionListener" S; B$ m0 Y$ \9 @! L
- {' s! X) i9 h* w6 {3 v& T; l1 N
- Vector StuInf=new Vector();
+ x5 ]/ G0 [ d. i' `5 i7 g - StudentInf SI;" z# N; ~" N5 u$ R
- String xm;
+ |7 u- d; i' F% J - String bj;% c, U2 r. j: X- y5 z, F1 J5 C
- int i,j,xh,cj;+ S. h8 u+ e0 Q7 Z3 N. q' ~2 \: i
- static int mid;
5 w% C9 [1 t& ~ - Label prompt1=new Label("学生成绩管理系统");) u2 G3 y% U. r" y
- Label prompt2=new Label(" 用户:");! q' n9 d) w7 f5 J& G7 i7 F; _
- Label prompt3=new Label(" 密码:");
9 @- `9 ?5 X' [% ^5 {4 \9 X - Label prompt4=new Label(" 班级:");
; @7 F; D @. _ - Label prompt5=new Label(" 成绩:");; c+ |, Y0 h5 c ]& [
- TextField input1=new TextField(8);- a1 c) w# S1 R7 E/ c! b
- TextField input2=new TextField(8);+ ~" V1 p1 T4 H6 r
- TextField input3=new TextField(8);& h/ y/ k+ ]9 m, U q5 H$ X! h
- TextField input4=new TextField(8);
y% [" H( ~; e/ L - Button btn1=new Button("登录");% ?( \, W* u1 j' _1 [+ o' u, ?
- Button btn2=new Button("增加");8 G7 f- x% y! P u; O5 r
- Button btn3=new Button("修改"); f0 ?8 l1 l" u
- Button btn4=new Button("删除");
6 p! g" s: O) M6 C -
) h$ ?- n- s; D) n1 {1 j - public void init()* z+ j4 N! y/ K# f6 J& ^
- {
# m0 V4 Q5 z) K( \# Q) | - setLayout(new GridLayout(6,3));
% M; q5 x8 t4 R+ C4 z - add(new Label());
* @2 [+ r% V4 ~+ g) c7 H. _ - add(prompt1);% @+ [1 ]" D& [& O/ i
- add(new Label());# ^0 l& s' Q$ E& \/ T- |( m! R
- add(prompt2);
3 [: K" n) f$ r4 N% s - add(input1);; H S X3 T* f! N& \: W& Y
- add(new Label());0 D& t9 y, h' f$ m9 n
- add(prompt3);2 n/ `. g# F2 Q8 O1 ?
- add(input2);& t9 z: \# \, V8 U: c& f
- add(btn1);
9 {1 U6 h/ q( E3 o, E/ A7 f. ?/ u - add(prompt4);# i0 B+ M8 t* J. A
- add(input3);
5 l, [) W9 [: G7 x- ~8 N0 A% r - add(new Label());- E3 G* t9 D5 C- A
- add(prompt5);
5 ~7 p" H5 [4 L - add(input4);& P( p3 s; s: P- N
- add(new Label());0 T" P3 b: _/ `1 b N
- add(btn2);
1 E5 ~# A. e' H5 M. Z" } - add(btn3);$ m9 _! ^: b% y
- add(btn4);
! Y! i W2 v. r0 K - prompt4.setVisible(false);2 z' ^8 f0 o/ U1 [
- prompt5.setVisible(false);
3 o& Y3 P- T( a! S9 V; C - input3.setVisible(false);# ]; G V* t; E. f
- input4.setVisible(false);
9 n, B$ Y" {5 I7 E5 x - btn2.setVisible(false);
. S; k- q# `5 J9 g \% F+ X, Z - btn3.setVisible(false);6 u Q6 r3 ~) R8 C. j( K
- btn4.setVisible(false);
d1 {3 ^; E, g9 P! k* l - btn1.addActionListener(this);
* N( v, [, ]# D7 S - btn2.addActionListener(this);3 J' ] L3 T" G8 t5 J% o
- btn3.addActionListener(this);
, }- w- d' @% m - btn4.addActionListener(this);4 o1 v8 T, i* Y0 U
- }
6 X0 W2 `4 [( g n( Y/ C - public void actionPerformed(ActionEvent e)
4 L8 r i7 i& L9 @9 } - {
; K* R: ?- r$ f. r1 w. s; ~ - if(e.getActionCommand()=="登录")9 o0 c; t2 }* q) q
- {* Z/ ^6 `, ?9 A$ m/ d4 J
- String a,b;! w9 B/ q s% G, E
- a=input1.getText();" D+ c0 J ~4 T. y& D
- b=input2.getText();2 T7 ~/ W& c+ O- l! J( K$ c
- input1.setText("");; {9 o3 [6 R# N6 N0 H) ?* D
- if((a.equals("12")==true)&&(b.equals("12")==true))
' v- `4 x& r# a* r - {
6 X! T2 j' o' s n' r. C - prompt2.setText(" 姓名:");
: R( [ W+ p5 i( a# M' q: ~ - prompt3.setText(" 学号:"); 8 F+ v; O( ^# y+ `; K
- prompt4.setVisible(true);9 i0 o- _: Y. Y# n+ x4 t
- prompt5.setVisible(true);9 w2 l" h7 N8 F
- input3.setVisible(true);
* Q, v9 E8 s1 u& b - input4.setVisible(true);
* Z' U5 c% @$ [, M- i! p - btn2.setVisible(true);
) l2 k: E4 v& F0 O# Q - btn3.setVisible(true);
2 @- l9 k" l% X z - btn4.setVisible(true);
! M# i+ V: M/ D* D - btn3.setEnabled(false);
" t ], O/ v, g& G. P2 g: c( [! R - btn4.setEnabled(false);
0 k) D9 H2 y3 y, I) }) _ - btn1.setLabel("查询");. @' L" I, a, [8 K( I3 Q1 T
- input1.setText("登录成功");
- d* N9 O+ k; |# `- | - input1.selectAll();" J: W7 A7 \- B# H
- }
1 z: O$ U: Y- i A: _* H - else
4 k% H1 }) H8 i- U6 p. v - input2.setText("用户名或密码错");) p% j+ {, b% \ U/ F5 a
- }
3 }' o! x& N) {5 p6 O0 l6 H - if(e.getActionCommand()=="增加")! M- n7 m2 P) u, [; B p- f! `
- {! X/ z* x9 z" V2 W7 I5 {
- boolean scucss=true;9 w' d% }; r( G
- try
" |# A( N' \7 b6 ? - {
" n) ~! L S6 C# Q( ?7 O - XingMing();
( O5 R# V, O% a l - }& P7 K. `1 ] e8 N! q% u
- catch(EmptyException as)1 a0 H ^, i T( D2 ~9 w
- {8 K8 ]/ p% }, k& n
- input1.setText("姓名不能为空");4 z- W% Z. D+ y3 k& O: c
- scucss=false;3 N6 f( y: |" K2 `9 r2 C+ P/ P/ s
- }
3 L6 t+ u$ s4 O! Q; F4 ] - try
2 G" e4 j' M' U8 W5 ^: ] - {
, Q7 G) q4 E0 l8 b1 ~' w - xh=Integer.parseInt(input2.getText());9 ^2 u# N; R' \0 h' d- r
- }' n( H2 D1 P' o h% F
- catch(NumberFormatException m)
3 Y, U3 D, R# B' n - {, u5 F3 {2 n7 {( U1 L6 d
- input2.setText("学号为空或格式错");
# [: k& ^, |7 k* y - scucss=false;: @# a# i+ B' {9 O8 l
- } 4 C+ I- N. u0 f0 U+ x
- bj=input3.getText();; Y: u2 A: w8 a
- try3 C8 a) [, {0 f
- {
/ [! }' I6 p0 ~' W* j- t0 b3 }* | - ChengJi();
/ w# U! Z3 F7 ?- }& G - }
1 E, U, n3 f2 }8 G5 A, x7 z - catch(EmptyException as)0 |5 x' X' v2 o
- {
" I+ e) ^: z6 L" }0 V$ `' p! I7 u% } - cj=-1;
6 c7 i# g! u! z( ? - }, M' l) U l" V" z
- catch(OverException dd)
6 t5 T" \' `( V' s& l - {) ?. S ]3 U6 i; J* { {
- input4.setText("应在0-100间");) X4 R. H7 ^4 {1 g4 Y
- scucss=false;
J8 O; b/ U; e% l) b% A% c# c - }
0 J, H* L ]8 v0 B: { - catch(NumberFormatException cm)
- \$ P+ U' \0 M; U2 ]$ X. C9 A - {, F4 l( r q- G- G
- input4.setText("成绩应为数据");! N! R9 |: h! P8 b$ K( d* j- `
- scucss=false;
( f0 D7 F$ K3 K- |& x6 @ - }# t* c. N' w9 y: [! N
- if(scucss==true)) O2 T5 E) r- V- B+ S* @" [
- {" X9 ?- T- o( o1 ~
- SI=new StudentInf(xm,xh,bj,cj);
$ W3 y* P1 s* g' Z8 y! M - Insert(SI);
1 x& T; H8 r9 L8 y. R! i" ~ - }9 j: M0 T6 W# R' n
- }: U& f/ ?0 T' d
- if(e.getActionCommand()=="修改")
7 c2 m# p8 c6 J, C2 p* } - {
6 `7 Z" E: f% }# n - xm=input1.getText();0 N1 u# r0 o, B% c+ u6 N2 k
- xh=Integer.parseInt(input2.getText());6 E: A( U% R1 J5 h t
- bj=input3.getText();- K; k& U9 `! ?$ \
- cj=Integer.parseInt(input4.getText());& e. c7 }+ N1 q/ ?" r: N
- SI=new StudentInf(xm,xh,bj,cj);
+ I0 B5 Z; | P, V; L - StuInf.setElementAt(SI, mid);
9 x& @9 Z& z1 n; i- M/ X - btn3.setEnabled(false);+ d# W) z% U0 {' }
- }
/ f0 B: u0 X7 `8 H5 r* e4 d! e& j - if(e.getActionCommand()=="删除")4 y8 K- k3 |: F* Y+ H6 D
- { U$ N. A6 \4 n4 ]: M/ p
- StuInf.removeElementAt(mid); : c. N& B$ R, t# Y' A) f
- btn4.setEnabled(false);2 v& ]) I7 r4 x% ~+ d! Y( H
- input1.setText("删除成功");. K- S2 [- O) J1 M. h/ O' }, w
- input2.setText("");
+ Q) r- U) H! H9 c# F# e - input3.setText("");& s) c# D* F/ P5 x
- input4.setText("");
: W; g& p+ [% h4 \ - }
2 t- W* |; \7 U - if(e.getActionCommand()=="查询")
, \/ u" S% F; X: D - {
1 x [1 p2 j- ~3 b' f; }9 A - boolean right=true;# w+ r4 M! e% L& Y/ P
- try+ m2 i4 o+ y! n3 ^! ^4 C
- {
% q1 M* u+ j. ^9 c7 u6 S - xh=Integer.parseInt(input2.getText());
6 B; K4 p& _8 ?: P8 ~ - }
; L* I% W" s5 F - catch(NumberFormatException m)
+ E9 @( z0 r; a# P5 B! o [ - {' H; ]: h2 Z- i
- input2.setText("学号为空或格式错");
1 k+ }: _ q0 W% U - right=false;
8 e5 i$ `) T' Q f - } # l h& w' u& R
- if(right==true)
/ [8 u3 A8 S. M7 k3 V5 c: y - {4 O: a3 Q2 ]/ s- e1 `9 O
- search(xh);
" `4 v. i. D) f" _- Y+ M; d3 x( l - btn3.setEnabled(true);7 Q% c! | S+ o7 d/ s
- btn4.setEnabled(true);2 o' Y: m' M+ B5 {
- }% R3 Z1 i( }: w8 G% d7 ]: H, S
- }
& K; b6 t2 y7 X3 W ^, A - 7 H: v' Q! d+ S; P
- }6 c5 b9 o! E" U$ I6 `5 ]
- //查找方法
; [' S; s* j9 K* r/ l2 _ - public void search(int k). O8 q$ k4 u [2 S6 S
- {0 z3 ~% @1 ?2 Q8 }4 E6 f$ P
- boolean exist=false;9 O) R6 d; k9 O+ E, K
- int low=0;4 ?( u! g( }* e) j& Q$ d" ~% J1 t
- int high=StuInf.size()-1;# P* |( f5 F/ T" \% z" a
- while(low<=high)- q. O1 N7 ^( J. c
- {
' l; \( K. u* M% J - mid=(high+low)/2;
+ n' C% j8 k( F, h# T) Y- `" g - StudentInf a1=(StudentInf) StuInf.elementAt(mid);
6 [0 H0 n7 y+ t6 [8 u - if(a1.getStuNo()==k)
# e1 P& s3 h X: h! Y - {
) G) i, @5 X1 P. W& B - SI=(StudentInf) StuInf.elementAt(mid);0 x* Q% q8 `1 g' ^7 c% j% h& T
- String x = String.valueOf(SI.getStuNo());( d. N1 H+ i+ f! W% l* W
- exist=true;
* H) ?; c% r( b4 W - input1.setText(SI.getname());
- P' [; {# y g. x& \- G# t& c - input1.selectAll();
# K% Y4 s! O: S: S& L - input2.setText("0"+x);
' j' {/ \* }) O* G - input3.setText(SI.getClassNo());
6 j9 A: f0 _+ q2 n% h2 O - if(SI.getLevel()==-1)
) K- X' p# Z2 o4 {6 s - input4.setText("未参加考试");" B/ P2 U7 a( ?
- else5 X/ `+ L' g. D5 s
- {9 [# Z- M: r& V0 J% ~+ }! \3 b
- String y = String.valueOf(SI.getLevel());
$ L2 ~$ ~( d+ h( D \; z% g2 y - input4.setText(y);
2 H0 X( T3 J8 y0 B' C! Q - }
1 @8 Y5 q' e9 x: c% a$ b0 P - break;
; Z3 w! h3 ]5 G) [0 _ - }4 ]( m" Y% K7 r' {+ ?
- else if(a1.getStuNo()<k)6 P1 y! r. m$ o( C" {- l# }
- low=mid+1;
4 V6 X% s3 T+ K" b; H - else- S& p; k, {$ [4 {! z4 y& y
- high=mid-1; - E% j# k* {0 J# V$ Y7 a1 f
- }
8 D. j# W: a- S0 D. o- ^6 n' W - if(exist==false) q7 a* S5 x( N- k7 Q1 V
- {
# ?, b* o9 D: Q8 ^) A - input1.setText("无此学号学生信息");" S3 g5 O3 N& K* {: d+ i$ {
- input1.selectAll();
, n* r8 _7 w! X7 D: N4 U( x - }
' G# v3 H) l' m0 J6 A2 ?) v9 i - }( _3 e0 q4 t: u& y, y I' i! m
- //添加方法" o2 q9 j; {9 I& n
- public void Insert(StudentInf q)
. @+ L0 W2 x; ~ - {# `: F2 q& v7 G) H
- int i=0;
7 `% F! n1 N* k [& q -
2 w* n6 V7 E8 ~ - if(StuInf.isEmpty()==true) 6 G* n; a8 o' r; o3 }6 x, f; F
- {
# ~# x A; A2 i - StuInf.addElement(q);% n5 Q" x9 B9 N' o8 f
- input1.setText("");
; S' |' ^) r0 G6 P. n5 d - input2.setText("");
( R$ Z9 E9 C5 q - input3.setText("");8 T& k- q1 C* X5 I" f
- input4.setText("");
' P' B/ F% G, ?- S' [- B# ]3 a - }
; V! H% U# w2 ] S - else. j$ S; P# U. F; T8 E, |- G
- {( }# e( u, u$ B$ y* S9 f* g
- StudentInf xh;. e+ c3 f J/ e
- xh=(StudentInf) StuInf.firstElement();, A d, \% J$ _ |& t
- while(xh.getStuNo()<q.getStuNo())4 ^ R& {- Q4 n* [1 l$ o
- {2 d' u$ |/ t; t
- i++;& _5 m! q- M6 L6 W' F: v
- if(i<StuInf.size())
j% {$ N5 f1 X2 ]" q" c - xh=(StudentInf) StuInf.elementAt(i);1 j$ M8 d- F' p$ j# p
- else 5 u# ~! R6 u1 s: d) j
- break;; X8 x+ E |) s9 X- w; O
- }
& c1 _8 S. G- C# w - if(xh.getStuNo()==q.getStuNo())3 u4 `8 J$ K' E. X- p+ K; v
- {
7 l1 m, {% t* R; d9 f4 O6 Q7 b - input2.setText("此学生信息已存在");
8 ]: }# L' u' o* a - input2.requestFocus();% K! k& y+ E! U- y" l2 p
- input2.selectAll();
; G; W/ d& s1 S" `5 S. S9 q - }
; T8 F% O( U Q4 T- s5 ? - else
; v" u- ~' u$ h3 p! [: r - {
# ]$ m' X) {1 Q9 _" s - StuInf.insertElementAt(q,i);
2 T; L" M+ C+ E4 W8 M3 r2 V# N+ A - input1.setText("");- `# b9 c' A" Y9 F1 R$ ]4 }" X
- input2.setText("");/ i( h" s5 l/ I4 |; f9 F3 i
- input3.setText("");7 b9 Q: \1 j- w& c
- input4.setText("");6 g. { h, B1 p$ D" X2 r! G
- }
; _/ u4 [% u+ c& n - }
' S- K( }* E6 `& {) j9 E) @ - }
' O/ z+ ?; h* G! p' F -
9 S8 _0 u: L; K( s& H0 O! Q - //异常类. ?9 i0 [/ k; {% x. l% g& f# i
- class OverException extends Exception F: }8 I8 u( k! F0 w
- {
/ x3 w5 |' K, s: Y - String over;" v" X. e4 _ \3 G Q+ K( U
- }
/ C( ^+ p2 X5 @0 ]" c - class EmptyException extends Exception* a$ i6 c- K1 }/ t* s8 M) e+ U
- {
) P6 Y$ ^9 D( K* N8 Y - String empty;. B& u5 v# [. F. V0 I
- }1 O& w. M0 c# j, `' i
- void XingMing() throws EmptyException
8 x" t- P& W$ k6 n* M7 i& ~ - {
6 D4 }# X; G7 V7 d8 `* J - if((input1.getText()).equals(""))
+ j4 N& u' @" _1 F. L+ G& ? - throw (new EmptyException());
$ R% i: ]6 Z0 S g( A7 f0 T3 C - else4 C/ R$ b9 w3 T9 h/ v. w* L
- xm=input1.getText();
) k. e) \+ O. y$ S7 L! H - }
/ [" W) L; }0 h! d4 X# Y - void ChengJi() throws OverException,EmptyException 4 F. t$ p" }* p! X1 v2 c* E
- {
# _; r9 j5 K0 F% s; @$ X/ w - if((input4.getText()).equals(""))5 {- ] `; X* e) S
- throw(new EmptyException());
$ G3 f5 ?9 [; N( {. D X# R6 i8 G - else
' U4 l- @+ I ?- n |: c) X0 g - cj=Integer.parseInt(input4.getText());
* v0 c4 L+ \& [9 A - if(cj<0||cj>100)1 z& c7 B* M: s6 U: x
- throw (new OverException());/ T# @: G& ]& {# n
- }2 x' o, L, M8 ~- q) ?
-
2 B+ v0 g$ _2 x1 C' B* d - //学生信息类( V) B) Z; p8 F9 B( }0 m% t
- public class StudentInf
# y3 O! p9 a; A) [0 k# ~9 u# A - {
, m% F6 K. j# N* u8 x - private String name;
9 o, r% V5 S$ H. t2 w4 Y7 ? - private int StuNo;$ u) w3 B% L) ?" h% F
- private String ClassNo;
) C* r' {8 n6 N$ P. } - private int Level;
; L" A' u2 d5 ?! l* d N - StudentInf(String xingming,int xuehao,String banji,int chengji)5 s, {' l# Z* d3 t1 s
- {# O+ J1 u) z, w) G3 W( B7 f
- name=xingming;
& V m: N$ L) U - StuNo=xuehao;
+ r1 K/ b# m9 ]3 J' ]8 l - ClassNo=banji;
# E$ {/ `4 Z" W3 V/ A$ m - Level=chengji;
+ i% S- p/ u& }! H7 d# T+ Q8 Q1 z - }
. }; ?6 g: z `' t5 m& \# ]0 g, ` - public int getStuNo()
( u' I5 h# c$ E6 h, W/ j - {5 h4 Z2 {3 L8 K) h
- return StuNo;5 S8 {$ i9 n( F/ y m! l8 n, S
- }4 T+ G% Y2 ~5 i. d
- public String getname()
1 \8 V+ Q4 ^1 h7 D% l - {3 b3 f; U( u- E0 R; `' B8 H: N
- return name;
( m; Y* \# a: V; v l+ b - }
( X. _" o% l5 j+ Z2 z, `6 y! W - public String getClassNo()4 u1 y( r' j9 o1 k" ~& e a! a, c- N
- {
7 J. I# a. T0 h - return ClassNo;' t% I/ r( |2 f/ r. L: g7 u
- }9 ?: S7 y4 |: M" P* f j
- public int getLevel()
5 c3 ?' S- n! U; R: i& h - {
$ d+ D% }- C, | - return Level;
- |4 y! z9 H4 o+ A: o N e - }& [1 W( V* O/ O, ~8 h! {5 t' Q
- }* }" Q/ c2 B8 a) q$ x
- o$ K( E1 x, u* S Q8 D/ J- }
复制代码 ( j* D7 Y4 b6 g; r8 u9 b1 ]8 e
, k' i3 r$ q2 X/ \ |
|