该用户从未签到
|
- import java.applet.Applet;7 o, ]8 P8 H B
- import java.awt.*;2 G+ l% C0 ]2 q5 ^. i
- import java.awt.event.*;0 Q( R9 k# U# O& G( z) x5 M
- import java.util.*;3 l+ f. e0 c- h: w9 F7 y9 ?
- import java.lang.*;9 i; I, u; o3 y
1 _$ ` I: V/ o/ t7 i" g! m- public class Students extends Applet implements ActionListener4 n* f& p3 Y6 O! G3 W1 I- ~
- {
9 P0 x5 Y( r+ {; h+ h9 F) I' B - Vector StuInf=new Vector();# A/ g0 ^ [6 |/ V: T! h" X0 [! [! v$ J
- StudentInf SI;) ~! d' @1 v, e7 h6 s( Y( u! {
- String xm;
1 j5 L$ P+ M) i9 K6 |1 C+ U1 t, O - String bj;
( z( H0 b! L, a/ ` - int i,j,xh,cj;
6 }6 [# o; G, v - static int mid;
1 ?' Y9 N& k2 s- i - Label prompt1=new Label("学生成绩管理系统");" r! A! d* c" j
- Label prompt2=new Label(" 用户:");0 `% g. k: n4 A; v, i$ g
- Label prompt3=new Label(" 密码:");
& g% O; g" [- ^5 i) n% W1 ~# C - Label prompt4=new Label(" 班级:");# T# D- E- k$ X4 C9 R
- Label prompt5=new Label(" 成绩:");- Y8 Z& B: h$ m! Y L4 Y. S* r: t/ ^
- TextField input1=new TextField(8);
3 h; Y* p: b$ D7 l# d$ w' V. Z - TextField input2=new TextField(8);. { \: I2 ~, g# p5 T
- TextField input3=new TextField(8);& h3 [0 h- s8 v+ \* r9 R3 f
- TextField input4=new TextField(8);
; {( V m0 ` H( r - Button btn1=new Button("登录");+ O; e" n S/ }% `
- Button btn2=new Button("增加");7 h8 ~2 r6 l/ Q
- Button btn3=new Button("修改");5 j/ `3 {6 \% i+ S$ M
- Button btn4=new Button("删除");
8 g2 S7 w, S: l3 G. b4 |' y - 2 t# k/ `4 E' L1 H: h& P3 j# ~
- public void init()0 Z" ~2 {9 A' x: o% k
- {
9 F- d0 f7 W4 i - setLayout(new GridLayout(6,3));
/ ^4 b" i& v& ?: {5 t) p - add(new Label());
4 M! j+ q) {3 n m( } - add(prompt1);6 f# I# f {7 D7 a
- add(new Label());
4 Y, W1 ?) x$ R* z6 {& j5 s - add(prompt2);
1 ^6 ^1 m l3 F; Z& @6 B& m* U - add(input1);% j3 F& y, e( F0 ?- j+ I: n
- add(new Label());8 u" e, a0 I3 i+ v) U: S
- add(prompt3);0 G8 r8 B" }3 H+ ?2 r7 e T$ u
- add(input2);& B3 i; q* e( @! y& J% u
- add(btn1);1 Q; I* t- V7 @! [' V- v
- add(prompt4);
4 Z* I7 d* [. C: p. X - add(input3);
& F- B w' Y/ Q1 o3 t - add(new Label());
% }* \; m E) m' b9 J4 V# b - add(prompt5);
x) r' r; y( u- F5 n9 F - add(input4);
6 X' W; x+ e' @- e% w6 C - add(new Label());
: }; v* ]8 t) d( A5 T3 {3 @ - add(btn2);
! U% T& [' ] \# D6 m# R - add(btn3);* S5 w3 g! y. K% b; Y3 B
- add(btn4);
) p% A: j- d- {8 x" W - prompt4.setVisible(false);
3 U+ W1 m# V; k3 v, d - prompt5.setVisible(false);
: \# m* _2 J# [3 S, `1 V! Y - input3.setVisible(false);1 Q; j! o; T# G: E
- input4.setVisible(false);( Y8 Q1 J! M2 g3 v
- btn2.setVisible(false);+ _" S; i2 C. {! \" @
- btn3.setVisible(false);
0 L" h2 n2 |8 x$ K* R0 N0 R - btn4.setVisible(false);
$ m* U4 {3 U0 \- p% Y - btn1.addActionListener(this);
1 x' w$ n1 l( P9 O6 | - btn2.addActionListener(this);& `" ^- F3 b! M% d: Z8 U3 c# h( a) y
- btn3.addActionListener(this);
( C! Z8 e1 x8 }; ?8 f$ X - btn4.addActionListener(this);
9 I2 ~+ C, f4 o1 G& x - }
2 h4 B6 _1 z3 n. P5 _3 N - public void actionPerformed(ActionEvent e)
7 ^) Q4 t( m$ U/ N( x* _ - {" F& e' v' c' p- `2 a, O
- if(e.getActionCommand()=="登录") g5 z7 S0 ]# Y. P% @
- {2 A& ]9 s# _' V: k
- String a,b;' A5 _6 I6 t8 ~1 D
- a=input1.getText();# j# Z) }9 ]! b3 W% y: |: D
- b=input2.getText();7 B5 f, ^6 X2 W; W D
- input1.setText("");
- k4 B( b6 x9 _ - if((a.equals("12")==true)&&(b.equals("12")==true))
5 l) C% c. d& O - {. e( P/ k2 F. y
- prompt2.setText(" 姓名:");
' a$ X5 g' u# w$ E/ J) R - prompt3.setText(" 学号:");
$ M; _! _6 A9 d$ \$ ~5 J - prompt4.setVisible(true);
3 p5 s% {) L; \/ M1 i - prompt5.setVisible(true);* N& N. t \* K6 S1 }- p) B/ {
- input3.setVisible(true);
* Z: x- S9 ?0 l. c* G% E$ @ - input4.setVisible(true);
! J6 A& S" Y) x" c3 p2 k& y- Q - btn2.setVisible(true);
! u, S5 o$ o; Z/ Y - btn3.setVisible(true);
# M* Z6 s6 Q% Q - btn4.setVisible(true);. K, }+ R! H0 V( }+ g4 o% X$ @5 E
- btn3.setEnabled(false);
4 [6 S8 R7 h' d1 L, A! q f- D H9 i - btn4.setEnabled(false);& z( A0 m8 _# U, }3 C9 M. S
- btn1.setLabel("查询");$ G" S& v5 D& G: N) S' q0 D! L
- input1.setText("登录成功");$ Z- E, v- s8 v1 j2 i0 b: o
- input1.selectAll();
2 I$ P' {0 f* x0 ^0 \+ Y B - }9 b' L4 H6 E8 Q. @4 ~ T2 L. p
- else
# u ]0 \' w. B: r* t - input2.setText("用户名或密码错");
9 P2 v# ?4 d( o1 _ - }
( Q6 X9 ?' N% a @& C$ y - if(e.getActionCommand()=="增加"), s$ E* S2 M e I
- {
/ ^/ ?: d, p- p3 H' K - boolean scucss=true;; v$ m' R3 z& L1 k1 d' O; M7 A
- try& L R6 {6 Z' T1 ^4 y
- {- h( E0 w# Z) @! |7 K7 K
- XingMing();
$ K: x7 V( p7 h - }$ ]( K' Z* _" h" j6 G% O
- catch(EmptyException as)
$ B! b! L) s" E1 M+ e - {! b. z/ y! r4 ?
- input1.setText("姓名不能为空");4 ?+ w1 X, I0 f% {/ M" g3 _
- scucss=false;
: Y8 U# ]' x' Q w4 z/ C - }& w/ y8 n/ p0 ]- E
- try+ {2 d7 I6 F$ s+ E$ q+ v
- {
4 U+ y( Z3 k3 `0 f! Y7 D+ w - xh=Integer.parseInt(input2.getText());
' M# X1 U& d" {' F/ V6 u$ D - }
( y& _ \* y( @2 H, I6 Y - catch(NumberFormatException m)+ }& K5 y3 s' U8 {2 x$ m
- {2 J- t. l4 l9 O9 p
- input2.setText("学号为空或格式错");3 v' H3 `6 j2 G& U8 z( u
- scucss=false;- E- M, E- m6 x, A
- } 1 S& s. m5 T# M8 R1 H) w* W
- bj=input3.getText();: B9 H5 ^3 N6 P2 G" X/ x; D
- try1 z+ ?' S( ~9 e/ R
- {
" {: [. g( U: l7 W5 W! L - ChengJi();+ I/ q7 G. F8 R/ I
- }
2 Y2 ]3 H8 V: W - catch(EmptyException as)
9 b. R8 m$ o' y+ m3 K& a4 F2 N7 R - {$ y% g6 c) P/ b$ Q: k
- cj=-1;/ I% Z: ]$ {- u
- }( l R5 P4 X5 A0 k$ h
- catch(OverException dd)
! M( S4 S6 A3 b& |3 O d4 _; T - {
7 M0 o( J6 ~+ z1 ?' t - input4.setText("应在0-100间");
7 ^6 Q3 }( R/ D+ G( y% G, ^ - scucss=false;- x3 Y* v% b3 Z8 G0 o
- }
) q; l7 v. K+ A) m; d8 x; t/ d; Y - catch(NumberFormatException cm)
% q1 |# o, s5 ?* U- W - {, y0 v* x C1 l/ }5 {) Z% z* X2 R
- input4.setText("成绩应为数据");# Q3 d; d. I' q0 H: m8 E$ l) ?
- scucss=false; , T8 G/ n) ], U% O/ i6 d# Q
- }# j: U3 }$ M% J0 q" Z, |) q* m# N
- if(scucss==true)
g7 _* Z; v, T. t - {6 `1 o/ D4 K( U4 o
- SI=new StudentInf(xm,xh,bj,cj); ~2 b- V: E& [& S
- Insert(SI);2 B5 D# X1 M- _+ \$ q
- }
8 ?: D4 V, ^/ m' G8 @7 I9 |4 W2 y% ` - }1 ?% N5 M7 P1 m% r
- if(e.getActionCommand()=="修改")% K3 T3 |, X9 P) ?7 l+ ^# ~
- {. o5 `( [% P+ K+ S
- xm=input1.getText();% ?6 ~+ N0 P @0 V1 \! c! d
- xh=Integer.parseInt(input2.getText());( D* y# G7 N' l3 o& W
- bj=input3.getText();
: |& g9 ]) {5 C0 Y. i) j% y/ k; O' i - cj=Integer.parseInt(input4.getText());
$ y& x7 d$ q$ h" X7 `4 u - SI=new StudentInf(xm,xh,bj,cj);
5 \. e: S: f' X- E - StuInf.setElementAt(SI, mid);) X1 V. R8 `6 y
- btn3.setEnabled(false);
$ D! W. G( m% a; M- I- F - }
" X! m3 ?( e2 \2 J& x - if(e.getActionCommand()=="删除")
; w! |7 G' J5 G+ k7 S - { |9 w" N" J6 F9 t
- StuInf.removeElementAt(mid);
p1 W3 j0 u3 i - btn4.setEnabled(false);: H6 E- E4 n) p m& W0 t6 g
- input1.setText("删除成功");
, f3 O1 }/ n+ t- l K7 O - input2.setText("");
$ y. X. x, X% k. }9 |) O F6 a* `9 G- T - input3.setText("");" B* |8 z, q, C' F: `: K# h
- input4.setText("");
$ \0 A9 O, k) q7 V - }
, o4 N; m/ ]& T1 {4 r5 O - if(e.getActionCommand()=="查询")3 a6 R6 s8 f4 i5 z5 d: S. i
- {; [/ o5 A" L0 C) X& d, o: n- E2 Y
- boolean right=true;
1 {6 A- I+ r* B7 l+ z! `9 c) o - try
9 F5 {4 `: l" ^( H) ^' t - {
, G8 ^: d& y" h" p - xh=Integer.parseInt(input2.getText());% S9 i0 T( Q9 Y: E/ Z7 H. z( N1 V
- }
( e$ C! u. L& u8 h4 X - catch(NumberFormatException m); v0 g. w. ^: K1 T
- {( h5 k: B: H* |( U6 G4 }: b% ^
- input2.setText("学号为空或格式错");% W4 Q5 k0 |: c. y7 D* @0 v" q( Y
- right=false;
, f9 Z4 C. b, b% v - } 6 H' v2 Y/ r/ v" x
- if(right==true)
! a# C7 P! u$ r" p( v; l* h - {3 ?$ j' K& @/ z0 h7 g
- search(xh);4 n/ }0 X1 ]3 R+ b# a/ n
- btn3.setEnabled(true);
- i4 `+ ?, ~8 J3 Y - btn4.setEnabled(true);
5 X( ^8 {% V' E" Z; x - }( |8 T: {, s! Z1 S: g4 f, D$ Y
- }
( p( f' Z. ]% o6 |* a! U - $ |* O/ S0 _- \, Y/ n
- }
0 l4 \( `& k; C# m - //查找方法
9 o& w+ }. `; x8 M - public void search(int k)+ S: _2 Q* }9 u& p
- {
: k: N" a Q3 i2 |% _5 V - boolean exist=false;: k$ _2 p, ^8 E- P
- int low=0;
1 W0 P* U3 V6 r. ^- V4 ~ - int high=StuInf.size()-1;
: ^0 r" Z" o4 D: z! H3 ~ - while(low<=high)
* o+ u, E2 Z' k9 J' V - {$ r6 \/ F7 r" S
- mid=(high+low)/2;
8 W; [- T- `, t+ ] - StudentInf a1=(StudentInf) StuInf.elementAt(mid);' R* f+ M1 U* I o( w0 V
- if(a1.getStuNo()==k)8 S8 o4 ?9 g# l. {' |: D
- {
- ~; A( }% I/ ? l/ A5 ?2 }. Q - SI=(StudentInf) StuInf.elementAt(mid);
5 v+ Z% a) l9 }# Y5 _ - String x = String.valueOf(SI.getStuNo());: j4 {( w# S* }: V
- exist=true;" G% Q; c/ ]1 f7 o/ m
- input1.setText(SI.getname());8 j3 I# C" L& R
- input1.selectAll();3 d$ }% Y- n! D' A" z/ m5 F. t
- input2.setText("0"+x);
$ F, z9 v! n; b) b - input3.setText(SI.getClassNo());
: X. \" [. u3 F3 \ - if(SI.getLevel()==-1)% p G5 K" S5 D F( U
- input4.setText("未参加考试");' B3 Z+ q h7 a T5 T4 N' j( [1 [. j9 n
- else0 D0 m# K/ Z- ?: T9 a; x1 K
- {
8 Z2 o% W$ |" I& P1 ~ - String y = String.valueOf(SI.getLevel());
/ b! f5 U& E, F9 h( u - input4.setText(y);" u& K' R. d$ Z' j
- }# b+ r% Q) N2 g8 l
- break;
: l* m/ b+ O/ x4 s - }' b0 K& t' e. w, \5 l( d1 |
- else if(a1.getStuNo()<k), v- V9 U& C0 n
- low=mid+1;) {0 M3 F3 r" Y# P4 [
- else
, P( g% I% x: R6 f- g - high=mid-1; ; ]/ N: K3 O2 p/ A8 [
- }
4 v8 G, \* b( f) q, u - if(exist==false)
5 |& C) m! z0 B3 ]; S, I - {
) A$ e+ Q8 e I* X - input1.setText("无此学号学生信息");
5 I! F) z4 q8 d2 K& J - input1.selectAll();
4 J$ C4 `; X2 o - }: l2 D' @7 ?8 `. s& C# N4 U
- }$ d4 l- r" w3 A V
- //添加方法0 a( l; s( t0 ]6 r
- public void Insert(StudentInf q)
3 f- ^8 G/ P, Q; ]1 @9 [ - {
! R4 S6 m, D- I - int i=0;
" O1 S+ H+ K1 E; m- C. M6 y7 f9 W8 J -
8 ]4 W& w; D) {8 a m: I' p' ? - if(StuInf.isEmpty()==true)
& L4 V" w$ o4 Z9 y( R( y - {
3 e7 b3 l. M {' c - StuInf.addElement(q);
8 g; y3 _* E8 m n - input1.setText("");
( L4 V. Y$ `& a - input2.setText("");' u' l/ c6 V- N" A% N7 z5 ?
- input3.setText("");
" t6 @3 o5 d6 Z: u7 @1 z# P - input4.setText("");
5 i/ G2 @4 W) \2 S - }- v0 G+ X$ i2 j( Q( F
- else
) o7 v$ w% _5 L+ n8 J: R' J0 H% t - {: S* u! |$ u0 Y7 g4 @: D; W7 n1 P$ h
- StudentInf xh;* \9 M E1 V5 F5 a7 `! Y$ X; @/ h
- xh=(StudentInf) StuInf.firstElement();
7 @1 s) v+ x. @, R; g3 h4 V( Q2 w - while(xh.getStuNo()<q.getStuNo())' u" S5 O; Y6 ]& e
- {1 y- {' @: o4 G Y) V- c
- i++;
' r; ~( s9 l3 u1 D) S - if(i<StuInf.size())
y& z1 O1 y( ~- X2 f; V - xh=(StudentInf) StuInf.elementAt(i);* z! f' I9 [+ \1 u& x1 X( o9 Z
- else
6 N7 z. Z! s% \8 A - break;
C: x0 n' v. y" M2 |1 c/ D - } 0 g, q* |! z" @- n# `* o
- if(xh.getStuNo()==q.getStuNo())" Q; V5 p/ P7 d7 T1 `
- {5 F+ s) t# _9 P$ w
- input2.setText("此学生信息已存在");
. ?4 q/ @+ g# H. \ - input2.requestFocus();4 F; {: ~, Z- f' m# [
- input2.selectAll();' Y4 R& W; R1 \1 Q; a+ J* y, P
- }) @: K; J2 E( A: g. L4 O
- else & f) P/ J+ E0 m9 e' X
- {3 D7 M1 K2 ?+ \0 t
- StuInf.insertElementAt(q,i);
6 p6 x( k+ r% f# C- C6 ^6 n) r - input1.setText("");4 p% X% U% I) }( s; p' `
- input2.setText("");
, W7 Z6 r- l6 t- r: }, T$ Z" x - input3.setText("");
, s2 N# b9 [. o- o - input4.setText("");) S, X7 I$ d& [6 N0 f
- } w8 z; k' i3 _, J) {5 \( A
- } # t( d. e; P+ @& n2 n
- }# m7 R8 h' Z) ?( t6 j
- * _6 r8 J' G `. s! c
- //异常类
* `: U- _4 E ^# C8 j5 A - class OverException extends Exception
* l" W9 c [7 E2 X% q0 D - {
; r0 c' a1 [! v- v6 S4 B& @4 D3 Y - String over;+ S G$ P: e B/ G3 Q: E3 y
- }
( c4 f) U# ~0 Y% F( i - class EmptyException extends Exception
& A' S2 ^( h* }' c5 f' ^/ _. i) B - {
2 v$ ]( N& V( B( X2 T" A - String empty;
( \, {& o& i& M - }7 J6 _" W. e; U% Z+ |: `
- void XingMing() throws EmptyException; V- Q8 y. m* n% J8 r
- {
* `/ L! u! X8 e, T7 F; R H. b - if((input1.getText()).equals(""))
% Z4 u8 d+ X/ w. Q3 {) x# k - throw (new EmptyException());# e6 x+ S" Z9 k9 s) w3 e
- else
/ Y5 ^; w6 M8 ]# n0 c0 @% Q& R2 D8 t$ i - xm=input1.getText();
5 |7 ] M- \3 V- c, V" Y - }" N. T/ L8 A8 H- T* T
- void ChengJi() throws OverException,EmptyException
9 Q0 M7 w* w0 Y* E% D+ [- ?) A4 N+ k: E - {" W, Y" ?) R z4 K/ K. U
- if((input4.getText()).equals(""))
# X& K3 M) B! E6 p - throw(new EmptyException());
& U' g- u; K9 n; ~2 ^. i4 o - else5 z! g8 _% A n/ M' z
- cj=Integer.parseInt(input4.getText());9 O& U& [! u( i( w X1 B
- if(cj<0||cj>100)
9 b1 V7 F- o7 E0 F) E - throw (new OverException());
; _/ {1 l- A! `$ v: }& o - }& }2 j N2 Z* p+ G1 v: l2 g
-
- Q8 B8 W! X b" e$ W7 L - //学生信息类
% N d, Z! n# Z) b: G - public class StudentInf
3 ~% m7 w2 I( S# _6 e$ L& p+ [ - {
" h9 S' X3 d* X! ~ - private String name;
' K. d+ X/ d$ h - private int StuNo;
8 T( [# e! g8 P$ }6 {+ F - private String ClassNo;
0 k: B4 S4 S3 `( ^, m" F# z - private int Level;: g0 n4 C. U$ `
- StudentInf(String xingming,int xuehao,String banji,int chengji): @. J* `; F- ^+ Z+ S6 G8 m
- {" T3 x" P2 @4 B, v6 ~
- name=xingming;- i# P5 l2 B) b# u
- StuNo=xuehao;
/ u% } f* o5 n; D: u% Q6 { - ClassNo=banji;8 Y4 ~7 _- u$ k5 v; l$ F" ]
- Level=chengji;
% O0 Y" e/ L2 m; w* l& V. A - }/ ^0 [" b9 D2 _) |( W+ @" M6 B
- public int getStuNo()
" j4 j; p2 v3 k; C! `$ M! v. E - {
, L) ^( O7 V2 V' p% R - return StuNo;6 @% l1 Y* T. r/ F* g
- }; s* ?6 y: Z4 z+ h) X
- public String getname()* E X; I$ Z" T z3 H3 r% b' U
- {
& p5 |" t: |0 X9 L \. t! e& S0 u - return name;* ^5 F b3 C4 u9 f7 J4 u
- }
w, O% ~/ [0 X$ N; [) \ - public String getClassNo()
! q. p a# C* |$ t$ P; A$ [# a - {& P# a' p8 s7 ?
- return ClassNo;
" f" u7 n% w- d( v1 \! k - }7 ^1 t/ i0 W. t8 _+ O
- public int getLevel()" L0 d3 w S6 ^: ~8 P
- {' b$ a( k2 F! U5 @, ~
- return Level;1 c4 R, ^6 R* g- C7 O& A
- }
- }' _8 u, s T/ U. G - }
7 l1 A% Z4 F; K4 H$ `: g
, Q4 k7 N: ~- ]1 j- }
复制代码 4 X8 L N( W* z# h5 I3 t
' d+ f" }* C0 o8 U. S7 q
|
|