该用户从未签到
|
- import java.applet.Applet;7 }+ m& b! {( R3 v
- import java.awt.*;
8 }; c" l: C2 J - import java.awt.event.*;9 s! {2 T# E$ ?+ B* Y
- import java.util.*;
4 Q5 ` p. S5 n+ z - import java.lang.*;
6 B; M2 N+ S1 v. c8 w5 M! q - : v6 Y( q5 _6 j0 V2 I
- public class Students extends Applet implements ActionListener
! C, {6 M, [$ Z% C1 t+ d5 z: B - {
1 ^. T m% z: a, B2 F/ P. @. O - Vector StuInf=new Vector();
* v+ M6 i& A6 u% {! \1 f$ y - StudentInf SI;8 n3 U4 T. ?( I5 ]) B
- String xm;
1 S7 Q6 |! ? h8 z - String bj;
( R; V7 t+ i* b) h - int i,j,xh,cj;
- r9 V- ~% S z+ j7 @ - static int mid;. F$ Z8 K8 t( d! P% g1 G. T
- Label prompt1=new Label("学生成绩管理系统");
; F& Z9 J) Z/ n2 q - Label prompt2=new Label(" 用户:");+ L3 _ k: U. X+ W& `5 v! C
- Label prompt3=new Label(" 密码:");- I& i5 z8 E* E5 h& y/ m* M
- Label prompt4=new Label(" 班级:");% G+ e* Z* M9 `" ~% ~
- Label prompt5=new Label(" 成绩:");
1 a* k* t- h8 h5 s" j# ? - TextField input1=new TextField(8);7 Y4 o1 U6 ]% o3 O6 |
- TextField input2=new TextField(8);+ C' Y0 B3 w: u% p9 r7 A
- TextField input3=new TextField(8);
; A9 K5 S+ B% v+ I g - TextField input4=new TextField(8);
1 c- r% {5 R# I0 ~: k& u) j+ U - Button btn1=new Button("登录");
6 g5 M) J* ~7 M - Button btn2=new Button("增加");
* N. L! N6 L! D+ s) ~ - Button btn3=new Button("修改");
' J+ Y+ j5 Q, x - Button btn4=new Button("删除");3 u% |7 H6 k, E A& m; D' t
- 5 s3 y [$ i4 T
- public void init()( J8 Z( Y7 F9 P5 k
- {4 d- {8 w4 T, R F9 S
- setLayout(new GridLayout(6,3));
, B% w6 D8 H! b0 T! i# C# l - add(new Label());
# N0 q. B! m- p" H% O6 ]/ J - add(prompt1);3 r- s+ \# o. w8 D5 P3 }
- add(new Label());
0 y* }! U; l, T5 m$ e0 f1 K - add(prompt2);
1 O! T i: ?+ Z5 P$ F, c2 E& A# A - add(input1);
4 g6 ]8 B: z2 o - add(new Label());/ s! N7 ]; S0 r8 [1 m0 ^( n+ Q* @" V. d
- add(prompt3);, f+ H3 A( ^0 U" t% T7 N
- add(input2);
7 n; c/ R! g! e1 h, Z3 A/ L - add(btn1);" o# l V1 o! h
- add(prompt4);
4 f% V% A q2 v5 V7 `! T - add(input3);
( \0 N6 y3 N( |; t O - add(new Label());
c* ?$ m; h4 L( Z - add(prompt5);/ P q n9 b. H% c) s
- add(input4);- \$ H+ |' P* p( I% x
- add(new Label());
4 ~! V ?/ o- L - add(btn2);7 k5 \1 k2 L+ H; E" O! c
- add(btn3);$ S- E l4 H% f* _: H4 w4 Z
- add(btn4);7 F! ?! j* }; E5 k
- prompt4.setVisible(false);
% O8 j& v! A: K- G+ k - prompt5.setVisible(false);
" k. Y, e& o6 B7 ^4 w - input3.setVisible(false);# x. ~# |9 C: j" u5 b
- input4.setVisible(false);
4 O+ F2 a; K& J3 A - btn2.setVisible(false);
5 f: V, r4 }# g/ ^! U$ e- b5 l - btn3.setVisible(false);
' d3 L2 `; A) T: K# k- V3 l- U - btn4.setVisible(false);
! L! C- I5 k9 ^$ V( H! i5 t9 Q - btn1.addActionListener(this);; _3 y; t* O6 G; I! y' [
- btn2.addActionListener(this);, a& V/ U( V: ~, y: h# K2 R1 |
- btn3.addActionListener(this);/ `' z x* N$ C; Y1 q! r
- btn4.addActionListener(this);
8 Q$ |# f4 T* p7 L. r0 Z1 ` - }
2 s3 M% d4 t& ]2 J* E3 u' q - public void actionPerformed(ActionEvent e)% i s) h) l. q) a- _
- {" P2 g( N1 v- ~" K$ Y/ G7 }
- if(e.getActionCommand()=="登录")9 B& H& r, X/ @# p$ K- R
- {% g0 q& W' t" a" l
- String a,b;9 L+ M: h2 G/ ^, B- O2 V
- a=input1.getText();3 U) x- \! c$ G9 o+ S
- b=input2.getText();2 H0 t; E Y- y
- input1.setText("");
1 s" y. `/ C8 d, g - if((a.equals("12")==true)&&(b.equals("12")==true))! ?' Q& i# L: w( `
- {
( S3 F+ h0 D4 ^6 }. ~6 ^- R2 Z - prompt2.setText(" 姓名:");/ G/ N7 u4 O$ X/ O
- prompt3.setText(" 学号:");
5 p; u1 Q3 A1 R0 ^ - prompt4.setVisible(true);6 j$ \3 M5 F) H) L
- prompt5.setVisible(true);& M9 {2 @9 T1 a2 ~% i
- input3.setVisible(true);
, h8 y! e5 k1 ?0 ^4 d4 L - input4.setVisible(true);
+ e/ [) ? F( S! j) N/ [ - btn2.setVisible(true);
% C6 O' b' K9 s/ E: \' T; M - btn3.setVisible(true);: @* V# i- o# H# b2 t$ D- v: T
- btn4.setVisible(true);
" J9 |6 F( j/ a+ G - btn3.setEnabled(false);, U$ \. l" @+ h: l
- btn4.setEnabled(false);8 W9 ~( @. J1 ? N
- btn1.setLabel("查询");# M. w9 n7 e, x) h6 _; x
- input1.setText("登录成功");
$ w4 G0 A: Y& v - input1.selectAll();) g5 z- F: ]! [/ q' M" B
- }
% V" F7 f$ E7 c h& F - else
5 e3 I- ^, g! t( P/ a - input2.setText("用户名或密码错");
8 f. y' a* D: ^# f) d0 Z; C - }
" d R7 @& }' L+ | - if(e.getActionCommand()=="增加")
; ^0 U1 S4 F( \ - {7 E. k) W( G- ?
- boolean scucss=true;9 G6 D) v" o; {- S& ^/ N
- try
. p3 Y2 E5 Q u; D - {+ u! d7 l/ s" e, ^ l. c5 Z2 A
- XingMing();: B, R2 s) U3 l4 i2 Y; T5 K
- }: j* {+ L& p/ [& D7 w- k
- catch(EmptyException as). h( s- B) E2 G. } U: c
- {
: I& M6 s3 V+ s0 U - input1.setText("姓名不能为空");" n! v0 f+ Z- R9 J
- scucss=false;
" t# ^3 h2 `0 q% l+ Q0 X6 r/ \* e - }! B$ x5 r+ d% f, d! c
- try
3 G$ M. o! e- Q - { |. d6 h6 U4 H
- xh=Integer.parseInt(input2.getText());. {7 A/ N# m% s- A) x* ~ U9 y
- }
! ^ G7 K. e' Y( B$ a; G% Z* @ - catch(NumberFormatException m)
9 k. R+ N, n* Y' \/ P% b - {& q( I5 L, h Y$ n9 x
- input2.setText("学号为空或格式错"); L, u2 _- r: q
- scucss=false;
7 G6 z9 \( E( D" @8 L - }
: G% F, Q5 S8 {( E - bj=input3.getText();
1 {6 I8 O0 C5 j5 {( M/ ^ - try
1 p6 G8 e [' N6 E' D8 _' c - {
1 p" e9 ]: v( M) x - ChengJi();
0 J5 q/ s8 ~1 u' {1 ` - }0 c; M1 C- q' \
- catch(EmptyException as)2 C$ l |. a0 b$ i. k
- {( V1 Q {) n. z7 I
- cj=-1;
( `$ I) ^ k; ^' O - }5 S7 X* E* ?6 q8 }
- catch(OverException dd): Q7 t0 y( G5 ^/ Q& m
- {9 L- S4 ]; N6 g. D6 h0 O
- input4.setText("应在0-100间");8 S$ ~2 T8 V. K: x8 ]
- scucss=false;# B: v% B8 i% N) i! K6 c @
- }
. V9 }; e% V5 D" p+ k - catch(NumberFormatException cm)
. X6 [' _; n# U/ e5 y6 F( u: E. _ - {) s& n$ I2 Y% w* \& y" t, _
- input4.setText("成绩应为数据"); t ?) L6 T$ V* o- @
- scucss=false;
$ W7 j1 f# G0 Y: }5 A4 J, F4 r - }0 r1 J+ A$ I. c4 |; Q
- if(scucss==true)
. o7 @- X- ?! _, j2 R0 u0 u8 N. L - {
# A# g+ H9 |$ F3 P2 z) U: n7 T+ @2 n - SI=new StudentInf(xm,xh,bj,cj);
; p1 [7 A2 c3 c% a - Insert(SI);
. T _' T5 ]' u: c6 B- L% K - }
% s: u+ j- s1 W2 K" K* Y - }
* G. }$ k O" P4 L) \( k) I1 z Y6 M - if(e.getActionCommand()=="修改")
$ t' ^2 ~ P# W4 b/ \$ |4 }6 t" r; z - {$ f% x: B. Z+ n7 z1 i
- xm=input1.getText();+ |+ Z0 ^ G2 G7 A9 M! ]
- xh=Integer.parseInt(input2.getText());
* }, z+ N0 q+ F8 {. M4 u - bj=input3.getText();
& n' x% _9 ~9 P# z7 u. t6 R - cj=Integer.parseInt(input4.getText());* {$ D$ g4 q9 o( U
- SI=new StudentInf(xm,xh,bj,cj);
3 b* A* C4 q; E% ^ - StuInf.setElementAt(SI, mid);
! K! b1 V. C* p& N) F' n) W - btn3.setEnabled(false);
' W8 N" C1 q5 o- J l; E6 X% t% s - }5 Y" ]5 ?: n7 h: J: v6 R
- if(e.getActionCommand()=="删除")* v, D6 Y, L& }3 ]
- {9 a: R- I$ |* Z% i" D$ Z
- StuInf.removeElementAt(mid); 2 I3 u0 A g0 m4 H z
- btn4.setEnabled(false);/ q4 m: _) |5 [8 j( I$ f
- input1.setText("删除成功");
& L$ ]3 ?4 O* o% c - input2.setText("");
4 a. T7 [. p( O - input3.setText("");5 A9 Z, K( _+ M2 D
- input4.setText("");
. D6 O# J: H' e2 b$ [' k - }5 J7 r) q! I8 x+ d2 ?) `
- if(e.getActionCommand()=="查询")
6 b. p- E4 p+ H1 Z - {
1 a1 S6 H! A$ }: q" M/ o - boolean right=true;" E* N! N2 G& V* u1 I/ Q
- try
! C0 d, k# @, q' f# h% r# P - {
" C1 ^, ^8 I- N5 f$ I+ C - xh=Integer.parseInt(input2.getText());
. f6 w u9 v1 D( _; t9 { - }
( B$ K2 ?- a) `0 R0 H! H - catch(NumberFormatException m)
& }. m+ [/ _' c+ ?; c ]5 e! R1 { - {4 d( e% N, Z) V8 p- B9 y% C: J( z
- input2.setText("学号为空或格式错");
3 o5 @1 u e' j# W# R8 T - right=false;# ?. D1 b- {: @) @7 A% S
- } 5 M1 U2 u0 W4 g
- if(right==true)4 y0 }" I* T3 F. O$ J! V' i
- {
, l; D" t$ _5 v$ Y - search(xh);
0 T, ]( y9 J# [" B - btn3.setEnabled(true);7 R8 G% A% O& H1 o* W x% w
- btn4.setEnabled(true);
$ |7 J5 W6 ?8 j K1 v; p - }
3 X8 U6 F8 s6 `' H% e" N7 D0 \( h8 f - }$ O* p+ P4 \8 y% `2 Q/ ~; D* H/ J
- 1 {4 \2 v, V( \ k. m) S
- }1 a1 w* S; C$ i; V5 h5 Z
- //查找方法: l6 N% y% z. j) y9 _1 v
- public void search(int k). ?" | y) K* k
- {7 F( K* j0 B9 ~% x
- boolean exist=false;% ^" q. Y9 n% p$ K
- int low=0;! M/ u$ O( Z: E0 X% {- E
- int high=StuInf.size()-1;! E) X) C3 C( F) B( Z9 K
- while(low<=high)
& a5 F) a, \: Q) q! X8 r! f/ t - {5 O. H# u/ \$ c9 X* P
- mid=(high+low)/2;
6 D+ [" W! n+ ~9 x2 U - StudentInf a1=(StudentInf) StuInf.elementAt(mid);
+ U: h! g' n4 ]+ e/ h1 ]$ W - if(a1.getStuNo()==k)9 n3 M" e4 [1 ^7 B5 X4 `% L: p
- {# l' D6 j& ^! I3 X- y4 h7 V
- SI=(StudentInf) StuInf.elementAt(mid);4 B. q C! F. @) [4 C
- String x = String.valueOf(SI.getStuNo());
, l9 M' x* g; |$ Q5 r1 {& i9 C - exist=true;
. h* N" x. ~, W" D5 F: m - input1.setText(SI.getname()); X2 o w4 E, N6 Z3 }1 j
- input1.selectAll();; G( }9 x) ~ E- m
- input2.setText("0"+x);. _9 K5 M) t3 W1 e. R+ l+ _* O
- input3.setText(SI.getClassNo());" {% X8 G% |% n0 r0 ^
- if(SI.getLevel()==-1)) D- e" ^3 A) n
- input4.setText("未参加考试");
; k2 ^4 @# @* q( C - else
0 @1 q8 I4 }( R% n - {
- R/ i0 v0 h7 p3 O4 H, B/ E - String y = String.valueOf(SI.getLevel());: e; h3 m- A6 y9 B! l
- input4.setText(y);
) Z1 r8 ~. W( a0 M( p6 T - }
) j: e8 o( `' o - break;+ @6 m8 C* F% T& E( g
- }
. E) w4 \' P( ~: L0 H* G! |& K5 _, [ - else if(a1.getStuNo()<k)& g) ]2 Y' @7 J8 ]: Z7 |5 d6 a
- low=mid+1;7 O h2 r A$ H4 T% e- X" w/ Y+ L
- else
4 s! f8 b* G0 h) P; Q0 n - high=mid-1;
5 _& v! p, v ~+ ~/ [ - }1 p; [& ?* H3 j7 u r, M
- if(exist==false)
) W1 S0 ~/ A3 e5 f x - {
- x1 o8 {* [5 F9 q. z - input1.setText("无此学号学生信息");/ J d& `- M6 z
- input1.selectAll();7 E% Z1 j6 l0 i
- }6 s* N8 D5 h" m
- }
! u8 S D' {; e' d - //添加方法
: b9 F, i- u) [1 p' i - public void Insert(StudentInf q)
. W' W& ^1 h) m3 n' I - {" j! C- y4 l( u) ]- {. h
- int i=0;0 \+ E( @# o8 Z7 I
- 4 E; g5 H0 X9 L- v
- if(StuInf.isEmpty()==true) , k/ u; T' ~; F# c) n. \
- {
- l' ~' @) O0 g - StuInf.addElement(q);
3 U9 n& j- f8 x5 e7 ` - input1.setText("");( X- u3 g% W2 F& q. I# }
- input2.setText(""); x" w' H% S1 r/ s% T+ c
- input3.setText("");
2 H* o! M+ V8 w @$ i - input4.setText("");
7 K& S* @ L( i9 \8 B, f7 e/ x - }
) d" O; H( J0 D5 a, z1 Y r& \ - else
]8 Z% s, ?: j3 |/ K. @) r - {
0 M% Q' h# t7 d# j m- n# R - StudentInf xh;
0 H- n8 F! x+ o: j8 Z' W% N* [) d - xh=(StudentInf) StuInf.firstElement();
( m$ [9 S9 [5 o' N. c - while(xh.getStuNo()<q.getStuNo())
' B0 ?3 j8 _4 K8 ~: m [; {% @ - {- ?! O/ y+ g1 e% e0 \! ^1 C
- i++;$ R/ u3 b# L) @& r: s
- if(i<StuInf.size())# U( M7 ?- l9 n( B" B R" }
- xh=(StudentInf) StuInf.elementAt(i);
: @* Z4 I: _' C) V+ F - else # c" \' p; g' D
- break;6 n0 R/ |1 ^8 G5 c5 z' i6 n$ v
- } ! |2 Y- k0 _8 c0 I& Z# ]- N
- if(xh.getStuNo()==q.getStuNo())
* j: e$ g- C! } - {* P8 z0 ]4 [$ ?* ]$ U# ]
- input2.setText("此学生信息已存在");
( {/ d7 ]; }. p! N/ W& o$ h - input2.requestFocus();- W5 ^: `: C( ]! g
- input2.selectAll();0 m4 ^7 J/ x" Y+ s' x8 ] S9 {
- }; }8 e8 Y9 t5 s! j: F
- else
l5 z9 v p1 f( O! h! h - {
! o0 I8 [- K$ T6 H, \ - StuInf.insertElementAt(q,i);
( }/ J, q% ?/ B - input1.setText("");
$ h! E+ C/ c; z8 X4 ~ - input2.setText("");
0 m+ D8 b0 A) F) s/ J - input3.setText("");
5 ^: E& E$ h: `# ~ G - input4.setText("");
4 ]2 w+ Z2 v6 _ - }. [+ X# K- h. ?! j
- } 4 [& s- n$ ?! ]7 o5 v1 q
- }
$ H: I1 m; U" H3 M9 T+ e -
* I3 Z% r8 k& @, W' i% T - //异常类) X1 E4 o. l7 ~% \% M _
- class OverException extends Exception
9 @) a* k4 c& k8 D9 F - {
# E8 Q3 q Y `2 b6 Q& K, R' q3 [ - String over;
1 E. k& w' M8 ?% ?- K - } 4 n# L7 p6 p$ E3 X* V/ S6 l
- class EmptyException extends Exception
" U6 W3 U1 M& { - {
- a* f) @, v* ~% s3 G0 q4 d - String empty;
/ T9 V+ q( t) ~2 J7 K0 p" U- l) p - }8 x$ K# j0 P$ L- Q9 l. m) z1 Q
- void XingMing() throws EmptyException
" B, i, c7 c$ m& f# Y - {9 o4 c, L8 k; B& j% r+ x
- if((input1.getText()).equals(""))
4 O" `! n# V( `: F/ M* ~$ j) O - throw (new EmptyException());% K1 g3 a8 Q; @3 T0 z" J6 F0 s0 \
- else
! y% V+ ?4 \/ E0 Z2 @4 ?2 n1 Q - xm=input1.getText();5 Y+ K5 f+ R( z. w
- }
! I* h1 @; m' W) [% b% s) } - void ChengJi() throws OverException,EmptyException + f3 `. j& |3 I
- {+ q3 z# ~0 f& v, Q2 `
- if((input4.getText()).equals(""))
# N2 W- O+ }/ r7 K+ h2 ?* n - throw(new EmptyException());* |/ E0 b' P1 Q7 T+ n1 l7 y: i
- else
8 x ]' ~! P! n* n4 S - cj=Integer.parseInt(input4.getText());7 [+ B. f9 f9 u r' r" M9 K
- if(cj<0||cj>100). C) f/ B Y" u Z5 Q3 K/ Y1 I
- throw (new OverException());
4 T* i/ Q, |% Y - }
2 r& O5 r N' d' u9 [ - - w. s# z$ Z; r$ h( K
- //学生信息类6 D: @5 g9 y6 l* {6 Z% U+ x
- public class StudentInf
: m) P" \" |/ G* w; S. n% H - {
) J, ^2 O: K" ]: H( x9 e - private String name;3 J# B% J; Z: t
- private int StuNo;0 V2 Z5 k& u1 V& W
- private String ClassNo;
1 j, g# g; s2 v7 o X1 p - private int Level;9 T2 j" X* z* r4 m1 x, N
- StudentInf(String xingming,int xuehao,String banji,int chengji)! |+ G# ]( B% @8 {
- {
% H' ]- D: t# H/ V - name=xingming;
, f! m1 u+ o6 ]6 J+ N8 ?. _ - StuNo=xuehao;
0 i( Q+ Z! u/ V - ClassNo=banji;9 B3 S& d- M S& ]* [' Y0 f
- Level=chengji;+ H$ o3 Q3 J# n
- }
d2 |% ~6 l0 c( e, r( A - public int getStuNo()& A; e7 L! `; W: L& d: s3 o
- {
4 r# N3 K8 O( d* w9 S) B - return StuNo;0 {- h1 }3 R7 c0 w8 o$ }( l% m
- }6 U. M& u+ p) k1 o+ s/ P" t
- public String getname(): ]& G! L$ m7 ?, P2 C# `( `
- {9 W9 Z6 Q0 I+ Q
- return name; \; S6 ~0 \3 h. u* ?
- }
/ ^. j4 B# G, \ G" t4 r( ` - public String getClassNo()
" o7 o- v, @& P- o! @ - {0 T/ W4 C, r/ Y6 x
- return ClassNo;2 A# |* M) |; ?
- }0 y& F4 W( D1 ?
- public int getLevel()
; f/ X5 t0 }1 _! @$ p - {
2 T: S0 S5 ?4 ~; h+ E - return Level;
3 M* s2 w5 B- |7 e s - }" J$ y7 e2 i9 d- {, U9 m0 E
- }
8 e: | \8 L( o- v- @9 U% g
$ g( ~8 P0 _* S8 f" L- }
复制代码
- M* z9 M1 m5 ^) l/ ?- L9 @: u" z$ D. t! w2 z
|
|