该用户从未签到
|
- import java.applet.Applet;9 B$ C h( x( Z/ p
- import java.awt.*;
& S5 U/ g- s8 u& _, B: W/ H$ V# y - import java.awt.event.*;2 e2 W+ B1 ^& Q8 E0 I% r6 i; y
- import java.util.*;
0 I& L9 K+ _& l, h- r _$ Q - import java.lang.*;
. ]9 r4 M+ P, o2 y; [! r
- @6 X6 u" _: |* f& O- public class Students extends Applet implements ActionListener% E9 N+ ]! a+ C% _* W" B
- {7 Q W4 V1 E3 o
- Vector StuInf=new Vector();
. M' d3 z+ \) U% e' h; d: G - StudentInf SI;7 r2 | J8 N* x1 ~7 }3 ]: \
- String xm;% _4 O- R# K* v# q+ x% t T7 r
- String bj;0 w6 L. f0 s, C/ q s1 G( O
- int i,j,xh,cj;
6 I. @% a2 A4 o - static int mid;
4 |& S. R6 S1 {0 T# d* ] - Label prompt1=new Label("学生成绩管理系统");* O! k# u1 W X5 T8 z. x
- Label prompt2=new Label(" 用户:");
! Q+ L+ ?# y& ~6 g, Y$ M1 p, n - Label prompt3=new Label(" 密码:");
/ t T. H% W5 O: D% F0 @ - Label prompt4=new Label(" 班级:");
* b( I) a. U7 Q, i( n) H1 P2 R. E - Label prompt5=new Label(" 成绩:"); J2 [( d% w6 J) k- a
- TextField input1=new TextField(8);( }/ v. g2 B0 H) V- `0 J
- TextField input2=new TextField(8);
( I8 _/ F- M# H u+ y9 ` - TextField input3=new TextField(8);$ [ V7 Z, c; \0 ?7 Q( L
- TextField input4=new TextField(8);; n" B7 b" e0 ]3 x- q* a
- Button btn1=new Button("登录");
0 \* x4 n m D$ r6 H! o1 u - Button btn2=new Button("增加");
7 t0 \, y9 m5 [4 m - Button btn3=new Button("修改");% Y1 q, e. s ]# d$ `
- Button btn4=new Button("删除");* a- }' }4 {' k0 F8 t
- ! ?* C9 h! j% `5 U0 c1 W: T( C6 N
- public void init()7 J. d! a# g* E* V. w
- {6 |' I1 E2 [& f8 P' Q
- setLayout(new GridLayout(6,3));
" }$ O9 s7 m* Z2 [ U - add(new Label());
& m, {* `" z# D) m! c! D% k& n - add(prompt1);3 G, z# `: M# S( r4 X& ?& t5 D# M4 n
- add(new Label());
( \& @ C9 @2 A- G - add(prompt2);' ^# b: t6 S" j
- add(input1);
- y8 I9 G9 N: K; E& G" f - add(new Label());; T& H5 `$ h' p# o' ^
- add(prompt3);1 m- N% `% u: V) \+ J2 \/ q
- add(input2);6 P! {& g' W: d$ P9 A6 N/ T v
- add(btn1);
. Y5 \6 Q: Y1 j+ ]: u3 L - add(prompt4);# N7 w& w" ]- ^! s* G0 G
- add(input3);
6 \' H; C3 [' t% j- c; g - add(new Label());6 u# J6 m+ J( ?6 i# I$ m5 A
- add(prompt5);: [% o" _7 D; l, i% Y9 R W
- add(input4);: J& ^( u; C) q
- add(new Label()); M+ A" A* f, J) B+ E& p7 K( z' E
- add(btn2);
4 ~! V( O* P+ Z z, l1 u - add(btn3);
" I7 `+ s+ j+ U. G1 ~. W3 }3 d; F - add(btn4);: l e( q$ O* h/ A# p/ k9 A
- prompt4.setVisible(false);
9 T/ j; D! ?4 ^: ]0 A# ~. C - prompt5.setVisible(false);5 k6 V" k! \; q1 h
- input3.setVisible(false);$ b( c, @6 j/ A9 k
- input4.setVisible(false);$ }/ A1 b* H5 f$ w
- btn2.setVisible(false);! ?4 |; o' ~8 I9 T% L0 f0 n/ ]
- btn3.setVisible(false);
. A+ u6 Q8 R* E/ m - btn4.setVisible(false);. d% O3 r5 T e4 L6 y
- btn1.addActionListener(this);
# F2 s {5 H4 B P8 n - btn2.addActionListener(this);
! b4 E3 f" ^' Q8 X. { - btn3.addActionListener(this);0 u: A3 H8 A/ G- `# j8 [3 A" a$ v
- btn4.addActionListener(this);
; {, G1 t! n, ^) A9 G - }
: L; M' f5 J. q$ l6 @( H% R - public void actionPerformed(ActionEvent e)
" A& Z* a: E' a9 ?4 Z& `* y( p& B: y - {+ ^ e) K1 c' v1 H; a) q
- if(e.getActionCommand()=="登录")5 V$ L- |. f4 N
- {/ S X' G6 Y3 @
- String a,b;3 b& g/ w9 J) Q" A4 w! T' V& W
- a=input1.getText();+ y. i4 E: B& Y M2 s( V7 }% g3 t
- b=input2.getText();: p( S3 ^! M/ f" t0 c
- input1.setText("");- i7 Z) z" z( J& O Z% T
- if((a.equals("12")==true)&&(b.equals("12")==true)); \; Q0 |7 E( E6 A- c8 g+ @
- {
- f3 c: o' H- u7 J - prompt2.setText(" 姓名:");
1 q5 @, Q8 H4 P+ R* j" c! G - prompt3.setText(" 学号:");
9 Z t$ K3 V3 y6 I* [ Y4 j - prompt4.setVisible(true);1 p0 R) _# x2 w
- prompt5.setVisible(true);
+ a; B- O3 v' O: s3 w6 n, y( l1 E - input3.setVisible(true);
0 ^" Q/ U2 `8 T1 u - input4.setVisible(true);+ F( `5 R- v4 Q7 W! ?- b
- btn2.setVisible(true); Y" v5 U- v& Q' T, r" j( V
- btn3.setVisible(true);0 @& o. z9 g) e0 d) _' D) y
- btn4.setVisible(true);
4 }" r |% C7 [ - btn3.setEnabled(false);$ U8 L% K3 T& |# J$ V
- btn4.setEnabled(false);8 T' Q8 w6 w7 R
- btn1.setLabel("查询");
) T2 D; G+ ^7 e) F O) [3 j$ ` - input1.setText("登录成功");5 g0 m* W" A% e
- input1.selectAll(); Z, [4 y t" a8 X3 k. J
- }8 \+ @( w% \& Y/ ^+ o
- else# ?( H8 b1 {) x. n/ ~+ E5 R) \* Z
- input2.setText("用户名或密码错");
& g5 P. {) c7 i8 {$ e8 P( i - }. R& Z! G6 {6 r% t0 Q
- if(e.getActionCommand()=="增加")$ |3 e, O6 @" h& j0 d
- {" K3 L) l1 G0 g1 k
- boolean scucss=true;" u& u3 z3 K( [. a3 ]
- try# @0 m% a# F( a) D
- {
w/ c! ]( Y. R" ~2 S - XingMing();# h2 i( q M7 Z3 [7 v( g2 M3 h5 s
- }0 q* W- s; Q0 h0 j
- catch(EmptyException as)& u8 ^0 h: D4 }6 L8 k* D
- {
! j' \: Y9 x! c7 y$ c# M# V - input1.setText("姓名不能为空");
) A7 Y8 X: O' t0 r# N" b6 E# y - scucss=false;
; O H! ], c3 i) X8 F9 S0 \$ A1 g - }
0 d8 Q# f/ O; ]* J - try+ ?8 x- f0 z. C5 Q
- {
. M- l1 o" g2 D9 S - xh=Integer.parseInt(input2.getText());0 Q2 e4 H/ N! M' K( d
- }
" p7 l A% U3 E+ a- K - catch(NumberFormatException m)+ V9 J$ N) R# b/ Y
- {
+ j& `( k0 e# \+ O - input2.setText("学号为空或格式错");
' ^, y- h3 l/ i: e6 i# F, \% g! V - scucss=false;
7 {' R0 F) Z9 P' O$ n$ }) Z. B - }
! D4 T- x) Q: h5 _7 ]" \ - bj=input3.getText();1 b+ c! B0 C5 D3 A. H
- try" G. T6 ?5 h; C% ^6 o- x. D2 c
- {# i# h% X( u5 f! G5 l6 m% t9 V
- ChengJi();3 o0 N; J2 |+ `9 p+ S/ z
- }4 O; q6 e, W+ m N+ w6 d1 H$ n4 _
- catch(EmptyException as)
# \5 H7 I" i: a- {. c7 k5 } - {* }) `7 F5 D& b8 M5 Y) T: S' ~
- cj=-1;2 U! ~/ p4 q, B% Y1 h$ `; o! u. ^
- }
o1 w! S, h, D, b- i$ ] - catch(OverException dd)
4 _$ }6 W2 i: Y; d - {$ ~0 h- I( E2 a
- input4.setText("应在0-100间");) Z# ^7 U) q: _9 ]2 p
- scucss=false;
% A5 P5 v$ M: W9 x* V - }" L& p F$ ~# [2 a
- catch(NumberFormatException cm)$ j* _! o/ o; Z7 I/ j- A! W3 @8 O
- {
& U Q+ k& J! b! t" P% W1 @- ?9 a* ^ - input4.setText("成绩应为数据");7 {; j0 f8 S" H+ l
- scucss=false;
; d; F& k8 _4 s5 r n+ Z - }
% ~4 @0 g) B3 d5 w+ w - if(scucss==true)4 T; F/ Q1 m% _+ S. S- L/ k6 C
- {
% j# y, W/ u) a0 c& t - SI=new StudentInf(xm,xh,bj,cj);
& o9 f1 M7 a1 b- B - Insert(SI);5 v/ F' t9 _- c& d G
- }& ~, R% h4 Z4 z5 Z5 v2 J* f
- }
0 n- z6 r) E/ s/ ?3 ? - if(e.getActionCommand()=="修改")
/ }! s8 ?/ w% L# _( E - {& _4 j/ v2 h' b4 \, G
- xm=input1.getText();
. h8 f9 ^: p; j: Y - xh=Integer.parseInt(input2.getText());
( l }6 k7 c9 K! W; w - bj=input3.getText();
) c' `1 O$ }' y. \) j - cj=Integer.parseInt(input4.getText());
6 c. G# {* B- p7 a - SI=new StudentInf(xm,xh,bj,cj);
( O- _* L( f2 l3 d/ n - StuInf.setElementAt(SI, mid);
: V2 ?# k" ^) T3 Z - btn3.setEnabled(false);5 S4 }' p" F: E( n: w
- }& r! @+ e+ j0 E8 @# a) J% z- x- `
- if(e.getActionCommand()=="删除")
# S& P( H j) s4 z2 Z% s; Q% l - {
' S7 K. Q& i' F3 Y9 s" m - StuInf.removeElementAt(mid);
0 e9 b, m- P% S8 A - btn4.setEnabled(false);& Z- b$ z6 U7 q6 Z
- input1.setText("删除成功");* T- B3 i' o& ^ b- y
- input2.setText("");
: G7 s5 l" ?5 ?) O/ \ - input3.setText("");
_ U8 m& Q5 E& N8 p( G - input4.setText("");
4 x- R6 @0 @, D7 I0 p - }
: k$ O" M' J1 V6 P8 }# R1 y2 H - if(e.getActionCommand()=="查询")* X6 p) _- w' R5 R6 o2 B
- {
/ M( {0 o+ b1 W3 e - boolean right=true;
6 R$ ~1 ]* `! p5 M( e - try
! U6 z3 J. S; k% h8 @" p - {
0 K0 V) g& V" k! W - xh=Integer.parseInt(input2.getText());
1 B: y' j U; G - }! O- s4 g0 j ]8 b
- catch(NumberFormatException m)
0 O: m! F" Y: ]/ k; K# b0 e - {
! y8 i; C/ \9 I- P1 R - input2.setText("学号为空或格式错");9 y/ F- C* G0 Y% S1 c& i6 m! X3 h9 J
- right=false;
& k, i) x; r9 f7 T& s; F - }
2 F9 J' q" C P1 p2 e" D/ g" j - if(right==true)& y: `2 l4 {% I1 g. ^/ D9 L
- {3 s- J! a1 m0 h
- search(xh); p( q; f$ f3 n9 O; d: l
- btn3.setEnabled(true);
( q" m, K; }5 N' }. u/ o { - btn4.setEnabled(true);5 Z1 l3 q) w0 Z- _5 n. u# q
- }& z* r( w2 \+ d6 P; h5 @: {' P# s
- }1 A1 u2 Z. v8 \: D# D5 q9 `
-
: A* d8 g, u, X1 ?! ?* s. O- s5 m9 y - }
B8 g& D6 P, M - //查找方法
# k- }' @ h0 L2 m% z. B - public void search(int k)
5 G+ g C' R. E& q+ S2 b l - {
& f+ G1 i4 q& G9 x - boolean exist=false;* } N d% a0 o( y3 T7 V
- int low=0;
7 P1 I+ @0 I7 r2 H; r - int high=StuInf.size()-1;
9 r f# @8 k' J& M - while(low<=high)
" k r) d; E6 H, W; W: Z - {
8 ?5 J- b1 G' i" E" Z4 z0 y( }/ w - mid=(high+low)/2;, W) Q' O% q+ ^) S- f
- StudentInf a1=(StudentInf) StuInf.elementAt(mid);
! o' _2 N5 C, O4 H' L - if(a1.getStuNo()==k): w( G- J3 y; O3 _6 W# u# b
- {
% R( D" X$ Z1 c- W: x- \8 v - SI=(StudentInf) StuInf.elementAt(mid); A4 s" n$ p) L
- String x = String.valueOf(SI.getStuNo());
) a+ \9 r j# b) p0 B1 Q( o - exist=true;
1 l7 U# Q/ w2 I6 q. F0 G2 } - input1.setText(SI.getname());
" `6 s3 r# X* u8 @" L j - input1.selectAll();
, [) U- m% T9 Q Q3 [ - input2.setText("0"+x);: a' U3 ^/ @7 [/ d: y1 Q
- input3.setText(SI.getClassNo());0 Q! S7 q' z w9 g
- if(SI.getLevel()==-1)
2 y, i& P0 Q: ^: Q. ^6 U2 y - input4.setText("未参加考试");% p* j$ e4 j$ I( z: r7 x; {
- else
; n9 ?( v* o; T2 g' n. o" ~* k - {
6 r! g; Y' s% T: G - String y = String.valueOf(SI.getLevel());
; ~ W' W/ Y6 p6 d/ n - input4.setText(y);
! j& V% a5 e9 V3 R4 E - }- J% F6 J V X" K- `
- break;* M1 K: l! h" ~! \' l6 H
- }
6 c7 p" a- u8 T# }4 a - else if(a1.getStuNo()<k)
% | }5 s* }9 v; u( A - low=mid+1;( ?$ R* v! y, y, H
- else
) e- F d3 s" S/ [$ }0 e - high=mid-1;
( S: ~4 d/ A) G) ~ - }
" \7 X5 L$ P; _/ E2 b' M - if(exist==false) U$ H1 m5 j1 F
- {6 s5 l5 Y0 ^' i# R& j3 p
- input1.setText("无此学号学生信息");9 w6 c4 r2 P/ L3 a
- input1.selectAll(); w% g) R: p1 I# Z
- }, `2 I8 E1 u* u5 A/ D& b
- }
* \' ?/ T" D0 T - //添加方法
( F! [4 ~ Z4 P* [- x, c, N' ^' D4 p - public void Insert(StudentInf q)- g5 @6 @, ~+ z, @
- {9 ^2 }5 m* G0 K t- \( z- M7 S5 W
- int i=0;) p# v5 D' }8 u
- 1 L1 s% P h' i- I6 y$ A. u
- if(StuInf.isEmpty()==true)
' w( m" ]* t3 _& v - {! B- p" G% b& \
- StuInf.addElement(q);
3 S% D1 U! W' y* b: \ - input1.setText("");
5 @$ g& a7 j. y- V4 e0 r - input2.setText("");
* T( d: m5 F0 f" ?9 G8 O - input3.setText("");% W. H/ I" y) G- d" r; V4 `0 c( X% @
- input4.setText("");
5 \! {4 N/ y$ w5 @ - }& D- q! f/ }% a5 b! a$ H* @7 }
- else) _ I3 V5 ~( l" R3 H- ]
- {. p7 a" w2 j' C% T Q& Q
- StudentInf xh;
! |. @& {: l% P& I3 \ - xh=(StudentInf) StuInf.firstElement();
7 W Q- [$ {- K3 O. s" x - while(xh.getStuNo()<q.getStuNo())
* i* u. ~$ |% u# X: l' S3 V# q Z - {- C5 o8 o# G# e( P$ `( o% c
- i++;
8 [9 P$ o# _- u; o0 _: b - if(i<StuInf.size())
6 Y; H, G; T" s9 ~9 M - xh=(StudentInf) StuInf.elementAt(i);, B% B/ u3 I" f, n" b& O- {
- else
# E0 f d+ S" Q# y+ _ - break;
' J, o) U# S2 c |: {- P% h, Y - } ) s; H/ D5 y, l
- if(xh.getStuNo()==q.getStuNo())
- E- ?/ W L! Q. _. r4 d - {
/ L8 t8 ^) K9 d# H9 k. N4 v+ w4 U - input2.setText("此学生信息已存在");
/ @0 x) @3 b. d& c5 Z X - input2.requestFocus();
. c8 }6 e8 u' u4 a - input2.selectAll();" H7 i3 J+ t0 H) L0 }" z
- }
; r* W7 y5 o2 G, w - else / v+ Y, J$ t2 a" Q% V
- {
4 Q1 S' p9 X9 s* B - StuInf.insertElementAt(q,i);
6 `! k& p a0 r7 G# O/ ], _, O- e - input1.setText("");- _, S- y) q. A4 t8 m0 `- g. P$ t
- input2.setText("");) T, X. ~( F, j. N- f- h* D
- input3.setText("");
: m, [: \1 t" Y4 Y! ]$ z1 ^; V. l - input4.setText("");: C: {* i1 u8 Z0 p/ Q# B
- }4 l, O0 ?( s( x# x/ ] Z. @
- }
% L, ]2 d4 ?8 J0 w - }. v/ D3 o( b9 L) e
- 5 O1 P1 ~% F6 y( C1 l' K
- //异常类
) C6 ~# n3 \5 u - class OverException extends Exception9 g- C h8 z* B7 x9 X
- {' P- g9 Q$ n" |5 \, E& c! O
- String over;
# \/ y, T! B: r$ }) O - } 1 L+ T2 J k! q3 F, x
- class EmptyException extends Exception
j o1 L" \4 y - {
5 C$ ?5 `% G7 k# ~ b) S: t) P - String empty;8 T8 {9 m8 x5 r( B: J
- }5 E3 u8 s: X& x3 e9 }' n
- void XingMing() throws EmptyException
! S: { M1 R: V$ }2 t- w - {
& i h& a! j' C4 Y; W4 ~, [$ P - if((input1.getText()).equals(""))
% N! g1 G% g% N - throw (new EmptyException());
, H' s& z! A5 p1 y$ \0 Z: n, ~ - else
/ q5 i# t9 Q" L2 o- I4 s - xm=input1.getText();' i- s3 x1 c/ B: P
- }
0 q2 H' U1 Z' ^% F& V9 ~6 C - void ChengJi() throws OverException,EmptyException : L" a" ?1 A" \2 Y
- {
% |/ t( x- m% w - if((input4.getText()).equals(""))- A$ A+ Q6 h3 o7 ]9 j( d# A
- throw(new EmptyException());
5 ]3 c( v4 T& w5 Q1 l8 \ - else
# @3 K7 s- }. z" U | - cj=Integer.parseInt(input4.getText());4 k$ u5 Z/ ?( G2 W# U
- if(cj<0||cj>100)2 v' ?' f9 |& v) [" p# Y+ _
- throw (new OverException());
" j6 v1 | F! w2 x4 ^0 R& I0 N - }5 X' U3 O, y; \5 S! S( d% A8 ]
- $ Z- N! q; ]: y8 X4 [7 i6 C; s' X
- //学生信息类
; i3 _/ B3 x" G8 D/ w9 x - public class StudentInf
& H3 }- L a" \- y4 \! c - {
& A& a( a$ o. O1 B/ H$ S6 A# P - private String name;
/ G. E; j* W' l4 F - private int StuNo;. z( P7 f$ G8 S+ p
- private String ClassNo;
4 L& z5 F8 D+ X4 n; r) A - private int Level;
2 @" q- O2 J8 ^2 I - StudentInf(String xingming,int xuehao,String banji,int chengji)2 G; V; d9 o( E! u7 Z$ `
- {
8 Y6 J+ [9 L+ N+ C6 C% y - name=xingming;" l* e) K( \% Q- V" Z
- StuNo=xuehao;
$ m4 L- }. ]) E6 z% L/ @ - ClassNo=banji;3 ?1 F, Q& w% [
- Level=chengji;
& z/ i* j* W( w - }" ]. i4 e* K" {* \7 ^' ?( G
- public int getStuNo()0 x* c+ I8 l& i$ i% E
- {9 |: q$ g) J$ z# H- }* R
- return StuNo;4 W8 G' ?9 \% B+ c. f; x
- }
% h+ Z6 ~7 r: @. i/ ~( V) F - public String getname()9 |" d" I! x, P! \7 T: M
- {" U: b. {7 l! E( K5 g/ ]3 w
- return name;
7 U4 T4 A! L0 Z* c! u: b2 d - }* r) N9 k' P* J5 Q
- public String getClassNo()2 [9 R; \1 n6 F$ g4 F8 L2 J
- {! E; i% C, ~: f J) _. G: V
- return ClassNo;
& r& D! w" x& D. g' G - }$ }: v+ N2 n Q0 ]/ ~
- public int getLevel(), j G: T* X8 D n
- {$ [) w: I* L* @- }; A
- return Level;
7 W+ s i X+ B6 x' U - }8 t1 b& S9 |2 \+ A4 T
- }
6 k( {: x/ C1 \' @2 U
3 Q+ n0 J# f, I' D) S g1 }2 i- }
复制代码
( W6 R6 n( P) d$ p* b4 K# K y' @+ ^; o6 F" y
|
|