该用户从未签到
|
- import java.applet.Applet;/ y2 z* Z7 [8 i: e
- import java.awt.*;
{3 A5 }, P) R# Y4 o, E' k4 S. _ - import java.awt.event.*;6 C6 C. }8 P8 a# S8 O
- import java.util.*;
+ M* g0 u/ S# `9 J1 x8 e - import java.lang.*;/ b. y' ?* @# H* t, E# z- U/ p. {
( e9 K& b5 C7 ~# g4 |- public class Students extends Applet implements ActionListener
& J5 }5 H) D) {0 Q: A/ x - {
6 J! e' U" q$ ~* z6 X7 ~6 L - Vector StuInf=new Vector();
5 G0 ^6 @* o3 S6 A9 s - StudentInf SI;/ e/ k$ \% v7 T2 Z1 e" [
- String xm;
9 ^* d# g0 n3 a1 e - String bj;
; D5 i* V7 H9 u2 Q - int i,j,xh,cj;( e% H- F, k4 {# s
- static int mid;
, g' H7 t: n* U9 ^8 h# ` - Label prompt1=new Label("学生成绩管理系统");8 |, d/ f9 D( Q5 ]
- Label prompt2=new Label(" 用户:");
4 [9 ^" N" H* c% ^' [ - Label prompt3=new Label(" 密码:");% `% o! g" ?* n0 \
- Label prompt4=new Label(" 班级:");
2 G" G+ l R( N4 Z9 \ - Label prompt5=new Label(" 成绩:");% ?) K% s. P9 c' U6 g; |
- TextField input1=new TextField(8);
1 r2 N" i4 _' Q: p - TextField input2=new TextField(8);! l7 E9 M" P2 l& k- E
- TextField input3=new TextField(8); o, X) e: ]4 U4 [7 A( K
- TextField input4=new TextField(8);
- r. g9 h. `9 x: O - Button btn1=new Button("登录");6 S! a7 U% R7 t4 A, T
- Button btn2=new Button("增加");
3 h6 Q$ R/ m, ]8 J3 ?$ k# ^8 Y; P - Button btn3=new Button("修改");
; L& m. n7 _2 c; ` - Button btn4=new Button("删除");
* _( Y4 |$ ]4 @' H -
! _( q& M7 ^2 c5 b5 v - public void init()4 D/ u2 J- G5 c
- {
' R r6 L: o0 {0 C - setLayout(new GridLayout(6,3));7 ^# m2 C9 O" H2 C
- add(new Label());; W- L$ k/ b n, [) M1 U
- add(prompt1);
: M$ V0 ^! ]3 N7 f( n0 j - add(new Label());
1 @1 s8 Q4 W+ T. `5 Z* w - add(prompt2);
( {$ h+ {+ ]4 \% y! p4 H6 z - add(input1);9 x5 j* H- k8 B m
- add(new Label());- e; q0 W/ Q$ E" w1 J' b) _
- add(prompt3);
0 m0 s. _1 ^" j. G1 V C3 S! ~ - add(input2);, \& @4 Y) {4 K9 H5 O" d- ], l9 H
- add(btn1);
3 k, S; H% f& s; l6 p/ i/ L& W. q - add(prompt4);- N+ |: z# O! V5 U% i8 J! u
- add(input3);
+ I: [3 u" n4 ~/ {4 f' f! d p - add(new Label());
* S' S' f8 T+ Z% I. D% y! ] - add(prompt5);7 w Q! \5 r" T8 i% W7 ?( ~( }
- add(input4);9 |8 F/ W" l |: M1 i& \
- add(new Label());
) c* H1 z1 m# q& \3 D& d - add(btn2);
) I' j4 g) ^1 q% S( Z3 P6 i - add(btn3);
2 }3 B& U0 c& w# a- H' e - add(btn4);
m w/ F* P* f - prompt4.setVisible(false);1 B" `' x6 @4 S; h, Y9 C
- prompt5.setVisible(false);" t z8 V( q. F: o0 C
- input3.setVisible(false);
+ H0 f. A& T3 _3 K - input4.setVisible(false);
9 b; t* _8 D) R1 B- ^( ] - btn2.setVisible(false);
) D' N7 u0 S( d0 h5 E: l! p - btn3.setVisible(false);
' D* J8 H H! o2 a7 x% _ - btn4.setVisible(false);
; U3 i" [4 o5 N- \9 @" F - btn1.addActionListener(this);! e: Y1 ]+ f' B }# K/ A# \
- btn2.addActionListener(this); ], L- ]# a6 n6 |
- btn3.addActionListener(this);- n4 ?, f* I3 O5 I1 C
- btn4.addActionListener(this);9 W, l8 [" G- j, W% B% D- W
- }
# v4 Y3 s. R7 p" W+ L - public void actionPerformed(ActionEvent e)
, G" i" g M' v6 c# p2 u2 x - {; P% f9 y! O, Q5 G1 \2 J" }% ^
- if(e.getActionCommand()=="登录")
" a4 ^: V7 \2 P$ D7 C0 Z0 a m$ a - {, P/ L7 a- m' f# b, |1 _( Y- J
- String a,b;- A) K n) a, _) s+ g& t6 C
- a=input1.getText();% a* Y8 i) W1 |' E& w6 J
- b=input2.getText();7 ?6 O7 a! }* _! B E
- input1.setText("");) `8 R: d- l6 ?! W, f. B0 H
- if((a.equals("12")==true)&&(b.equals("12")==true))
# m5 b p9 [( E$ j2 v: U+ U - {, a$ ^+ J" S5 J4 ^* M4 [
- prompt2.setText(" 姓名:");
\7 L5 U- v; G4 e5 a) q - prompt3.setText(" 学号:");
& c! x0 e* \% |2 _: H1 o1 Q+ u2 y/ y - prompt4.setVisible(true);& A: P1 O3 L2 w' g
- prompt5.setVisible(true);
) F" `% i: b. r4 N/ r - input3.setVisible(true);
3 B5 J$ T. U9 q7 \& o3 h - input4.setVisible(true); `# m! i: c2 m
- btn2.setVisible(true);
: ^3 n) ~* V4 l& c9 { - btn3.setVisible(true);
, r; A h* A) |. h+ _ - btn4.setVisible(true);
0 Q+ x) k8 x9 s% g$ Z6 U - btn3.setEnabled(false);) z) R* c# U, x3 a
- btn4.setEnabled(false);0 [! L7 W# g2 F' \' u
- btn1.setLabel("查询");+ P' w" j) S4 G+ O
- input1.setText("登录成功");0 m" q! T# f' c: ~/ `
- input1.selectAll();
4 [7 D3 [% t, n* S. X1 J+ c1 U - }
l1 k9 L. F& c9 t+ `7 } - else
/ A% \& [$ T7 t# k9 L) N - input2.setText("用户名或密码错");. D5 u- f4 m2 P6 s7 `
- }
1 y) Y6 T8 y% ] - if(e.getActionCommand()=="增加")
. @3 e' t5 I: J - {' |; }4 k J& p( E; X, v
- boolean scucss=true;
" g& h6 j# u, y& ?8 @2 l$ R - try$ n( W' t" I% }3 }; o
- {
9 X) o9 P( ]% o! _7 k - XingMing();
3 }, B& W7 g* Q$ ^3 D9 \ - }8 P& v8 ~/ Q0 n2 \8 x1 V
- catch(EmptyException as)9 X' g/ F+ p: x1 {, _: D2 M
- {
: M% J* G1 E+ A8 W I& H) a - input1.setText("姓名不能为空");
1 p$ b2 x5 b; D - scucss=false;
f* X4 O* F. U: V - }: `) M5 J) u4 O3 r) a* }% N6 j
- try& d! G, u# K2 F. b& L
- {' o' d$ d0 K5 N/ |
- xh=Integer.parseInt(input2.getText());2 I: @ B& l& O- J
- }
1 D) c) e2 U6 @0 J; s - catch(NumberFormatException m)
% i( `' v2 j- Y3 C# P7 G2 m+ e - {3 L/ l, j5 G' c: f" a" H, |% _
- input2.setText("学号为空或格式错");0 [' r& k- N1 x4 P! V& g
- scucss=false;# n9 b$ p4 o2 Z2 Y
- } , D* R- o B4 y8 R5 A
- bj=input3.getText();
; e7 {7 m8 I. X" U3 F( L - try
5 Y# D9 x/ Y2 E: m4 v/ A+ w7 Y: _ - {9 [- T) `2 @! z1 \* B
- ChengJi();# {/ l# S5 y0 e" W. b' ]& g
- }
% u8 k% B$ p( Y5 a - catch(EmptyException as)4 E5 g" W) n' Z0 e! i% X+ y
- {7 S- o3 b) G. Q& q' c' r1 Y5 ]
- cj=-1;# Q/ B! a1 q! W# i
- }* q1 T( Y5 F) [- b
- catch(OverException dd)
7 V' K' a$ e2 L' J* }7 m - {
2 k+ x0 x c) J, c4 Q; P1 i - input4.setText("应在0-100间");
/ j9 [) h6 @) N; k% V - scucss=false;
7 l8 G) B7 H1 |! P& y - }
( _, ]6 ~ H) W) d* } - catch(NumberFormatException cm)8 X! v- J0 W+ r! e; E. J
- {
# Z8 k0 _# ^9 q5 {% X" c7 T - input4.setText("成绩应为数据");# D& H# I* V% C6 q8 ~4 A
- scucss=false;
: U6 W9 ^3 c6 h+ ]+ X c* d - }
3 `6 e: C7 l0 |* ^& e0 y- H - if(scucss==true)
+ R4 b& n2 r6 S+ U8 L. j - {
]9 c( |" k* w - SI=new StudentInf(xm,xh,bj,cj);: K1 f+ Z1 I6 D/ x8 [
- Insert(SI);
/ s3 d! s; s/ L0 P7 `& \5 ` - }. q1 _; `6 D6 q& H+ N- L: \0 e
- }
" R0 | Z3 \1 E, u: ^7 B: C - if(e.getActionCommand()=="修改")
& U; Q& B* ^* C4 o) X - {
! W" I* ^: \+ x. W' I - xm=input1.getText();
% L% u; C5 j( I/ ?9 x - xh=Integer.parseInt(input2.getText());
( z5 z" _: R6 O, x# c - bj=input3.getText();, n6 \# j7 |& q1 q @+ E
- cj=Integer.parseInt(input4.getText());) d" g1 r4 ]3 i, ]3 X
- SI=new StudentInf(xm,xh,bj,cj);# l' p$ j' C8 y% F( m
- StuInf.setElementAt(SI, mid);
I8 X( J( k% L; W" L" `2 H - btn3.setEnabled(false);
6 g" G; P# {- H - }
! ]8 b m) y3 l9 @6 E - if(e.getActionCommand()=="删除")/ q1 g0 S& f1 A# a
- { @' D: o8 T7 {( y% ]1 R) [
- StuInf.removeElementAt(mid);
/ R5 [* D+ W( u) \ - btn4.setEnabled(false);
8 {( y! i, T4 ]6 K: m% P" e: x - input1.setText("删除成功");8 k4 S4 b# b" k9 R
- input2.setText("");
' k% Q, g2 W( m' y* {# P - input3.setText(""); d K# [8 R/ W; F0 i" s% S6 O1 c
- input4.setText("");
^; I' r! ]# w - }
& m$ I* @+ w4 I; W" t7 a - if(e.getActionCommand()=="查询")
6 c# n N$ d' F2 ?" m3 O$ I1 B - {$ U1 H* b4 ~7 X8 ]8 q2 s
- boolean right=true;' H* E" R j- J7 N( ^( l' P0 o/ m
- try6 {3 M" d5 V) f, A
- {9 }2 j( S* E+ i2 S* e3 x
- xh=Integer.parseInt(input2.getText());
2 H; x/ |/ x3 M i2 b - }
6 E w' F2 g; F) f+ A - catch(NumberFormatException m)1 w4 Y) f3 ]6 G
- {) B e0 Z) ?4 ^( L+ d
- input2.setText("学号为空或格式错");
, G4 O0 r( K% p+ k `1 U - right=false;
% Y7 d, v6 o+ E1 B& }& ? o - } 1 B/ U" F4 _% q
- if(right==true)/ {% W- x0 ]" ^0 ~0 g
- {
\9 R2 W6 s9 U1 I2 K( F - search(xh);
- ~2 P6 a1 ?. K - btn3.setEnabled(true);
+ J7 M8 Q, D) }/ E% t* a - btn4.setEnabled(true);
+ e/ D, |& C' w - }2 t1 j" Q6 j+ u4 }, R( M. q" X3 x
- }7 }+ d; P& h$ @0 \0 l. `
- . n9 C% }9 h5 C
- }
# w4 Q2 q: k. \) z+ A `! J - //查找方法
+ Y# l3 q" b. p - public void search(int k)+ j Y7 l+ [: a8 L- q) m. j0 W* W
- {% s1 \6 u8 X9 h2 Z# Q
- boolean exist=false;- b! `. v7 S- j7 i* L! K2 i
- int low=0;
0 r: B; u6 z1 M! j3 _. E: A - int high=StuInf.size()-1;0 M5 L: O" \/ }1 E% b+ F
- while(low<=high)
5 ]& T# g/ ]1 ]9 n6 S8 y - {- l1 E8 D0 ?) R* V5 P+ I* q/ P4 p
- mid=(high+low)/2;
5 W7 |, w5 |2 t3 E7 N+ r6 P - StudentInf a1=(StudentInf) StuInf.elementAt(mid);
$ q9 a, p" [: G. X( C' ] - if(a1.getStuNo()==k)
, T5 A6 q# R/ V( r - {/ w! ~# p3 u, J* l# e
- SI=(StudentInf) StuInf.elementAt(mid);
6 N- g- o# w+ D$ m* L) \, q - String x = String.valueOf(SI.getStuNo());
9 E' `6 C; k2 g - exist=true;) G$ l8 T* C& Z* Y* m: d$ X' f! B
- input1.setText(SI.getname());
* K! ^! r7 a% L4 e3 } - input1.selectAll();) [/ r0 E# {. N3 b9 W
- input2.setText("0"+x);
3 G; Z% N# L' K `7 @ - input3.setText(SI.getClassNo());2 j+ d/ ?6 O2 \3 o, K! H* t
- if(SI.getLevel()==-1)
% j) Z# I2 o& J' u! n - input4.setText("未参加考试");3 ?- }0 g. O4 Y) C2 q
- else- b# p' X( V5 u8 b* _
- {
3 K5 g8 y) h$ j4 z5 Z8 Z - String y = String.valueOf(SI.getLevel());
/ w: w- ]) [: o# [. V8 v* M - input4.setText(y);: {, L5 o$ r6 q0 G8 I
- }
) V: |2 p! ?6 I4 l - break;
' _0 p9 {4 h; y% r - }
$ R# m: e7 j3 G1 y( t - else if(a1.getStuNo()<k)4 X' R( k8 c a4 u$ n5 |# |
- low=mid+1;4 a4 A- V( S$ W4 w$ ~
- else' C& ^. @/ M: l5 S
- high=mid-1; ) K: Y+ H& q6 R U- U3 k
- }/ D, ^8 i$ s+ @& [* l4 b8 ~+ [+ ]3 [
- if(exist==false)
! M; J& S/ w9 I; D, j5 t1 H - {
/ ~0 l. ^+ V) M9 N! c& [ - input1.setText("无此学号学生信息");% N' G* Z$ {8 y6 A6 n
- input1.selectAll();# _. E4 u3 q, ]) P. b' Q
- }
5 p3 R3 E$ A# n- J1 Q' o - }
0 }) g, {" R5 Y% I$ q/ v0 v - //添加方法+ a5 t1 K8 Z! B" t5 y
- public void Insert(StudentInf q)
; v5 o8 g7 o, O% L6 q) m! }5 I - {8 n- o, Q/ p4 c+ [
- int i=0;
" \4 v! o; u1 u -
$ c6 }3 S0 S! X" u- N - if(StuInf.isEmpty()==true) 8 n. A9 a+ D I( u9 X
- {; \/ Y; L! q! Q# J
- StuInf.addElement(q);8 L% G5 T+ I; R5 `
- input1.setText("");
+ Q0 ?& ]; {9 P1 e% T6 `7 a. p8 [$ E - input2.setText("");# Y5 {( u2 q& D# Q
- input3.setText("");" l% S! W- S% m* y& \
- input4.setText("");
d8 m' N8 L% k- ^% b+ u - }# I) R# n; [: e {3 F( F
- else5 v8 L: T. z; s5 t/ N( {
- {
% v0 d8 j; T' w - StudentInf xh;0 ^9 v. P7 M3 n- Z1 T* B& t+ J( N
- xh=(StudentInf) StuInf.firstElement();
$ i$ o( L5 {2 a- }; [ - while(xh.getStuNo()<q.getStuNo())
) ~, a/ i6 _5 p% `7 S- i3 e5 t - {/ @( A8 R, L E% h4 M! \( S6 i
- i++;
2 [* ]! Y( T! ~" M8 _! S - if(i<StuInf.size())
. ^; ^% I+ o0 i - xh=(StudentInf) StuInf.elementAt(i);% y: H) I# C$ V3 S; r
- else ( r H1 R* j" F+ c0 B
- break;! R7 E1 r- U! Y: [5 n
- } d6 a) [& F% @) x" d
- if(xh.getStuNo()==q.getStuNo())2 { E" j2 e1 ~% k2 R$ [6 I
- {/ Y" ~# P4 K; h9 o. |4 _, `" l3 R
- input2.setText("此学生信息已存在");- @( ]% A( M* F2 l' a
- input2.requestFocus();
' A" N/ W1 Y7 e _0 i4 n$ U - input2.selectAll();. R3 C% k3 Z) o% p
- }
- M& f) r0 X5 n6 @% }0 Q; U - else & e7 S# E' ?" {& x- H
- {4 i8 X3 z6 }3 m, `% i: B& G& \) A* t
- StuInf.insertElementAt(q,i);
1 d- N. z% u `* z - input1.setText("");
% D4 H0 D5 |6 i6 R; A; T - input2.setText("");: k' P; K( v" r/ {7 e% z
- input3.setText("");+ I2 m. |; g0 o6 j* R; {
- input4.setText("");1 b2 N+ l$ @ O. \ g5 W: f
- }# I7 X1 k5 D+ R
- } 4 \, y# t; _. ]' s
- }6 _6 B" f8 D# Q/ c4 {
- 2 A8 r3 C1 q, J& z# p3 b z! U
- //异常类
1 c. G; ` V+ {2 a* Y J* n) C% B9 j0 X - class OverException extends Exception
# H+ d, S4 t- l, r0 I; B* J - {
- x6 E' B! L% Q; O; F8 s) \ - String over;
# L. U# r2 w# Q+ S+ w/ O - } : q) ]- ]7 C5 N8 t- o
- class EmptyException extends Exception
% ~2 }2 C; ?4 B1 `9 Q - {& O0 x7 z/ l! s8 [" O
- String empty;
' Y$ c7 ^+ d+ o# j% | - }
7 {4 f) U! V) a4 E - void XingMing() throws EmptyException
& i. H5 ~. o6 N# l - {2 i. z/ k Y$ N( f9 q
- if((input1.getText()).equals(""))
$ V5 a% o( ] y/ D2 D# G - throw (new EmptyException());
$ J! `; [3 P0 T& `1 o - else" k5 t& M( `5 J: t7 V
- xm=input1.getText();
+ C$ ~$ C Z0 |0 p - }
) ~, d9 R9 i0 G/ Q. h4 p0 k - void ChengJi() throws OverException,EmptyException + Y! B7 J3 z0 x! b( e- I( X$ @
- {
% g2 y* N% z3 R# [% n2 [ - if((input4.getText()).equals(""))+ v: A" A2 ]: a5 P& K
- throw(new EmptyException());
1 w! ?. w$ m( ^. L; t) ]8 } - else
- X4 J" q0 p* C- V1 v+ B - cj=Integer.parseInt(input4.getText());3 W- T! f }( c) S
- if(cj<0||cj>100)
) E5 O. H6 f1 y* t/ ` - throw (new OverException());
& ]! N& r" i" |. a8 g5 ? - }
9 {/ y; O5 v" h' v - ) |2 u3 a/ z2 b3 X
- //学生信息类
) v h( H) q# B! A3 @ - public class StudentInf
( h2 M/ ?9 [( r: w - {, S4 _2 G7 |$ G* y7 W1 N' z
- private String name;
8 h& G: { N* H - private int StuNo;
+ V s5 z* D# O; U6 O - private String ClassNo;0 s, }* V! y3 }# U* i" W3 |5 O+ T- K. ?
- private int Level;
7 I6 P. A: q2 b, J - StudentInf(String xingming,int xuehao,String banji,int chengji)
$ V+ S: a, W7 b9 ~) F - {3 F, P" b5 o9 m: q5 G0 D7 F! ?
- name=xingming;, I) a- x7 ?3 c2 n: F0 c
- StuNo=xuehao;
6 B$ w- n' b3 g) b; f - ClassNo=banji;( D4 X- V1 e6 W5 ]- y) Z' J
- Level=chengji;
6 v3 k$ X" k' \- H+ R - }
1 e, I2 a$ S2 ?5 h, C - public int getStuNo()
" C) O' ?! w7 O" s - {
$ r' M7 t3 C1 ?. W7 _4 N0 ?& m& c1 C - return StuNo;* a6 c3 h* z" q5 b2 l* M
- }; r- E3 Z' O# @- w7 [6 j
- public String getname()
- ^. S: N) x2 {, m3 W) I" m - {
g/ v- r1 i- I; I$ @ \' ^5 ]2 a - return name;
, |+ S1 R$ @4 J8 B1 f - }
* h0 W. y# g4 c# } - public String getClassNo(), ?; ? C; J: s* k$ h0 B& j% K* ]
- {
0 @+ v8 a" u' B) D$ @ - return ClassNo;/ ~2 X4 D# ^- z% S C% j
- }
- h/ V; A+ K5 ?% g - public int getLevel()- |( R, F) T2 [ u( P, ?0 ^
- {
1 X6 y3 z! y- e) p6 L# V - return Level;& e% [ K4 `: k$ B: {# V& r
- }
) N( q- S7 R: O( U - }% x! k( u3 R( e
- 5 k, A8 Q3 |# F/ ]% h
- }
复制代码 t7 k! v' X* B
- R: |; V) U& R; M8 @5 U% W |
|