Java Applet实验报告Word文档格式.docx
《Java Applet实验报告Word文档格式.docx》由会员分享,可在线阅读,更多相关《Java Applet实验报告Word文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
super.paint(g);
g.setColor(Color.blue);
//callsuperclass'
spaintmethod
g.drawLine(10,10,100,10);
g.drawLine(10,20,100,20);
g.drawLine(10,30,100,30);
g.drawLine(10,40,100,40);
g.drawLine(10,50,100,50);
g.drawLine(10,60,100,60);
g.drawLine(10,10,10,60);
g.drawLine(20,10,20,60);
g.drawLine(30,10,30,60);
g.drawLine(40,10,40,60);
g.drawLine(50,10,50,60);
g.drawLine(60,10,60,60);
g.drawLine(70,10,70,60);
g.drawLine(80,10,80,60);
g.drawLine(90,10,90,60);
g.drawLine(100,10,100,60);
}
}
问题:
如何改变窗口的大小?
答:
在编译的过程中提示applet标记需要宽度。
但是定义了以后用appletviewerMyApplet.html
时却并未显示,setSize();
这个函数并不是applet的函数,通过XX查到用voidreset();
函数,这样就可以变化窗口的大小了。
任务2.编写一个JavaApplet小应用程序,该程序随机产生一个圆半径,然后显示该圆的半径、周长和面积。
最后,说说你程序设计中体现的面向对象的编程思想。
提示:
使用方法Math.random()可以产生一个双精度型(0,1)的随机数。
importjava.awt.*;
classyuan{
privateintbanjing;
privateintmian;
privateintzhouchang;
publicvoidyuan(intx)
{banjing=x;
publicvoidmianji()
{
mian=(int)3.14*banjing*banjing;
publicvoidsuanzhouchang()
{zhouchang=2*(int)3.14*banjing;
publicintgetbanjing()
{returnbanjing;
publicintgetmianji()
{returnmian;
publicintgetzhouchang()
{returnzhouchang;
}
publicvoidpaint(Graphicsg){
this.resize(1000,1000);
yuanyuan1=newyuan();
doubler=Math.random()*300;
inta=(int)r;
yuan1.yuan(a);
yuan1.mianji();
yuan1.suanzhouchang();
intbanjing=yuan1.getbanjing();
g.setColor(Color.red);
g.drawString("
半径:
"
+yuan1.getbanjing(),25,25);
面积:
+yuan1.getmianji(),25,55);
周长:
+yuan1.getzhouchang(),25,85);
g.fillOval(120,55,banjing,banjing);
任务3.建立JavaApplet小应用程序,随机产生5个数,然后以条形图的形式来表示这5个数。
publicclassGraphicsTesterextendsJApplet{
intshu1=(int)(Math.random()*500);
//不要放进paint函数中否则移动就会
intshu2=(int)(Math.random()*500);
//重新画图
intshu3=(int)(Math.random()*500);
intshu4=(int)(Math.random()*500);
intshu5=(int)(Math.random()*500);
Stringa=String.valueOf(shu1);
Stringb=String.valueOf(shu2);
Stringc=String.valueOf(shu3);
Stringd=String.valueOf(shu4);
Stringe=String.valueOf(shu5);
//重载JApplet类的paint方法,参数是Graphics类的对象,是由浏览器传递过来的
publicvoidpaint(Graphicsg){
super.paint(g);
//调用其超类JApplet类的paint方法
this.resize(500,500);
g.setFont(newFont("
SansSerif"
Font.BOLD,12));
g.drawString(a,0,500-shu1-20);
g.fillRect(0,500-shu1,100,shu1);
g.setColor(Color.yellow);
g.drawString(b,100,500-shu2-20);
g.fillRect(100,500-shu2,100,shu2);
g.drawString(c,200,500-shu3-20);
g.fillRect(200,500-shu3,100,shu3);
g.setColor(Color.green);
g.drawString(d,300,500-shu4-20);
g.fillRect(300,500-shu4,100,shu4);
g.setColor(Color.black);
g.drawString(e,400,500-shu5-20);
g.fillRect(400,500-shu5,100,shu5);
}
任务4.(选作)使用JApplet实现某一门课程的分数录入系统,界面包括学号、姓名、班级、班号、成绩的输入域和一个录入按扭。
当用户单击录入按扭时,程序检查各输入域中是否有非空的数据,若有空域则抛出一个Exception对象;
同时程序检查成绩输入域,如果不是数值信息,或是负的数值信息,也抛出相应的异常。
在抛出异常之前应提供详细的信息说明,从而使使用该程序的用户了解所出现的问题,同时考虑上述异常出现时程序应该如何处理。
。
importjava.awt.event.*;
importjavax.swing.event.*;
importjava.lang.*;
classActionHandle{
publicJButtonbut1=newJButton("
显示"
);
publicJButtonbut2=newJButton("
重置"
publicJTextFieldtext1=newJTextField();
publicJTextFieldtext2=newJTextField();
publicJTextFieldtext3=newJTextField();
publicJTextFieldtext4=newJTextField();
//声明一个按钮
publicJLabellabnumber=newJLabel();
publicJLabellabclass=newJLabel();
publicJLabellabname=newJLabel();
publicJLabellabmark=newJLabel();
publicJTextArealabinfo=newJTextArea();
//声明一个标签
publicJPanelpan=newJPanel();
privateStringword;
publicActionHandle(){
Fontfnt=newFont("
Serief"
Font.ITALIC+Font.BOLD,12);
labnumber.setFont(fnt);
//设置标签的显示字体
labnumber.setText("
学号"
//设置默认显示文字
labclass.setFont(fnt);
labclass.setText("
班级"
labname.setFont(fnt);
labname.setText("
姓名"
labmark.setFont(fnt);
labmark.setText("
成绩"
labinfo.setLineWrap(true);
labinfo.setFont(fnt);
labinfo.setText("
分数系统欢迎您"
but1.addActionListener(newActionListener(){
publicvoidactionPerformed(ActionEvente)
{
if(e.getSource()==but1)
{
Stringstr1=text1.getText();
Stringstr2=text2.getText();
Stringstr3=text3.getText();
Stringstr4=text4.getText();
Stringstr5=panduan(str1,str2,str3,str4);
labinfo.setText(str5);
});
but2.addActionListener(newActionListener(){
{if(e.getSource()==but2)
text1.setText("
text2.setText("
text3.setText("
text4.setText("
labinfo.setText("
publicStringpanduan(Stringa,Stringb,Stringc,Stringd)
Stringword="
;
Stringword1="
Stringword2="
Stringword3="
Stringword4="
Stringword5="
if(a.length()<
=0)
{word1="
学号不能为空"
else
if(b.length()<
{word2="
班级不能为空"
if(c.length()<
{word3="
姓名不能为空"
if(d.length()<
{word4="
成绩不能空"
elseif(isNumeric(d))
成绩只能为正数"
else{word5="
您的信息是"
+"
+a+"
+b+"
+c+"
分数"
+d;
word=word1+word2+word3+word4+word5;
returnword;
publicstaticbooleanisNumeric(Stringstr){
for(inti=str.length();
--i>
=0;
){
if(!
Character.isDigit(str.charAt(i))){
returntrue;
returnfalse;
publicclassJAppletaextendsJApplet{
publicvoidinit(){
this.setLayout(null);
ActionHandlet1=newActionHandle();
t1.labnumber.setBounds(5,5,60,20);
t1.labclass.setBounds(5,30,60,20);
t1.labname.setBounds(5,55,60,20);
t1.labmark.setBounds(5,80,60,20);
t1.text1.setBounds(65,5,100,20);
t1.text2.setBounds(65,30,100,20);
t1.text3.setBounds(65,55,100,20);
t1.text4.setBounds(65,80,100,20);
t1.but1.setBounds(10,110,60,20);
t1.but2.setBounds(80,110,60,20);
t1.labinfo.setBounds(5,130,200,100);
this.add(t1.labnumber);
this.add(t1.text1);
this.add(t1.labclass);
this.add(t1.text2);
this.add(t1.labname);
this.add(t1.text3);
this.add(t1.labmark);
this.add(t1.text4);
this.add(t1.but1);
this.add(t1.but2);
this.add(t1.labinfo);
this.add(t1.pan);
这个实验题目里面最难得部分就是原来运行JFrame现在变成了JApplet。
原本都是向JFrame这个容器里面加东西,可是现在加上会出错,网页右上角会显示一个小红叉,大概意思是不能把JFrame里面添加容器。
这样就出现了很多错误和改变,后来把voidinit()
当成JFrame直接添加这些就没有出错。
思考题:
1.什么是小应用程序?
它与独立应用程序有什么区别?
小应用程序必须配合html或者用appletviewer来查看,独立程序则不用。
2.简述小应用程序的生命周期。
一开始也就是打开浏览器的时候下载用init()方法初始化,之后start进入运行,
这时候比如最小化一类活动会stop();
退出浏览器则会调用destory()方法。
3.repaint()方法的功能是什么?
repaint()通过调用线程再由线程去调用update()方法清除当前显示并再调用paint()方法进行绘制下一个需要显示的内容.这样就起到了一种图片的交替显示从而在视角上形成了动画。