该用户从未签到
|
- import java.applet.Applet;+ H2 C {' N' @2 o7 H7 B
- import java.awt.*;1 }1 x* g0 [; D0 t
- import java.awt.event.*;3 @2 K9 C3 a0 t- X3 v! g
- import java.util.*;
) x# R0 S% g8 j( S* t3 \ - import java.lang.*;
8 @! l2 v) y4 l& _3 y' c; z
- @$ ?4 l5 n: r- M; N! _- public class Students extends Applet implements ActionListener: p1 I2 G- ~6 W! R5 \% l5 R4 ~
- {6 @2 ?8 b3 Q8 E1 C6 E* M
- Vector StuInf=new Vector();0 K- D. E9 x/ ]' o9 m$ g
- StudentInf SI;
; L2 t s' d) k - String xm;
% P/ b i7 n+ | - String bj;, ]9 _9 p7 W- J- `# _. k
- int i,j,xh,cj;
& \/ K5 o. C/ c. Z, F2 r" I7 @6 _" Y - static int mid;$ ]9 |0 K+ \8 s V& K
- Label prompt1=new Label("学生成绩管理系统");
" u. D. ^" G8 _- F - Label prompt2=new Label(" 用户:");
+ ~9 h6 j. n, l, D5 a4 x6 f - Label prompt3=new Label(" 密码:");
+ @8 j6 t- r6 [6 u - Label prompt4=new Label(" 班级:");6 M3 e8 E2 p4 F2 m
- Label prompt5=new Label(" 成绩:");- V- a* ]. u2 x
- TextField input1=new TextField(8);
5 |3 o9 Y- R. { - TextField input2=new TextField(8);" u! j8 T* a! s$ E0 \
- TextField input3=new TextField(8);
7 Y8 L2 R# Z; b% ^ - TextField input4=new TextField(8);: \0 T% ^4 G0 ?7 ?
- Button btn1=new Button("登录");2 V% x! R# X7 W9 L7 b
- Button btn2=new Button("增加");: v. u% I0 P M
- Button btn3=new Button("修改");
$ B9 Y8 }: l5 \ - Button btn4=new Button("删除");* b9 ]/ G4 t: i6 R" f
- + ~$ i6 y0 r5 s
- public void init()
# x- p7 B q; i! @2 G - {% T9 r+ {. x9 ?/ a
- setLayout(new GridLayout(6,3));
0 t6 `5 q/ U; }) G - add(new Label());
* h/ s/ \: U( ]7 C. w0 m - add(prompt1);6 g/ H% k/ ?3 i6 Z: R1 ~
- add(new Label());
0 g7 j8 d2 p. C7 \3 O, L - add(prompt2);& S9 h. z& g& t3 b) z1 O
- add(input1);
8 h {( R; S5 M- ^! k& N - add(new Label());
`" n4 Z; f s7 d: } - add(prompt3);
* p' B, U. v0 @( E0 g - add(input2);$ D0 o) u3 f+ q
- add(btn1);
# Q* n5 e* T" u& u& @ - add(prompt4);
1 _! y- a( p3 w4 m - add(input3);
. L8 m4 t' p2 M1 r/ e/ S: Z - add(new Label());0 m3 q- n% }. T5 H4 O$ s! z1 h
- add(prompt5);
/ K! }9 E* _: F) b/ E - add(input4);
7 n$ L0 T, }4 O# ` - add(new Label());
5 h3 S; \; `/ j4 d" | O - add(btn2);# \' l S& \: p
- add(btn3);- D; s8 Q/ q4 h$ I8 e& L
- add(btn4);& a0 I% v1 P6 F: z6 _! h. b9 h' k/ F
- prompt4.setVisible(false);
/ m7 w9 l7 n% W, _$ M. x# ^# l - prompt5.setVisible(false); n" i* w! Z6 T O
- input3.setVisible(false);5 c& E4 \2 O. u/ A; C$ E
- input4.setVisible(false);* R2 _1 B7 X. C% u" Y# ?5 G
- btn2.setVisible(false);
* I% _- ~* K/ o/ [3 ]: W - btn3.setVisible(false);- u5 d' P' O' }! c# O" z
- btn4.setVisible(false);$ a* l4 w j! K1 G: K# |
- btn1.addActionListener(this);
8 b ?; }# f8 N3 V+ ` - btn2.addActionListener(this);& l* c9 h% J! ~- h4 W8 O4 C
- btn3.addActionListener(this);
& I2 X7 \( p, s- D2 g - btn4.addActionListener(this);: M8 b7 J/ X4 |5 z/ ~3 R+ ^
- }: j0 z* @8 e8 z" {
- public void actionPerformed(ActionEvent e)
7 v' ^7 a$ R% P& h( r - {
/ J/ n3 ]5 z6 }5 P3 L4 N! }* g2 x8 F - if(e.getActionCommand()=="登录")' L, G# `1 e6 ~) Z1 H8 V- N
- {4 F+ z4 ~' {8 Y: J, S3 F. u* c( }
- String a,b;' S+ ]' z3 G# F
- a=input1.getText();. b, j$ c, t& S: I/ n! u
- b=input2.getText();" [0 p& R1 a' N, l: e
- input1.setText("");
2 P5 H, U, ]; D - if((a.equals("12")==true)&&(b.equals("12")==true)); M) T5 E6 p6 k1 \
- {& O+ C# o& Q7 {1 _
- prompt2.setText(" 姓名:");6 \& |' ]/ v( H. i2 ~
- prompt3.setText(" 学号:");
- D& |" F) U; M3 H. j" @' _3 j - prompt4.setVisible(true);
5 e9 b7 {9 r0 L3 Y# u9 r - prompt5.setVisible(true);
+ w) @' q* D. n4 w* i" K - input3.setVisible(true);2 m1 H, X% n( C0 J
- input4.setVisible(true);4 y: J; p, H- B# R8 y: c% F6 m
- btn2.setVisible(true);3 B& e! _# F" ?; k/ A# m
- btn3.setVisible(true); Z2 @/ d6 \' Q; t) c8 d
- btn4.setVisible(true);1 V+ l- I5 w+ o0 O1 J; T
- btn3.setEnabled(false);
K# O! j6 U. B; d# \: v% X - btn4.setEnabled(false);
( [& f6 }1 d. d' n - btn1.setLabel("查询");
( ~% O1 o6 I2 F - input1.setText("登录成功");
+ G$ l7 Q& D, _! ^# h - input1.selectAll();
2 B6 i3 Z' ?, ^4 ~, a - }/ e. s: O$ X1 d4 b& e. v
- else
: R& \" l' H; U( K/ j* A1 m) R - input2.setText("用户名或密码错");
3 e a; ?5 H7 i" F/ \$ Z' r - }& ?6 j1 g. l4 O7 N- _) Z: d3 ~- H
- if(e.getActionCommand()=="增加")
" Q6 g, L* I) Z7 e. E" Y1 ]9 G' ^ - {
+ H1 m# a' V: t2 {- |7 e+ S - boolean scucss=true;
: J- [0 ]! k, v5 u - try
4 n% i6 N4 {' i2 S" v$ y/ s - {
3 r5 \& K: W% R' h6 ?: B - XingMing();
0 Y+ F- j" P% g+ S9 w0 A - }
9 V9 i6 O6 U# R2 B) p" V. r4 U - catch(EmptyException as) a% v% c8 R" Z) u6 @8 \: U
- {' u; o4 j( Y& O) h# y
- input1.setText("姓名不能为空");
: p2 S) d1 t; j/ C9 @ - scucss=false;. I. E( l" J* R3 g5 X; |( d
- }6 b0 p3 j" e0 S
- try
5 Q: l) S9 g; O2 r; V% \# X - {' ]7 X# R" J9 W9 r/ g' i
- xh=Integer.parseInt(input2.getText());7 w# o, F* |1 v5 |7 p$ ^. Y
- }
2 D& y8 B% _( v - catch(NumberFormatException m)
1 I( g$ u# o- b1 c: b: i# f( D - {$ g9 G2 x Q6 m* ~; T2 o1 W
- input2.setText("学号为空或格式错");% {- q& H6 A4 ?) n
- scucss=false;% Z, Q! y0 D& H: ?. q% y
- } 0 e' d" T o/ } { D4 |
- bj=input3.getText();- t; x& _5 Y! N! M8 q9 j4 q0 O
- try) H: O( ^. m1 m, y" A8 f
- {
) c0 ~& s% V" c - ChengJi();
: e m k5 H0 e - }& ]" D5 |7 T5 [/ s( j& i, W
- catch(EmptyException as)2 D/ L* L+ r* ]. t) N
- {5 x2 O8 k( [( a. a# j
- cj=-1;
G' ^7 f: L) B g4 D& B - }
# G' E/ X& |: v% b! G0 F# J - catch(OverException dd)" g( g( R v0 o! _4 F$ K
- {
: D6 d' Q- F0 A$ T( E- u- c - input4.setText("应在0-100间");, E+ B4 y$ G; F$ A4 b! H0 @0 f0 h* H: o
- scucss=false;: ~# o8 C8 ^4 |9 n
- }
* \# m' A8 Z% d4 e: m; \& U; S: V - catch(NumberFormatException cm)
0 q P+ \) O' Z: O+ h, O% A" J - {* j9 j; _9 }( M& i- P$ Z# I* ^
- input4.setText("成绩应为数据");
2 ~; b/ V D# U* |% x# G - scucss=false;
7 l" J4 B6 Y1 \+ E7 s - }: | q0 ^$ m* Q* y; [( R! y! R
- if(scucss==true)- J0 e2 {# R/ n3 Q9 T2 d
- {$ i2 j7 \3 D* B
- SI=new StudentInf(xm,xh,bj,cj);
5 X: B/ m) K( M. {7 q - Insert(SI);. ?& D9 A5 Y5 l3 ~* p, `# r9 _9 c
- }% ?% J* L. g6 v
- }
4 `& V7 Z! r# g7 j) z - if(e.getActionCommand()=="修改"), c+ |; l3 b6 g* N. U: t0 [
- {, V/ v( z. g; X
- xm=input1.getText();
: d) \ F- L. x5 v - xh=Integer.parseInt(input2.getText());
6 M, g; h2 {/ M2 u9 T- [# ~ - bj=input3.getText();8 V* u2 u/ O, i
- cj=Integer.parseInt(input4.getText());2 m# ]# G1 D2 x! A3 Y a" y
- SI=new StudentInf(xm,xh,bj,cj);/ Z, _, h' M6 q3 y2 m" L
- StuInf.setElementAt(SI, mid);/ M1 y z% e+ I) g
- btn3.setEnabled(false);
2 M/ ~% n4 t' \) o, ~. p+ ^# ]+ J - }$ F. a2 \# u, N- Q
- if(e.getActionCommand()=="删除")
/ j0 g* x* I0 s; L7 j3 @ - {; x. q) Q& [9 M% @% c& v
- StuInf.removeElementAt(mid); & T7 [% E' }& g8 N% D( Y& Z
- btn4.setEnabled(false);; \/ X. i4 M' n+ W3 a0 e! D
- input1.setText("删除成功");
" M0 a( @5 Z/ j - input2.setText("");6 M! b5 W6 Y, g/ q) G G; p1 m t( g
- input3.setText("");6 X0 D; d, X) I/ k7 C5 |% H+ B
- input4.setText("");# Q8 O) j) o- v
- }! ]- W7 V. \1 \: x G* Z
- if(e.getActionCommand()=="查询")
2 @8 q! o; o, u$ L! h - {; I3 y$ q" D6 H( \
- boolean right=true;% @8 c/ E4 B$ ~( M' H" u
- try8 k* j9 v$ w/ c
- {
; e& q; Z) V, \/ S1 ? - xh=Integer.parseInt(input2.getText());
5 M- b$ p. x$ A+ F' K" V! ?; E- f - }. t4 W0 f6 L! Y4 K* Z
- catch(NumberFormatException m)* x5 ~& R9 z! J$ l
- {
! Z1 j1 h% g0 D2 e, E1 | - input2.setText("学号为空或格式错");1 s$ ? V( u" n
- right=false;
- x7 d- e0 f( Y; j- R' m - } / Q& c/ H3 N+ z8 {. g* V! T
- if(right==true)
% E: t/ X# O9 m7 ?0 ~ - {
Z+ r* P; R& r2 V1 C - search(xh);0 a5 d7 J- c/ q
- btn3.setEnabled(true);! x* K9 _# f" r* m% ~" r/ i8 F
- btn4.setEnabled(true);2 P8 Q. X/ l$ j
- } X% q0 {* G0 \8 t) s
- }
/ x9 [2 \. L8 H6 b, q n/ X -
& I# b2 ~1 a0 R. A% J - }" X3 ]+ O* d3 Q- o6 N7 r9 v5 }. g
- //查找方法! S8 ^2 d0 k7 J
- public void search(int k)2 ^/ k* V7 `# S
- {' P0 `9 N5 V6 U/ {" X3 i) F- z
- boolean exist=false;
4 v5 ]% V3 e( X& j+ v( r# r - int low=0;- g" `0 B* z8 g: f4 A) W, S
- int high=StuInf.size()-1;- b# c6 [# Z: L7 ]- n
- while(low<=high)9 Z7 H# X7 X+ g
- {# ]6 k) s+ h+ T$ h+ d- B
- mid=(high+low)/2;
" @, i- e. F5 ?$ Z; b. C7 \ - StudentInf a1=(StudentInf) StuInf.elementAt(mid);1 @% W; Q! W6 D3 ^
- if(a1.getStuNo()==k)7 D# o8 ^! D8 X1 ]8 L; f
- {& ?( M5 N& y$ _5 y$ Z d% a
- SI=(StudentInf) StuInf.elementAt(mid);
0 Z, }0 }; q% } l! j2 c - String x = String.valueOf(SI.getStuNo());
+ ~! W" X9 ~1 C( |" R - exist=true;
# q8 ?: z$ `4 k: Z - input1.setText(SI.getname());( C' I& C2 e3 q5 W# W% T' I
- input1.selectAll();
N: ? j# W( @4 U5 {/ I4 V) H) Q - input2.setText("0"+x);
) o2 h/ p$ T' T6 Y3 v: t$ Q# e - input3.setText(SI.getClassNo());+ s2 }4 G; ?2 ~8 A6 t
- if(SI.getLevel()==-1)" N# T9 a/ `8 Q9 s! R. _1 [7 u
- input4.setText("未参加考试");
+ V& L# }$ Y( x } Q* s4 S - else, K) w; A3 j2 \* ]
- {. a. l3 F1 N# d! n( ] w# f. ~
- String y = String.valueOf(SI.getLevel());/ r7 b) Q. s6 F& T" {
- input4.setText(y);# s; H, E+ Z" C
- }
2 s/ o, h' f% d7 `" w - break;
& m8 C b; w) [ - }$ ~- U+ X" Z" L5 x
- else if(a1.getStuNo()<k)
% m- p# }: A& m$ a$ o4 j - low=mid+1;1 o4 Y; D, J5 o. c+ d. U5 d3 I
- else, Z: J6 i/ h2 d. S9 O- F9 t% @
- high=mid-1; - X9 ^3 h; r# X
- }
7 p: x5 J) r0 Y& Z# Q6 n' C - if(exist==false)
& G9 C( Q) l1 _/ d! ]% ~ - {( D3 f- x8 [8 e
- input1.setText("无此学号学生信息");
4 l& R \' C: Y, f& H Q - input1.selectAll();( G7 x: ~1 [$ k, I4 T* U* e) w
- }
3 W$ D5 h2 H1 m! w+ e9 s - }
* X: w/ @& S6 O+ N7 G. Z6 S3 c1 U0 ^ - //添加方法9 o6 |% x. I0 c! F+ h& z8 e
- public void Insert(StudentInf q), v c( n6 Q$ u/ d
- {
5 k9 X' I: J1 R& k - int i=0;
& K* Y. `+ T0 g$ l% b - / J3 v9 U0 ]1 |+ ^% V
- if(StuInf.isEmpty()==true)
]; J0 v: ^8 |" g' X1 j - {+ Z: o: _$ v5 M- t: [
- StuInf.addElement(q);
1 f& ^2 B7 E" @ - input1.setText("");4 c; B! Y- E# j% s% w" J, h
- input2.setText("");
# j5 l; K0 U1 r* M5 j) O - input3.setText("");
% z0 N- h" c6 x3 b - input4.setText("");8 s% c9 H6 T9 q2 |
- } Y+ P, ~& Q4 @* ]5 T) }- Y
- else( P: L9 b) l3 O* ?. @
- {6 O+ ^% s) y9 Q2 l6 P9 t" ^
- StudentInf xh;
( |. N+ d( a2 u - xh=(StudentInf) StuInf.firstElement();
' T: E1 E2 b& d& A - while(xh.getStuNo()<q.getStuNo())
?( x# [) k2 `% ` K - {' u# V" k6 G4 _9 B+ o7 \0 V) D; a8 W2 S
- i++;' u( s+ D$ x# m1 ?# Z3 W% b
- if(i<StuInf.size()), Z s' s5 y$ y- A' g" h% A- {
- xh=(StudentInf) StuInf.elementAt(i);
+ t/ B% k: i/ }/ ?- i- ~2 v$ z - else
9 \4 W4 J" h' v/ R: `4 R5 v2 u: | - break;3 A1 Q# ~9 n; t J& }5 u
- } : {: h- u# y# q
- if(xh.getStuNo()==q.getStuNo())
. ?( j' a6 d4 n* [ - {! X2 h4 r# B$ G/ _7 c+ D
- input2.setText("此学生信息已存在");4 t$ F- s9 Y/ Q: _/ }" l2 S
- input2.requestFocus();' N4 |8 L: a8 x4 [- I4 ^
- input2.selectAll();/ H2 b! x' W' p6 Z9 k+ g: a3 @" ~
- }, \8 c5 t; U. P+ O3 c2 n
- else
5 ?2 h& {- ~/ R7 i% j9 _5 @$ y - {
$ B% S) l- `3 Y% h$ G4 _/ p+ H2 h - StuInf.insertElementAt(q,i);
# {. V2 a" D9 u" X. r. ] - input1.setText("");
/ a9 f, ?' @# }; U - input2.setText("");) i! {+ {, J& W+ |5 l
- input3.setText("");) Q7 k2 L8 d' V9 \$ Q+ V+ |6 Q
- input4.setText("");
3 \2 T) t- B/ \2 Z8 H - }
! a) O7 m( f y - }
8 ~5 y/ O0 `) ~4 i - }
# v0 {5 z% Z" x5 o) i3 x" V/ }, { -
; x F, P9 Z0 H, | - //异常类
. M: S1 \; G- U0 }- X* f - class OverException extends Exception/ F7 @3 h! b1 \4 `) I, H1 y. R
- {
, a( @' u$ s7 f) N- U# z) _ - String over;( O( B( {1 `5 f1 v. ]9 i' R
- } 8 O$ n2 A( ^! |( N7 w
- class EmptyException extends Exception; M& i w7 Z. }
- {7 x! A# C/ o) Y4 ?/ [6 {: _, S
- String empty;( p# V( d5 m3 q
- }# [/ i% A% x- ` R( _3 S
- void XingMing() throws EmptyException
N; E, [2 G M1 X; b( Q) A! w - {
! w1 t3 \! X/ k - if((input1.getText()).equals("")); F0 z' w6 p6 ^7 c6 R
- throw (new EmptyException());
! I+ ]1 X$ x$ _) U3 u% ^ - else# g1 _7 }6 X" v8 O6 ~0 p& j* M
- xm=input1.getText();8 ^* e# ~. M6 }! B# w; U
- }' b5 H" i r4 `0 x% S! A" d
- void ChengJi() throws OverException,EmptyException , X ^2 P' B) B" S, Y( x
- {! u5 u* A! g7 Y
- if((input4.getText()).equals(""))) W$ P/ Q4 p) h' v
- throw(new EmptyException());+ e% b9 g6 W! _: N# p1 E. A5 K
- else
) r; A Z' w6 i% W z% w) M - cj=Integer.parseInt(input4.getText()); u8 l5 i2 ^ ?
- if(cj<0||cj>100)2 N7 |9 A" K7 n. q% m/ q1 G
- throw (new OverException());
4 ^) ? K! y2 S' v6 d - }- {3 C3 h/ L, E) r9 e- Y5 V% I: E
-
/ G# t) S' {& K - //学生信息类/ K. q+ X4 V- t1 ~0 _8 h( S% C' G
- public class StudentInf* _. l5 F! Z8 E& H9 D" J
- {( n( m$ ?) I) [7 O3 r$ I
- private String name;
! F. {" X6 ~- n$ Y7 U! K0 |" n" S9 D - private int StuNo;
9 ^8 V: @+ x& j+ Z - private String ClassNo;
& p* A! U0 s# x7 J! Z5 Y7 M% A9 o - private int Level;3 G0 A' n8 ]# a7 ?5 G5 N+ ~# A
- StudentInf(String xingming,int xuehao,String banji,int chengji)$ S! F" y, I( A3 _" r: V
- {
9 ^4 g6 v* q6 z6 Z - name=xingming;
. m' ^2 Y+ y- [, s& m( t, c( l( v f( } - StuNo=xuehao;
0 }2 B5 e8 [) d" Z9 K+ W- ^# e6 y - ClassNo=banji;7 K3 O/ j7 ]" X5 [; k. V$ b
- Level=chengji;
) Z5 l! @4 n a3 P: H - }" N$ M5 E; m, o% w& x7 S
- public int getStuNo(). o, C- q) q+ P$ W2 {4 ]
- {( a$ C, F; g7 C9 `6 C1 |1 I
- return StuNo;
4 q+ Q1 O- ?; Z& H. Q5 d: s8 f - }
# K$ N% w. P+ u- V - public String getname()
" l! a" V, p# T; |: u" H/ z7 A' s1 A - {
0 b; c, Z z/ w: {* t3 [9 v, _# r - return name;
! D+ d- Z$ P8 M) l, } - }2 i/ C# Z+ d, Z2 {/ J; I
- public String getClassNo()* e4 y2 _' Z9 K
- {' ?6 k% |: O$ w. D4 V" n( f4 k
- return ClassNo; i+ P9 b6 C: {6 }6 g3 r9 v
- }
) [2 v* \8 J Q2 w+ S - public int getLevel()0 Z" C m/ I4 Z; f1 c8 Z
- {
4 [' U0 G, J( ^; W - return Level;5 Y6 A. \5 k% B- S/ T+ X5 k
- } L$ `3 o0 ]% ^
- }/ n2 G [- E) j: _1 r
- 4 V6 D2 a1 X& f( [& C
- }
复制代码
0 W2 Z2 y3 n# e. n
/ H! y( G. `' D' g2 k) I |
|