javalz.docx
《javalz.docx》由会员分享,可在线阅读,更多相关《javalz.docx(16页珍藏版)》请在冰豆网上搜索。
![javalz.docx](https://file1.bdocx.com/fileroot1/2022-11/23/8e79f62e-c44b-4864-a0d6-598004db79b7/8e79f62e-c44b-4864-a0d6-598004db79b71.gif)
javalz
Java程序设计实验报告
专业:
信息与计算科学
班级:
0701班
学号:
0708060102
姓名:
吝芝
2009年12月11日
一、功能描述
编程实现一个学生成绩管理系统的图形界面,在系统中可以添加学生成绩信息,修改学生成绩信息,或者删除学生成绩信息,也可以查询学生成绩信息。
二、类描述
类名
属性
方法
备注
Students
StudentInfSI
publicvoidinit()
Stringxm
Stringbj
inti
intj
publicvoidactionPerformed(ActionEvente)
intxh
intcj
staticintmid
Labelprompt1
publicvoidsearch(intk)
Labelprompt2
Labelprompt3
Labelprompt4
Labelprompt5
publicvoidInsert(StudentInfq)
TextFieldinput1
TextFieldinput2
TextFieldinput3
voidChengJi()
TextFieldinput4
Buttonbtn1
Buttonbtn2
voidXingMing()
Buttonbtn3
Buttonbtn4
OverException
Stringover
EmptyExcepyion
Stringempty
StudentInf
privateStringname
StudentInf(Stringxingming,intxuehao,Stringbanji,intchengji)
privateintStuNo
publicStringgetname()
publicintgetStuNo()
privateStringClassNo
publicStringgetClassNo()
publicintgetLevel()
privateintLevel
附:
源程序代码框架
importjava.applet.Applet;
importjava.awt.*;
importjava.awt.event.*;
importjava.util.*;
importjava.lang.*;
publicclassStudentsextendsAppletimplementsActionListener{
…………………………………………
publicvoidinit(){
…………………………………………
}
publicvoidactionPerformed(ActionEvente){
…………………………………………
}
publicvoidsearch(intk){
…………………………………………
}
publicvoidInsert(StudentInfq){
…………………………………………
}
classOverExceptionextendsException{
Stringover;
}
classEmptyExceptionextendsException{
Stringempty;
}
voidXingMing()
…………………………………………
voidChengJi()
…………………………………………
publicclassStudentInf{
…………………………………………
}
StudentInf(Stringxingming,intxuehao,Stringbanji,intchengji){
…………………………………………
}
publicintgetStuNo(){
returnStuNo;
}
publicStringgetname(){
returnname;
}
publicStringgetClassNo(){
returnClassNo;
}
publicintgetLevel(){
returnLevel;
}
}
}
三、界面设计与事件处理
publicclassStudentsextendsAppletimplementsActionListener{
…………………………………………………
Labelprompt1=newLabel("学生管理系统");
Labelprompt2=newLabel("用户:
");
Labelprompt3=newLabel("密码:
");
Labelprompt4=newLabel("班级:
");
Labelprompt5=newLabel("成绩:
");
TextFieldinput1=newTextField(8);
TextFieldinput2=newTextField(8);
TextFieldinput3=newTextField(8);
TextFieldinput4=newTextField(8);
Buttonbtn1=newButton("登录");
Buttonbtn2=newButton("增加");
Buttonbtn3=newButton("修改");
Buttonbtn4=newButton("删除");
publicvoidinit(){
setLayout(newGridLayout(8,3));
add(newLabel());
add(prompt1);
add(newLabel());
add(prompt2);
add(input1);
add(newLabel());
add(prompt3);
add(input2);
add(btn1);
add(prompt4);
add(input3);
add(newLabel());
add(prompt5);
add(input4);
add(newLabel());
add(btn2);
add(btn3);
add(btn4);
prompt4.setVisible(false);
prompt5.setVisible(false);
input3.setVisible(false);
input4.setVisible(false);
btn2.setVisible(false);
btn3.setVisible(false);
btn4.setVisible(false);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
}
publicvoidactionPerformed(ActionEvente){
if(e.getActionCommand()=="登录"){
Stringa,b;
a=input1.getText();
b=input2.getText();
input1.setText("");
if((a.equals("lz")==true)&&(b.equals("02")==true)){
prompt2.setText("姓名:
");
prompt3.setText("学号:
");
prompt4.setVisible(true);
prompt5.setVisible(true);
input3.setVisible(true);
input4.setVisible(true);
btn2.setVisible(true);
btn3.setVisible(true);
btn4.setVisible(true);
btn3.setEnabled(false);
btn4.setEnabled(false);
btn1.setLabel("查询");
input1.setText("登录成功");
input1.selectAll();
}
else
input2.setText("用户名或密码错");
}
if(e.getActionCommand()=="增加"){
booleanscucss=true;
try{
XingMing();
}
catch(EmptyExceptionas){
input1.setText("姓名不能为空");
scucss=false;
}
try{
xh=Integer.parseInt(input2.getText());
}
catch(NumberFormatExceptionm){
input2.setText("学号为空或格式错");
scucss=false;
}
bj=input3.getText();
try{
ChengJi();
}
catch(EmptyExceptionas){
cj=-1;
}
catch(OverExceptiondd){
input4.setText("应在0-100间");
scucss=false;
}
catch(NumberFormatExceptioncm){
input4.setText("成绩应为数据");
scucss=false;
}
if(scucss==true){
SI=newStudentInf(xm,xh,bj,cj);
Insert(SI);
}
}
if(e.getActionCommand()=="修改"){
xm=input1.getText();
xh=Integer.parseInt(input2.getText());
bj=input3.getText();
cj=Integer.parseInt(input4.getText());
SI=newStudentInf(xm,xh,bj,cj);
StuInf.setElementAt(SI,mid);
btn3.setEnabled(false);
}
if(e.getActionCommand()=="删除"){
StuInf.removeElementAt(mid);
btn4.setEnabled(false);
input1.setText("删除成功");
input2.setText("");
input3.setText("");
input4.setText("");
}
if(e.getActionCommand()=="查询"){
booleanright=true;
try{
xh=Integer.parseInt(input2.getText());
}
catch(NumberFormatExceptionm){
input2.setText("学号为空或格式错");
right=false;
}
if(right==true){
search(xh);
btn3.setEnabled(true);
btn4.setEnabled(true);
}
}
}
//查找方法
publicvoidsearch(intk){
booleanexist=false;
intlow=0;
inthigh=StuInf.size()-1;
while(low<=high){
mid=(high+low)/2;
StudentInfa1=(StudentInf)StuInf.elementAt(mid);
if(a1.getStuNo()==k){
SI=(StudentInf)StuInf.elementAt(mid);
Stringx=String.valueOf(SI.getStuNo());
exist=true;
input1.setText(SI.getname());
input1.selectAll();
input2.setText("0"+x);
input3.setText(SI.getClassNo());
if(SI.getLevel()==-1)
input4.setText("未参加考试");
else{
Stringy=String.valueOf(SI.getLevel());
input4.setText(y);
}
break;
}
elseif(a1.getStuNo()low=mid+1;
else
high=mid-1;
}
if(exist==false){
input1.setText("无此学号学生信息");
input1.selectAll();
}
}
//添加方法
publicvoidInsert(StudentInfq){
inti=0;
if(StuInf.isEmpty()==true){
StuInf.addElement(q);
input1.setText("");
input2.setText("");
input3.setText("");
input4.setText("");
}
else{
StudentInfxh;
xh=(StudentInf)StuInf.firstElement();
while(xh.getStuNo()i++;
if(ixh=(StudentInf)StuInf.elementAt(i);
else
break;
}
if(xh.getStuNo()==q.getStuNo()){
input2.setText("此学生信息已存在");
input2.requestFocus();
input2.selectAll();
}
else{
StuInf.insertElementAt(q,i);
input1.setText("");
input2.setText("");
input3.setText("");
input4.setText("");
}
}
}
//异常类
classOverExceptionextendsException{
Stringover;
}
classEmptyExceptionextendsException{
Stringempty;
}
voidXingMing()throwsEmptyException{
if((input1.getText()).equals(""))
throw(newEmptyException());
else
xm=input1.getText();
}
voidChengJi()throwsOverException,EmptyException{
if((input4.getText()).equals(""))
throw(newEmptyException());
else
cj=Integer.parseInt(input4.getText());
if(cj<0||cj>100)
throw(newOverException());
}
//学生信息类
publicclassStudentInf{
privateStringname;
privateintStuNo;
privateStringClassNo;
privateintLevel;
StudentInf(Stringxingming,intxuehao,Stringbanji,intchengji){
name=xingming;
StuNo=xuehao;
ClassNo=banji;
Level=chengji;
}
publicintgetStuNo(){
returnStuNo;
}
publicStringgetname(){
returnname;
}
publicStringgetClassNo(){
returnClassNo;
}
publicintgetLevel(){
returnLevel;
}
}
}
四、运行结果
五.实验心得
Java程序设计语言是十分重要的学科,学习Java语言,重要的是要多看、多想、多练习。
通过本次实验,对图形用户界面的设计及事件处理有了更深一步的理解,虽然刚开始时遇到了一些问题,但通过查资料,阅读课本,终于把报告做完了,但对于调式程序还是有一定的难度。
这次试验让我知道,平时要多了解算法,多做程序,可以提高自己的设计和编程能力。