该用户从未签到
|
- import java.applet.Applet;5 s. C5 y/ y* { d4 p
- import java.awt.*;
r5 D! {# d+ J" [9 \5 ?: b1 Z- m - import java.awt.event.*; F9 U$ e* m+ q! V& }) w8 a* S& G
- import java.util.*;
8 N: O3 K2 X, F; L - import java.lang.*;3 @6 c' `& S5 I( d- n
. r$ Y: E. k* _( A. V/ h- public class Students extends Applet implements ActionListener/ A- b7 } l1 }
- {
2 t% v; ~7 p0 G - Vector StuInf=new Vector();
K9 I% R3 e) H - StudentInf SI;$ L0 o- X+ H I. k p
- String xm;7 A( J; [, F) y& n9 d
- String bj;. P# Z; f A; P4 x
- int i,j,xh,cj;' Q$ j8 i* o9 R, _$ o& b
- static int mid;/ X$ [/ J: S0 b0 u4 V* G: N
- Label prompt1=new Label("学生成绩管理系统");5 x* ~8 r% f; P3 Y: |0 }0 _
- Label prompt2=new Label(" 用户:");( x1 L) t. M/ X: H/ U
- Label prompt3=new Label(" 密码:");
3 t, F! b3 G8 Z6 a- S - Label prompt4=new Label(" 班级:");
6 K' W7 V9 G/ E8 o, Y( I- V3 Q - Label prompt5=new Label(" 成绩:");
7 C) }" h5 \; J2 P9 R - TextField input1=new TextField(8);
7 {+ {# {5 a6 h7 ~) o2 U - TextField input2=new TextField(8);- S' `6 e3 p5 h) U8 ]
- TextField input3=new TextField(8);, ~( z8 e- ]- v5 m) l" L
- TextField input4=new TextField(8);
& H" R: G4 X1 R. x4 n( j' G - Button btn1=new Button("登录");5 Y5 K, M1 d. y3 T( @' y3 O d/ C
- Button btn2=new Button("增加");
/ A: O) y1 w* [/ ~ - Button btn3=new Button("修改");
( C0 Z7 ~2 l- x( X - Button btn4=new Button("删除");
4 p% _+ U1 d- S0 l4 [3 ~7 v2 M -
+ Z' ]4 u* v3 B' _ - public void init()+ v7 M6 @, [& t! ^/ i
- {
4 P; A0 o1 s }5 O M, K# f - setLayout(new GridLayout(6,3));) w0 R c* n+ X8 a+ ?/ B3 m9 f
- add(new Label());
( x7 K# Z$ w$ i/ m# L1 r1 s* A; ~ - add(prompt1);
& U3 W1 Q7 [% p- D H/ F - add(new Label());
1 s8 E- f/ y1 D* w8 s6 b; u7 I- l - add(prompt2);* _, S( r& A0 h }1 X
- add(input1);6 `8 `& H( B) u
- add(new Label());
. ~; [ C+ S) U$ P - add(prompt3);
4 p; H9 e$ D( g - add(input2);2 f$ }6 n! ]' i
- add(btn1);" F# `6 n' K! n! X8 _3 G6 X
- add(prompt4);; T4 u; ^9 W3 p4 `4 k/ ]* c( o8 A
- add(input3); J5 u; g+ b3 f( L! ^
- add(new Label());5 R! E, U$ Y# ?( T2 G
- add(prompt5);
) {: d# N8 O, Y& I4 G3 ]2 s! r' | - add(input4);
2 G1 p4 l/ F7 U( ]6 A- T! C - add(new Label());
0 V4 R0 b# _* z5 i/ A: R - add(btn2);* ^- s: `, S# W3 f2 t# d: J' B8 m
- add(btn3);4 w. d: ?4 }2 T5 z& ]
- add(btn4); |1 W+ r, Y, ]/ m( i, G
- prompt4.setVisible(false);
9 ^4 f# F w2 F2 X - prompt5.setVisible(false);
/ K1 E7 ?# P! [: O6 K" Y8 P6 S - input3.setVisible(false);
9 ?& r# S' ^! l0 h0 W - input4.setVisible(false);
$ p% _) P- D0 L6 z - btn2.setVisible(false);
8 n+ t7 |0 C9 R8 b) |/ G+ @1 o - btn3.setVisible(false);
+ U8 T* s k/ |6 m3 I) Y - btn4.setVisible(false);
5 F4 I' ~- I6 ?2 \# d0 m. N8 r9 F - btn1.addActionListener(this);/ c+ k) n D9 K. `! {1 d4 l& Z
- btn2.addActionListener(this);; [' a) e% @4 _- r, X4 H6 u
- btn3.addActionListener(this);2 h6 V: o9 d! i
- btn4.addActionListener(this);2 O/ z7 d; w1 M0 [
- }- Z& P/ [8 Q6 ]/ d* J
- public void actionPerformed(ActionEvent e)' z& [! F0 @ v
- {
9 Z: e* _" O+ b; ~$ G% x, B3 @ - if(e.getActionCommand()=="登录")
M& f. ?1 m' f# p# o- I - {
5 X& ?! ^5 h6 e m1 s - String a,b;
( F6 v& S5 I& L; l' E. U( b, _ - a=input1.getText();
7 ] ]# S$ T! z- p! V" e8 i* K. Z - b=input2.getText();
5 [6 g' N" h. g3 i m2 ]# r8 Z/ Y5 b; @ - input1.setText("");
k1 b3 E6 x! h$ z - if((a.equals("12")==true)&&(b.equals("12")==true))4 f3 V$ t- j+ F% j! ]8 M/ Z
- {& A" k" D+ y6 N- l* e
- prompt2.setText(" 姓名:");
' c9 E1 Q G, h/ l1 f - prompt3.setText(" 学号:"); ( p- }1 `% [! L: r, z- k. H
- prompt4.setVisible(true);9 K5 a, {8 n- q- S F
- prompt5.setVisible(true);
( K' l1 J" K4 \6 o' ~7 I) t - input3.setVisible(true);
4 q$ R% L: g- g) @( ^ - input4.setVisible(true);
' N! o$ _4 ` i7 O - btn2.setVisible(true);8 R: P: S, @& V
- btn3.setVisible(true);8 E8 a; R- c. \! I: _! u# H" q
- btn4.setVisible(true);
+ N+ G1 D. x1 @/ m7 P - btn3.setEnabled(false);
! k0 V7 h8 D6 K7 x9 b$ s7 E& \9 F" U1 a - btn4.setEnabled(false);
; U8 k. w) C8 ^ - btn1.setLabel("查询");
_1 F* g$ O r) F' Q - input1.setText("登录成功");. \$ t4 s0 P3 X+ F
- input1.selectAll();4 r/ _1 l- W4 N8 C
- }/ w2 h h" F+ ?6 E: ^7 y- W. T
- else# \1 m/ \# X: h$ _1 p" p; U
- input2.setText("用户名或密码错");
0 z# u' ^! J3 f& z2 F/ J - }
* |1 M- ?* B4 M; C/ v7 X: E, y. I - if(e.getActionCommand()=="增加")
7 q2 N3 k: w$ ]$ q - {
' W* l7 U, @/ q - boolean scucss=true;/ X- @, K: f. J% _
- try' J- }+ t+ m4 h! g+ K
- {
E- ]+ g% g# j6 u. o3 _5 v - XingMing();
6 ~/ o; W, N3 G4 n+ Y: x8 m - }2 w1 Q# B7 z1 ~9 i7 r5 Q# L! H
- catch(EmptyException as)
) g& n* L/ i- P+ I' j4 n: k' o5 M - {
. H# f- J4 z) H" o5 R6 C+ m - input1.setText("姓名不能为空");
; @# m+ {* l: K! y+ i/ P, v& S9 J$ R3 L - scucss=false;
4 y8 `8 t! ]6 \: p& ]! ^- M0 _ - }9 N7 Q$ D3 S- ?: }
- try# R1 T, v" G1 m! f
- {+ T! Y1 u' N; Y- p3 z5 E3 v) ^; B; h" }
- xh=Integer.parseInt(input2.getText());, E. n- V5 p% j g
- }
. _+ m0 [ ?) f - catch(NumberFormatException m)0 x( Z( H; a5 |" s9 T: s
- {
/ h* F- D& O( B0 `6 l( ~ - input2.setText("学号为空或格式错");4 W# ~& K* k( }- k) W
- scucss=false;
8 x2 A) K4 V5 f9 B7 D1 p8 m - } . i% \, o5 Z* p. n! Y+ u- s
- bj=input3.getText();
0 P! |6 {0 F+ d6 s6 ?: v0 q: t8 W - try5 b) k' f$ Y- F5 M9 @6 x* z
- {/ ?2 `9 \" ^" P. Q1 g# _
- ChengJi();
# X- Q& D5 G$ S/ o - }
, u: |7 _+ r( T% V* i* \ - catch(EmptyException as)! t9 @* k& s' A8 {4 N8 ?
- {8 R9 w8 ^9 A1 U, R
- cj=-1;
/ T; R8 |3 w7 J7 a1 B5 I - }
0 h& @& e) @* h/ f- M( `' n. t - catch(OverException dd)
: |; x, L- e* I7 e8 Y) F' ~ - {
; p8 ], A4 D5 }2 u9 B - input4.setText("应在0-100间");( H3 z, t- ?7 F
- scucss=false;
2 p) c$ N5 @6 G8 r/ e8 Y6 d% L" I7 K - }/ w+ h* k) R4 K4 [. K( Z l
- catch(NumberFormatException cm)
' w$ O- c8 U5 a& d' R9 J - {! z: w) ~! B8 F+ X2 \; c9 T. X/ H
- input4.setText("成绩应为数据");
# \1 ~/ s6 u% b - scucss=false;
) x8 b) Y ^$ ?+ ]+ I" k - }
1 P$ @ m; q& H9 p - if(scucss==true): G( Z0 m7 F& r; D' }. e2 Z! D( Z
- {
$ L. P/ G0 q# ^* w W - SI=new StudentInf(xm,xh,bj,cj);
# `7 e! m: m( J - Insert(SI);, m. z* Z3 ^" k8 ]
- }
' _: ~1 f% u# `5 V5 B$ I0 ^; m- Z# ] - }
4 K" B$ _ a: C: [. u - if(e.getActionCommand()=="修改")
% C+ E/ @ x# f) p2 o - {( e8 E4 A }* D$ }$ T) Q/ L, i- ?
- xm=input1.getText(); o: N0 V- q8 Q- P/ c
- xh=Integer.parseInt(input2.getText());
4 h+ U) f: x* C4 P: M5 ? - bj=input3.getText();/ l( H9 l4 v# x- B/ o* ~
- cj=Integer.parseInt(input4.getText());4 E! y: e) ], [4 |7 v& v, t, G' t: o
- SI=new StudentInf(xm,xh,bj,cj);
! b) Q# s# O# c9 s$ ?3 p& `2 X - StuInf.setElementAt(SI, mid);
% w' e' w% R1 |3 v0 H; c+ O - btn3.setEnabled(false);6 D2 P. q @& }' a- {; Z! q
- }
5 b8 f! q$ N* Y' }* Y# F3 O - if(e.getActionCommand()=="删除")
' H# ~; y4 w4 d, H' C! I - {
! \* c* u3 N+ u& H! r5 s K - StuInf.removeElementAt(mid); ; x" _# i. }: ?; t. r U
- btn4.setEnabled(false);
! W2 }: U, j/ W# @1 }$ f - input1.setText("删除成功");& `- y- U* k. h% L+ x+ A: y
- input2.setText("");
9 C) V+ I9 H* R) m9 p- { - input3.setText("");. [5 U+ g2 x* K' h1 J& T% K4 N
- input4.setText("");+ V& d9 o" O" E+ S
- }6 A9 b; E# {* }" F7 G5 v6 I P1 |" f
- if(e.getActionCommand()=="查询")$ e, V9 z4 {, `, p
- {
! f4 I3 l4 a) ?9 _9 {+ o6 |/ u, F - boolean right=true;/ @: w+ ^0 u' ~% k9 g3 F3 X
- try. l* S( s" d; H5 w, I( \9 N+ Y
- {7 D+ E2 r5 H7 T, {" A
- xh=Integer.parseInt(input2.getText());
% f4 z$ w7 Z" b+ ] - }' B2 W6 o0 _6 p; E
- catch(NumberFormatException m)
: x6 q" B3 P& b2 y; `$ P8 z - {
1 }: i0 J! a* m - input2.setText("学号为空或格式错");& R5 K1 K" p9 t- C1 n, Y
- right=false;
6 c8 _0 g. g. |# _- @1 B, _3 M - } & k6 l& [& `5 h8 S/ \- D
- if(right==true)1 S1 J+ s' `/ o; Y
- {
$ R: X1 J" a9 @ - search(xh);
! @/ l0 @4 p3 c - btn3.setEnabled(true);- D: }: a# z" j. h. D- ]# C
- btn4.setEnabled(true);( t/ P0 n, \& \
- }& M/ A) u! W9 E' N& e
- } l5 W7 M/ X% C: k; @
- + q0 @- J2 n+ [. R3 J7 y
- }
$ I/ i$ t7 K2 F: L6 D) U7 R& @ - //查找方法) y3 w& I: H: U3 o8 a9 A
- public void search(int k): H% D* C, ]$ n9 l/ d
- {5 `8 Q8 E2 p2 p& }0 v
- boolean exist=false;' c' E# y$ V8 E
- int low=0;- v4 v( L* H8 Z2 H$ c* n" C
- int high=StuInf.size()-1;
4 C0 o! B2 ]9 I- e4 i( i e - while(low<=high)0 {% q) t% N9 h1 x
- {+ c/ ~; O7 v. k
- mid=(high+low)/2;
1 c8 _. I# o" f3 E7 d* D - StudentInf a1=(StudentInf) StuInf.elementAt(mid);
/ h4 v, M4 c) y) B1 p4 F; ] - if(a1.getStuNo()==k)- A" u: V" t3 M8 y/ K/ H1 {
- {; F- Y5 _( c6 Y& ?
- SI=(StudentInf) StuInf.elementAt(mid);# }: k6 U7 T( z2 [7 T
- String x = String.valueOf(SI.getStuNo());
* k% A8 D; z. t `8 Q6 p7 j; P - exist=true;
7 V# s& I8 H7 d& t2 @2 ^ - input1.setText(SI.getname());6 |. [ B& D$ c% P! H
- input1.selectAll();
0 f4 ^) T# Y( z) H m& { - input2.setText("0"+x);2 t& ?, T7 c. g2 {
- input3.setText(SI.getClassNo());
3 t7 u, e6 X% r3 P7 k% k- d$ c" I - if(SI.getLevel()==-1); I' w V( O! `/ ~6 M. p) b9 j
- input4.setText("未参加考试");% m- M, F+ j6 d, {% B3 ~5 i
- else
' e" D4 N+ ?" W, B( ?4 E. C. h. P - {' L% a M5 U( a& z* g8 F
- String y = String.valueOf(SI.getLevel());: I9 v4 D9 }# M& p/ s
- input4.setText(y);
6 ?* y" _$ n4 q2 A* @ - }% U% F1 {7 z1 G: ?' U9 v, O }# m
- break;
# B/ D; v. A" h1 o! E6 {9 I - }5 E8 l# g" A1 k# | ~ f8 C
- else if(a1.getStuNo()<k)
q. m2 t7 l+ W3 c7 j0 V& ?3 X: Z - low=mid+1;( Y$ g j; r. n$ M0 Z1 F! {
- else
/ X8 ?3 P$ i7 S, X; W - high=mid-1; 7 J9 a+ z% h# K8 F* H& Y8 W+ ^: e
- }
* Z9 x9 f$ ~( V) T7 X - if(exist==false)7 c" r3 m% Z# c) H+ q
- {
5 g/ Q( l& {3 e2 k U+ K - input1.setText("无此学号学生信息");' c1 S; o% C/ a) ?, C* G' r
- input1.selectAll();9 `1 u' I$ R) g E# d' @# _6 ^
- }: r) p; u9 ~$ k, J
- }
2 Q% l! [9 T% l$ Z; H/ e - //添加方法% d+ {) F& y3 H$ W# o
- public void Insert(StudentInf q)! P5 ]" w; ~: n0 B: v/ H0 g
- {
; o, s5 h8 `3 Y3 X - int i=0;$ X! H# v/ V6 ?9 O; `, `: w; ~
-
' }1 L0 i$ r o n" u! C. i. C - if(StuInf.isEmpty()==true)
" e+ X4 ^: A' d v; D - {
/ t9 ?+ D- j+ z* o( R& b- D3 N, X - StuInf.addElement(q);
- P" }, M6 P4 e& `" g8 _6 i6 U - input1.setText("");- I$ U. K6 U! R/ C
- input2.setText("");
+ X `/ O4 h, U _ - input3.setText("");2 p& p+ r% e( o, m
- input4.setText("");/ a4 A3 e$ b' s; e
- }
3 ]7 [' N/ m0 u1 O+ |5 ? - else
0 l: Q: R7 \: j* h; ~. H - {2 v. v/ Q5 H& s! p, A7 a' S
- StudentInf xh;
- D. O; W& P3 C' e! \& K - xh=(StudentInf) StuInf.firstElement();
3 u: ]8 Y. C, R9 h# H - while(xh.getStuNo()<q.getStuNo())7 q$ _( V7 s6 O! d" |! D
- {: v' `- P# r% O/ R0 v
- i++;
1 f9 q8 q, K4 I4 O - if(i<StuInf.size())* f7 Z5 c) j9 I
- xh=(StudentInf) StuInf.elementAt(i);' I4 f! d' o. L) a+ G/ ^
- else
. r: j1 ~; E" q! N+ C3 @: h. v - break;, R& ?; g' |" b- r; e7 l. ^
- }
5 f# j: L8 v8 ^3 H5 P) ]" ]' h - if(xh.getStuNo()==q.getStuNo())
* n8 n4 O( |" i' s - {
. C( F) A6 X5 Q3 u$ b5 j) r - input2.setText("此学生信息已存在");* h/ m+ |' h, Q3 y5 z+ _* t3 V9 Y
- input2.requestFocus();0 q" t) i/ v+ M& h) {* P; s6 q
- input2.selectAll();; g/ |% I4 l' I; \; ], o
- }
6 A, Y; k$ G3 X. x- U& c - else + `! b B3 B$ g4 p2 j
- {
- `: p( Y- p, G* X p - StuInf.insertElementAt(q,i); & H# U5 [+ o6 Y% y. I$ R* _& ^
- input1.setText("");) Y3 ~7 I3 t/ E& r
- input2.setText("");
* j0 X! D4 N, O2 S1 F I- }" u - input3.setText("");8 e/ _1 _) n( p: k5 U/ `
- input4.setText("");
L! e2 N# L- i- l H( {( \+ n - }- G! C2 I4 J% {$ r% M: [' i. B
- }
a( w( K* w r; ?6 t7 ?% J - }' S9 V/ P5 o1 _% q
-
9 `* Y, [/ |' U9 C2 S) i3 O" Q - //异常类/ q6 i& y" u( E ^ n+ i/ t+ Y
- class OverException extends Exception
* K" h) x9 g' p' z - {
4 Y; N3 d' p' l8 Z. H4 Q - String over;
]8 Z5 \* r# U% d- p - } % B! A% }& E: [) \. p: i) w
- class EmptyException extends Exception" p0 _! u8 h: D |$ t
- {
& O8 B2 [5 P$ ]5 b, P' o/ C - String empty;( P8 e' A5 p1 |. x! I
- }3 ]' Y& V* N: z5 n5 o
- void XingMing() throws EmptyException/ g6 C- ~! ^! l$ m7 x$ R( a
- {
2 @6 j4 J! @+ e( Q7 e7 O% H - if((input1.getText()).equals(""))* W {- l8 l% y" n+ i8 t; H" U
- throw (new EmptyException());) g# l5 ^! `/ n$ k0 F
- else5 G5 _2 ], p% {9 O
- xm=input1.getText();
0 v7 `2 s7 U% B- e1 H - }
7 @* j& q7 |2 l0 C& |. B O% `# A - void ChengJi() throws OverException,EmptyException
2 G: ?# j" w5 K - {
0 O* f+ w: \! W: D - if((input4.getText()).equals(""))% r* o; m5 l7 b! P
- throw(new EmptyException());6 t A, B' @) P! f8 q. n- a8 U. J" ]
- else# ? x, ?" H) L7 Y. w
- cj=Integer.parseInt(input4.getText());
, A& @4 @5 W. b7 h4 Y u: n - if(cj<0||cj>100)
- a) O8 U# X, }5 \9 q - throw (new OverException());% i9 d6 a% L8 e- i& T
- }
W6 n; k' S" P/ D5 k- H1 U# R5 d2 t -
% h: R; x! B9 h" N - //学生信息类
. ^ v, j R3 y( X, a+ r - public class StudentInf- a: o& a* k3 {5 w2 Z6 v T7 L4 }% D
- {
2 s( k1 a; h( ]. }7 A7 L - private String name;* d8 M# d5 P. Y- R2 C
- private int StuNo;- d- R2 N5 D3 y U
- private String ClassNo;
: R: a0 G" N5 d$ v - private int Level;) K8 n% w) Z8 G) s3 l8 i9 F
- StudentInf(String xingming,int xuehao,String banji,int chengji)2 q6 d: G3 B; {2 ~
- {
4 R v* X5 {* \0 E( J7 g/ X - name=xingming;
0 G2 |1 H$ U' ^ - StuNo=xuehao;& t& H) P3 T1 |: v
- ClassNo=banji;9 A! S' X. [' h6 C
- Level=chengji;7 Y' Y$ a3 Z+ {- A, l3 U+ A
- }
& t& Z L- }7 Q, { - public int getStuNo() a7 N0 a, C8 Q- \+ N
- {( A* B8 M: A) V" c
- return StuNo;9 `( b, i% b) i2 _, T6 u5 C
- }
, ]: X& H& O. [9 o6 f7 F, s% G- j - public String getname()* B3 k X6 e4 ^4 o& z' I
- {
9 r+ E3 P" {9 `6 U% o - return name;: s5 b8 t2 f1 S0 R3 X: i9 U3 ~4 p
- }# `6 C( s/ ~' a9 Y4 {5 F
- public String getClassNo()6 b" r6 I8 z* ], k& _. y9 ~, \0 {
- {% C6 l& F1 ~/ L7 e$ _' m s
- return ClassNo;
* I3 L2 K' ]& b# j - }- H1 B' V; A ~/ t
- public int getLevel()# U- V$ Z, b Q; m
- {
* \4 l2 H! }5 }- j4 n - return Level;5 l7 U; R$ I# S# ^
- }; i; V. A! M2 w9 I
- }5 k0 E, f+ C0 h6 s& i3 i, G
- / m) d; Q6 w- ?. q P
- }
复制代码
; l( u/ N1 ^2 }$ F9 M% K& u' ]0 b9 n9 U9 ~
|
|