|
该用户从未签到
|
- import java.applet.Applet;
! G8 b; k3 i( t - import java.awt.*;
% ~9 F2 C+ \$ D7 f* t- ?% U - import java.awt.event.*;
4 ?- O" o- c* w# H4 k - import java.util.*;
4 L# s: w) q# d) [) G, m* J5 y - import java.lang.*;* V$ a4 A$ c! l5 m0 J9 t, c7 T0 @
- ; P) @, c- }& x" i; l$ z6 o. V! A
- public class Students extends Applet implements ActionListener) J! w+ @8 v& z% V
- {6 H1 \. O+ v3 ]/ Z) n
- Vector StuInf=new Vector();
2 `" n; M, s3 }4 o) t8 Q2 p+ o - StudentInf SI;
" h( m5 T" ^5 T6 j - String xm;' x* ^3 a( H, C) n& f
- String bj;9 F7 j2 x1 v- [+ c
- int i,j,xh,cj;) B0 F$ ?0 J; y6 d( W, @8 d
- static int mid;
1 |9 l1 \# @' e% ]1 G& Y0 H6 v6 G( N4 m - Label prompt1=new Label("学生成绩管理系统");
: Y2 j+ L0 d! d0 @6 e; |" P - Label prompt2=new Label(" 用户:");
( K i0 h7 c2 z - Label prompt3=new Label(" 密码:");
r& k* [7 y) P/ ~5 a( d2 Y! l - Label prompt4=new Label(" 班级:");
2 m- @& O, q/ v6 P - Label prompt5=new Label(" 成绩:");; ~; j: p4 g8 T# b5 x* K. m
- TextField input1=new TextField(8); Y* V$ P5 g/ o8 b+ G+ T7 L& B
- TextField input2=new TextField(8);! u; t5 I. }! \/ [3 N9 x0 \
- TextField input3=new TextField(8);, n7 W; J# U8 C& X* @1 o
- TextField input4=new TextField(8);
y6 |2 F+ b" e, y% A N7 g - Button btn1=new Button("登录");$ t& ~* X- O' G0 Q# K f, p# T
- Button btn2=new Button("增加");
' K! K+ {- D7 {4 c! P% h) d- p - Button btn3=new Button("修改"); T- `! l7 g' b. B0 w# a& ]+ k
- Button btn4=new Button("删除");& U. D; b/ {/ e% c
-
) O5 X8 o3 D* ^: N- h6 r - public void init()
4 f, A5 h. W3 {5 c9 ? - {
2 S' k" ~4 V% E4 a - setLayout(new GridLayout(6,3));9 `! _5 y1 C5 x0 o
- add(new Label());
; |3 z5 c& {) ~2 ]$ D2 D5 K$ T - add(prompt1);5 V$ d5 G5 H% B4 j5 o) I3 J
- add(new Label()); q# @" n- B% m: v W0 Q0 k6 J! V
- add(prompt2);
& g" S2 a1 G2 \* Y - add(input1);
' F5 W" @ a/ S7 U7 A - add(new Label());# R( l" _( v( _6 Z
- add(prompt3);
* Q4 t9 a4 d3 G5 B9 e* R& T - add(input2);
9 [' ~7 M' f/ c1 r* @6 p - add(btn1);! [9 X- A# Z! C2 k4 Q; v( T7 |
- add(prompt4);
, g+ |+ k9 g3 L! x. V& d6 I* w - add(input3);) t8 m1 j3 a( V
- add(new Label());
! H5 K" a7 C+ L, ] - add(prompt5);
" T& D; Z# r2 h5 E6 u% x6 K } - add(input4);
# Z5 g; f6 r8 ^6 c - add(new Label());
" U9 S1 V/ [9 i0 F - add(btn2);
- o# }6 j. L$ [. J( d - add(btn3);# b, q/ c. J* l9 W
- add(btn4);
# m2 `- z% [0 C4 t0 y$ W5 t - prompt4.setVisible(false);: [6 _6 E; P/ }8 _. o5 f, ^
- prompt5.setVisible(false);/ Q: _( Z$ G' x4 k
- input3.setVisible(false); O1 V- _, @9 j
- input4.setVisible(false);4 f. L y1 ^" D% }1 u3 E
- btn2.setVisible(false);
4 B+ Z. s$ u& O* i! P7 m4 H - btn3.setVisible(false);" h* y4 m6 [$ b" l5 {- C$ {7 f) d
- btn4.setVisible(false);
# N* ], f) U9 o; Q+ _4 R1 q* o0 x - btn1.addActionListener(this);
# t! |9 b. P( u5 }7 j - btn2.addActionListener(this);
$ s7 E# T! h) h4 R3 { - btn3.addActionListener(this);2 e; G9 z! C) k9 B2 e! W7 T
- btn4.addActionListener(this);
) D' M+ M, c* R - }
5 \5 w9 w! H4 R3 s: \ - public void actionPerformed(ActionEvent e)+ \1 k/ X* j* u: L
- {6 }: T3 |; @ ~; H8 z* ]9 @
- if(e.getActionCommand()=="登录")
" x$ Q7 O+ P: q* {5 q; n9 T' V G* q! v - {6 o. r1 w) ^+ B7 P
- String a,b;
- B1 q* g2 S1 F! N+ E3 C - a=input1.getText();
- o" m1 Z/ k* D# j* G - b=input2.getText();7 j4 S7 B: b$ e; Q+ d
- input1.setText("");7 y1 X( c- D$ v+ S- V9 \
- if((a.equals("12")==true)&&(b.equals("12")==true))
$ C$ D- h7 I) R) A1 S - {
7 X& Z7 T: j7 }, U* { - prompt2.setText(" 姓名:");
: w3 u9 q( M' q; t: v - prompt3.setText(" 学号:"); . _" C+ }/ q8 g! }' A( C
- prompt4.setVisible(true);5 c9 b6 V8 x5 D7 g* c, m3 g
- prompt5.setVisible(true);
5 ?. u( ~& K$ }( N# \! p - input3.setVisible(true);" m, N7 F6 N6 ]4 M) k
- input4.setVisible(true);
" j- a3 I# [, C/ c, ]( x - btn2.setVisible(true);
" P3 N( ^: {9 \2 P; M- @3 d - btn3.setVisible(true);
% B; ]9 L) p) }6 G! K4 a0 ~ - btn4.setVisible(true);# q% D% v! _; T
- btn3.setEnabled(false);
& y. O! Q% N, W$ [6 r! U) P - btn4.setEnabled(false);% w6 Y) A! a! e: i) E+ M
- btn1.setLabel("查询");
; I% I3 T4 I8 K* X - input1.setText("登录成功");
- w) a3 v1 F0 b0 @5 V+ V0 f2 @ - input1.selectAll();
0 h# b. \3 t9 \1 h - }; m: z% i/ F+ } V% p3 Z
- else
- G0 [9 D5 D* y3 R+ { - input2.setText("用户名或密码错");
: u6 i0 j: x7 s* a - }+ H0 p5 W1 x+ s: W4 b. @/ X7 j; f
- if(e.getActionCommand()=="增加")
4 B! |( d5 D" @! J! `6 f& f' ~ - {
2 k6 S7 \* L X% F+ ?. Y - boolean scucss=true;
" S) [3 ]. p4 x - try" G* k A1 I3 c
- {1 x5 U; Z5 ~( L; W: e5 W: R" v
- XingMing();
# ]2 f0 t( Y3 X9 C - }
1 P! m1 ?( r. x - catch(EmptyException as)
q4 c. P _' r# r7 ?8 m! u% F$ y5 P - {( Z5 F3 @4 L& Q; ~, _
- input1.setText("姓名不能为空");% |9 D" m5 I$ Q: G O' p
- scucss=false;- B2 f4 q, n" @
- }3 ]8 P. m# T9 L7 s& y4 p
- try2 |9 R7 c7 }+ \3 @5 |; j/ z( Y
- {3 y% n, z5 H' V7 d8 U
- xh=Integer.parseInt(input2.getText());+ |1 w4 s% H% J
- }9 ]" u7 g4 ^# y/ m3 \, F
- catch(NumberFormatException m)3 z* [* f% L: F
- {2 ~: f, r& k: k/ M y/ v
- input2.setText("学号为空或格式错");0 z% u! s- Z! ~! E7 b
- scucss=false;. o3 U$ [! }* B
- } 9 H% M: B3 j1 l* e% s
- bj=input3.getText();& `' U# a' C- p( X3 q) g3 h
- try
" b1 \+ p4 X) g* ^' l7 I - {
- O) w3 F& t; _& H$ O- p$ C) j - ChengJi();4 R i3 e: ^' Q/ B- A0 P& x: p
- }
4 n" \- X- u& k5 q1 Q - catch(EmptyException as). F5 A* \! l L' K7 m3 m7 U" [
- {6 ?+ K; p6 m& r3 L8 i- s' g2 m
- cj=-1;
" Q3 N' e8 [; U& G, b$ V( n- l - }
8 g9 z: V: t. @, Z+ [) a - catch(OverException dd)
" _; ]9 u0 `) m" _ - {" }" u+ q+ m, {
- input4.setText("应在0-100间");9 r4 Q) }/ M% a
- scucss=false;
" ]5 @% C- [$ ]! m - }
; c7 r9 T9 l$ S2 N - catch(NumberFormatException cm)% N0 n2 P. Y/ b
- {2 l( c$ Q! w# x, s% K X
- input4.setText("成绩应为数据");
) O: J) Z6 b1 b8 q' x - scucss=false; : x/ v2 y* {4 r5 a
- }8 {4 H# D4 u h( K# A8 W- d
- if(scucss==true)" {" `' c* K2 t
- {) P" U0 t. n* v _+ q
- SI=new StudentInf(xm,xh,bj,cj);
% `+ K2 Y6 T) D- R' ^. J' m* ` - Insert(SI);, m4 L4 k q( C- l' |1 @
- }
2 X" |0 x0 q5 o' t- R - }' j' L! I8 I) S7 g0 s/ Q) \" U
- if(e.getActionCommand()=="修改")& w2 d) P, s0 @; a
- {/ h- C# ~4 x, W: u/ P
- xm=input1.getText();
4 I0 Y# O8 ^. [8 b! C - xh=Integer.parseInt(input2.getText());. Z4 ~9 X( l4 P
- bj=input3.getText();
2 @/ y- Y9 u: j9 X4 P& y: S - cj=Integer.parseInt(input4.getText());& M/ B2 k$ w3 Z( Z$ O! Z5 ]
- SI=new StudentInf(xm,xh,bj,cj);
% d7 Y' [ d9 J* f* p( _( G - StuInf.setElementAt(SI, mid);: U- Q0 S Y$ a
- btn3.setEnabled(false);. B! J. z# z+ q; | L
- }6 W/ N- {7 q2 f F9 I4 ^+ q
- if(e.getActionCommand()=="删除")9 g' F# n" G F9 g: |8 D6 e8 {6 l
- {4 z' p$ W; H9 z2 O& R1 h, s' ^
- StuInf.removeElementAt(mid);
+ ~$ u/ G: y& t6 V3 S6 k: X* J - btn4.setEnabled(false);! d. _# l5 X/ l: j
- input1.setText("删除成功");
1 b$ D/ ]5 G7 ~3 V; \ - input2.setText("");, d3 R. t/ K2 j F/ c
- input3.setText("");
8 _5 l4 U; g. y8 V$ q$ ?$ [ - input4.setText(""); i6 J0 x6 R8 \& Z% q
- }
( s# [( Z3 j" z& v! J - if(e.getActionCommand()=="查询")7 R. M* C+ V1 r% L
- {8 a4 F* q+ l) @, A9 `4 W
- boolean right=true;8 D. z0 P; Q5 _7 U6 ]
- try
* _( l9 \6 x( G2 k - {) i$ X" \. G% }% R- ?
- xh=Integer.parseInt(input2.getText());+ c5 n- w! X1 p( I& @
- }4 D% S `: d" }$ {2 q
- catch(NumberFormatException m)4 e0 ~0 l' I2 c% y4 l$ I/ P) H
- {. C% E4 a- w4 a/ B- e
- input2.setText("学号为空或格式错");
8 _2 W, C9 u% ~3 R - right=false;
) L- Q* N+ ~1 P9 Z - }
- I0 h- C# Y" j6 u" G4 W$ S2 R - if(right==true)
: ?6 W! }+ Z) {& V) X. p# m% d5 h2 A - {' J- Z5 Z; l) O. Q
- search(xh);
# ]) T, {0 g( A - btn3.setEnabled(true);( y8 t, V. ^1 m5 z% i8 S
- btn4.setEnabled(true);: T9 e3 i# ?2 x
- }" C2 Q5 C' {1 F5 m8 Z- s m4 N( H
- }
+ z8 {3 Y$ z! h( D - ; _( f5 A+ ^4 [$ r5 |' Y+ k" z( ]
- }$ u+ E1 W1 {5 O( g Z7 p4 p
- //查找方法1 t# d8 ?/ i/ @& h8 u
- public void search(int k)3 T1 o& o% I+ |- p' w0 Z: @
- {
! W& J0 V: @) T$ Z5 [0 C! `3 p - boolean exist=false;
0 c2 x1 D4 W0 P; a# C+ N7 g2 k' x. F - int low=0;
" K6 I: x' R+ W& w! z0 h( z - int high=StuInf.size()-1;
( K! }8 P! {! @ - while(low<=high)
+ ?7 d/ Z* L3 P8 g( G - {
9 m$ y+ r6 t( p, F7 w" L7 N* G - mid=(high+low)/2;) H/ F8 V i. N/ z
- StudentInf a1=(StudentInf) StuInf.elementAt(mid); ]7 w" u' L5 h$ w8 m1 b& s N' K1 S
- if(a1.getStuNo()==k)
, j+ C3 `5 [$ t$ d' Y& H - {! W; N1 Z" Q2 ^5 G9 Y+ U
- SI=(StudentInf) StuInf.elementAt(mid);2 c; [/ m/ w$ z& d! L& q! ?" R
- String x = String.valueOf(SI.getStuNo());$ ~- D" ] P0 z$ q) x
- exist=true;
5 X. P) ^1 P; b% F7 M - input1.setText(SI.getname());# y5 y% r% t( ]. N' x, k1 H
- input1.selectAll();4 \$ J2 _# d9 b1 B9 N' \
- input2.setText("0"+x);
% g3 x- u8 g" M - input3.setText(SI.getClassNo());
$ F6 |4 ` {# |" Y! A$ h6 ^: g - if(SI.getLevel()==-1)5 O" H& k0 b# \: p) I9 b
- input4.setText("未参加考试");
, I7 z v! V" E- q% f - else
2 G" U+ T+ E& r - {, F# \$ \5 G3 W
- String y = String.valueOf(SI.getLevel());
3 I) t4 Z# j7 ?! G' m& [7 d - input4.setText(y);
" ]! {' S; ~# v - }
7 d4 ^/ p7 Z' @0 O - break;
6 G" ~1 W7 _% Z* | - }; l$ Q8 S+ H3 u+ y
- else if(a1.getStuNo()<k)
% C$ H1 M1 O* A/ n/ H8 `- P) U+ O - low=mid+1;
( V1 l& L+ \/ d( k/ t - else0 X: s+ c. ?' \ Z7 R1 W; v4 t! l9 m, u
- high=mid-1;
; L8 a6 X4 K) v: u( d5 } - }& ]! ~. X) N+ e5 W& X
- if(exist==false)
, T: q1 J3 a5 Q) I a& W( g - {7 @, j# k/ A5 e* ?. ~* e
- input1.setText("无此学号学生信息");
2 j. z( e6 |; x" d - input1.selectAll();
+ O! m& S- A8 h$ D( s9 v6 }; l - }5 Y8 M. {4 N! `& K1 L
- }
8 M5 o. y* Y" I - //添加方法
' _3 `; R `& i; N' ?- O0 ^ - public void Insert(StudentInf q)
. [! }, `6 H7 ]. r - {* i& t8 A6 l; D f
- int i=0;
# _( @) i$ w" K# ` - . d# h$ t7 w1 d$ \4 \3 `7 W8 u3 r8 n
- if(StuInf.isEmpty()==true) % s D, t( ~5 J
- {
4 f0 L" ?/ V. b' k8 q - StuInf.addElement(q);
% U+ j1 o. g* }6 S4 X9 l - input1.setText("");" J& v6 @+ g Y& _
- input2.setText("");" t" _& a* C# f& P4 g
- input3.setText("");
$ u+ H/ B+ j8 y. L( f$ R7 h8 h - input4.setText("");7 _$ V# d) @2 x' D* D$ i: I' v0 @
- }
* n' F3 ~3 j6 W - else7 y- k4 M& b% [9 ]' Y
- {) _7 Y# s, i( P+ ]" g+ d' ]( Y! t
- StudentInf xh;
& B7 u6 c- l7 o - xh=(StudentInf) StuInf.firstElement();: c& @4 q' k& B q( f
- while(xh.getStuNo()<q.getStuNo())
; f1 G' t! ?3 b - {
! I( E: f4 Z. ]; S - i++;. f. P/ `0 _+ z# x% R
- if(i<StuInf.size())* ~# \/ g' K! d" @2 R" p
- xh=(StudentInf) StuInf.elementAt(i);9 U1 t; f. z) u7 `( U0 H! |
- else @. o, K7 ~1 k% q5 D' n/ J% u
- break; t9 [! k& U; k9 b
- } 8 e! e7 E0 M( V8 m$ [
- if(xh.getStuNo()==q.getStuNo())2 p6 `+ S) t& w; }1 b' _& }
- {
. x3 i- Q- A, d m, ^, c - input2.setText("此学生信息已存在");
) l# P% U" X, O4 T& U5 P+ z - input2.requestFocus();3 g+ {9 [1 P9 Q: t
- input2.selectAll();. s% w) q+ b, H7 H1 \
- }6 [! {9 ?, N$ n# G+ q
- else
2 a6 p$ R* I( Y, n - {
3 \7 y8 V/ ^# G( n7 ~* ]* B - StuInf.insertElementAt(q,i);
% X# ?* W+ z/ R1 |/ m - input1.setText("");
" Z& H( w0 G1 i2 ?/ l - input2.setText("");4 v, L4 I P! _% @
- input3.setText("");2 U8 { g' E1 v- k
- input4.setText("");
1 G9 r6 z3 c, G- d- D& L - }) ]# k- b, d' H7 e
- }
$ h/ X6 l n8 X+ j& V - }& a$ N$ e. c2 X
- 5 A, D1 O/ E0 {7 W: A: `
- //异常类
2 |; \, I4 t+ J) Y - class OverException extends Exception! i0 _; k9 I4 f4 @6 C- P
- {
5 Y8 q0 j5 { B6 o( ]0 b - String over;
6 n+ c5 Y& H: I; _- e8 U - } 7 s9 b2 X. }5 Q2 ] N2 l
- class EmptyException extends Exception
' X, `3 C7 t; F: ] - {+ J8 L! L" O i% Z1 F
- String empty;
6 z( Q: m1 T' w4 ^* u# S, h - }) P$ g9 x$ L, y! M1 p; h$ V
- void XingMing() throws EmptyException: A8 \4 n9 k0 K% w
- {! r" ^( k. J0 F' W2 ?
- if((input1.getText()).equals(""))
' y ^5 ^4 V8 ^" @ - throw (new EmptyException());
' O0 u4 }, \8 e I5 u - else a, p" y7 e" ^" S7 M; E3 d* Q
- xm=input1.getText();4 a! ]% ]- {* \! R- u; S* a( @3 `
- }/ @* F0 z3 M" O) ?$ L: t% P- O
- void ChengJi() throws OverException,EmptyException ; w3 C1 Z4 J7 J& U5 D7 N
- {; e) v4 M( ]0 h
- if((input4.getText()).equals(""))
/ J6 E& i( Q4 I1 F9 D - throw(new EmptyException());
- k7 k, d& F. D6 H( R - else
' v( A6 `% g6 ? - cj=Integer.parseInt(input4.getText());
' Q: P% H' K2 O; I. k - if(cj<0||cj>100)5 f& e6 d x+ u. `: K
- throw (new OverException());5 f, c F' h9 J' Q% G
- }. s) _7 X! V+ \
-
; G# r" x" ~; B5 ? - //学生信息类
- F2 _! R1 g' d - public class StudentInf8 a8 }& h7 b* `( v% m _! w
- {. T$ _! C2 v0 O% n, J% f( N2 V
- private String name;
2 p: t, \7 u: y - private int StuNo;
) n- {$ s$ p7 ?0 ~ - private String ClassNo;
# t, Z0 x9 l# @6 ^/ ` X# J - private int Level;
% J; J8 B2 ^, \6 D0 N( E3 x - StudentInf(String xingming,int xuehao,String banji,int chengji)9 E& H' I' @9 K/ K, G
- {" ~; c1 k. K2 X; s4 a
- name=xingming;
* @: d( \) M" ` ^3 c - StuNo=xuehao;5 {0 K1 |# b9 P( \& N7 z
- ClassNo=banji;
( G5 p' D* d' |9 X) [. f - Level=chengji;; x+ Z1 m2 f. s3 h0 a' S
- }. G) g- y+ R6 f9 }
- public int getStuNo()# k# h% U0 B8 q" G4 p" l
- {
% t+ H x3 v. F5 w% F - return StuNo;/ A _8 l" K$ b5 b# M( {4 ^/ B
- }
* N$ o* j# M% S/ a& A S - public String getname()
/ l8 e* w- I6 l& m* D - {
' ~7 A6 f T) y9 H - return name;
) C$ Z3 k$ d3 ^( T$ I" [% ?* Y - }% I5 o8 \; n$ ?( s3 X! [8 h% t3 y
- public String getClassNo()
' |3 m) _4 _$ P0 {* O - {: b; K- y2 V0 W! ^" Z3 L- s
- return ClassNo;
" a9 p# b0 x. m8 \( H) J - }
J3 Q4 \# |0 X$ h - public int getLevel(), z ]2 ^' I9 e6 M9 R
- {
/ E8 X- B( e7 X$ P5 r! o - return Level;
L' H) X9 Q( o0 H4 B - }# o9 O3 R* X3 u
- }' x5 t7 i4 N5 S+ H+ b& O+ X& v
$ q, `, w. Z8 I3 b9 d+ {- }
复制代码 $ s( v) o3 B2 `3 w& G5 k
. A( u0 q2 T1 y6 ~7 D5 Q3 u
|
|