一个applet的编写与调试过程
applet与application虽然有很大的不同,但操作上基本同application的操作差不多,但编辑上还是有一些差别的。因此,这里也给大家举一个例子。这个例子稍为有点复杂,希望看的时候已经学过了一些java的基本知识了。这个例子的目的是要实现一个跑马灯的java applet程序,这个例子的效果如下:

1 选择新建菜单“new”,然后选择“new”下面的选项:applet on html,设置好名称与路径之后,按下确定按钮,系统将自动帮我们生成一两个文件,一个是applet的初始代码文件,另外一个就是html网页文件了。如下图:
2 在工程浏览窗口中双击applet1文件,这时就可以打开这个代码文件了。代码文件比较长,这里不列出,要查看代码,请点击这里。双击page1.html文件,也就打开这个html文件了,这个文件打开时,会显示如下图所示:
这个窗口里面有三个选项了,如果你用过frontpage,那么你将对这个非常的熟悉。这里就像frontpage一样,是一个网页编辑器,工具箱中也有你编辑网页所要的控件。不用多解释,看看source(源代码),打开后,有的可能显示的是该applet的内容。而有的可能显示的是代码,这与vj6的初始设定有关,在applet上面点击鼠标右键,选择“always view as text”后,就可以看到代码了,代码如下:
<applet
code=applet1.class
name=applet1
width=320
height=200 viewastext>
<param name=label value="this string was passed from the html host.">
<param name=background value="008080">
<param name=foreground value="ffffff">
</applet>
这个不用多解释,按照自己的要求改就行了。此时不能点击quick view进行快速预览,因为该applet还没有被编译成class文件。
3 此时将这个applet程序运行,按下f5键,直接运行,那么此时vj6会自动编译此程序,并且用默认的浏览器打开这个html文件并显示出来。
4 这个时候我们就要进行真正的添加代码的工作了。只有添加了必要的代码后,才能达到预期的目标嘛!请打开源代码文件applet1,双击就行!
先要做的工作,当然是设置变量,变量放在主类当中,如下:
font ft; //文字字体对象实例
string thestring, font, style; //移动的字符串、字体类型及样式
int size, speed; //文字的大小,移动速度
int xset, max, min;//文字所在位置的x坐标,右边界和左边界
定义好变量后,就要像给出的源代码一样,加入一些初始的代码了。
private final string param_string="string";
private final string param_font="font";
private final string param_style="style";
private final string param_size="size";
private final string param_speed="speed";
现在要做的事情就是从网页文件html中取得各种参数了,就像初始代码取得参数一样,这些可以参照初始代码而写出来!如下。
string param;
param = getparameter ( param_string);
if(param !=null) thestring= param;//取得字符串参数
param = getparameter ( param_font);
if(param !=null) font= param;//取得字型参数
param= getparameter ( param_size);
if (param !=null) size=integer. parseint ( param);//取得字提大小参数
param= getparameter ( param_style);
if(param!=null) style= param;//取得字型样式参数
param= getparameter ( param_speed);
if(param !=null) speed=integer.parseint (param);//取得文字移动速度参数
int vice_style = font.plain ;
if(style. equalsignorecase ( "bold"))
vice_style=font.bold ;
if (style.equalsignorecase ("italic"))
vice_style=font.italic ;//处理得到的字型参数,忽略大小写
ft=new font(font,vice_style,size);
fontmetrics fm= getfontmetrics(ft);//生成fontmetrics类对象实例
min= -fm.stringwidth (thestring);
max= getsize().width ;
offset= max;
接下来要做的事情,当然是要做最重要的工作了。编写程序的主要部分,字体滚动的过程。这部分的代码就不详细解释了。希望你已经学过java语法了。
public void paint(graphics g)
{
int yset=getsize().height ;//yset为输出字符串的y坐标变量
yset=(yset-size)/2;
g.setfont (ft);
g.setcolor (color.red);
g.drawstring (thestring,xset,yset+40);
//+40是用来调整输出时的高度用的。
xset--;
if(xset
}
public thread textmoverunnable=null;
public void start()
{
if(textmoverunnable==null)
{
textmoverunnable=new thread (this);
textmoverunnable.start ();
}
}
public void run()
{
while(true)
{
try{
repaint();thread.sleep (speed);
}catch(interruptedexception e) {}
}
}
public void stop()
{
if (textmoverunnable!=null)
{
textmoverunnable.stop();
textmoverunnable=null;
}
}
这部分代码应该加在与initform()并列的地方,可以在之前,也可以在之后。在加完这些代码之后,程序的大体就完成了。但如果你此时编译的话,肯定会出现错误,在task list上会显示出来,并且错误为“class 'thread' doesn't have a constructor that matches 'thread(applet1)' (j0082)”这是因为加进去的代码涉及到了线程,而主程序不知道,没有相应的接口,(j0082)为错误的序号,查找相应的工具,有这个错误的详细解释。要解决这个错误,只要在主类后面加上一个接口就可以了。如下:
public class applet1 extends applet implements runnable
注意后面的“implements runnable”。再编译,错误已经解除了,但这时候显示的结果还是和开始的时候显示的效果一样。这是因为还没有在html中设置该设置的内容。
查看applet1.java的内容:
import java.awt.*;
import java.applet.*;
/**
* this class reads param tags from its html host page and sets
* the color and label properties of the applet. program execution
* begins with the init() method.
*/
public class applet1 extends applet implements runnable
{
/**
* the entry point for the applet.
*/
font ft; //文字字体对象实例
string thestring, font, style; //移动的字符串、字体类型及样式
int size, speed; //文字的大小,移动速度
int xset, max, min;//文字每次移动的位移量,右边界和左边界
public void init()
{
initform();
usepageparams();
// todo: add any constructor code after initform call.
}
private final string labelparam = "label";
private final string backgroundparam = "background";
private final string foregroundparam = "foreground";
private final string param_string="string";
private final string param_font="font";
private final string param_style="style";
private final string param_size="size";
private final string param_speed="speed";
/**
* reads parameters from the applet's html host and sets applet
* properties.
*/
private void usepageparams()
{
final string defaultlabel = "default label";
final string defaultbackground = "c0c0c0";
final string defaultforeground = "000000";
string labelvalue;
string backgroundvalue;
string foregroundvalue;
/**
* read the ,
* ,
* and tags from
* the applet's html host.
*/
labelvalue = getparameter(labelparam);
backgroundvalue = getparameter(backgroundparam);
foregroundvalue = getparameter(foregroundparam);
if ((labelvalue == null) || (backgroundvalue == null) ||
(foregroundvalue == null))
{
/**
* there was something wrong with the html host tags.
* generate default values.
*/
labelvalue = defaultlabel;
backgroundvalue = defaultbackground;
foregroundvalue = defaultforeground;
}
/**
* set the applet's string label, background color, and
* foreground colors.
*/
label1.settext(labelvalue);
label1.setbackground(stringtocolor(backgroundvalue));
label1.setforeground(stringtocolor(foregroundvalue));
this.setbackground(stringtocolor(backgroundvalue));
this.setforeground(stringtocolor(foregroundvalue));
string param;
param = getparameter ( param_string);
if(param !=null) thestring= param;//取得字符串参数
param = getparameter ( param_font);
if(param !=null) font= param;//取得字型参数
param= getparameter ( param_size);
if (param !=null) size=integer. parseint ( param);//取得字提大小参数
param= getparameter ( param_style);
if(param!=null) style= param;//取得字型样式参数
param= getparameter ( param_speed);
if(param !=null) speed=integer.parseint (param);//取得文字移动速度参数
int vice_style = font.plain ;
if(style. equalsignorecase ( "bold"))
vice_style=font.bold ;
if (style.equalsignorecase ("italic"))
vice_style=font.italic ;//处理得到的字型参数,忽略大小写
ft=new font(font,vice_style,size);
fontmetrics fm= getfontmetrics(ft);//生成fontmetrics类对象实例
min= -fm.stringwidth (thestring);
max= getsize().width ;
xset= max;
}
/**
* converts a string formatted as "rrggbb" to an awt.color object
*/
private color stringtocolor(string paramvalue)
{
int red;
int green;
int blue;
red = (integer.decode("0x" + paramvalue.substring(0,2))).intvalue();
green = (integer.decode("0x" + paramvalue.substring(2,4))).intvalue();
blue = (integer.decode("0x" + paramvalue.substring(4,6))).intvalue();
return new color(red,green,blue);
}
/**
* external interface used by design tools to show properties of an applet.
*/
public string[][] getparameterinfo()
{
string[][] info =
{
{ labelparam, "string", "label string to be displayed" },
{ backgroundparam, "string", "background color, format /"rrggbb/"" },
{ foregroundparam, "string", "foreground color, format /"rrggbb/"" },
};
return info;
}
label label1 = new label();
/**
* intializes values for the applet and its components
*/
void initform()
{
this.setbackground(color.lightgray);
this.setforeground(color.black);
label1.settext("label1");
this.setlayout(new borderlayout());
this.add("north",label1);
}
public void paint(graphics g)
{
int yset=getsize().height ;
yset=(yset-size)/2;
g.setfont (ft);
g.setcolor (color.red);
g.drawstring (thestring,xset,yset+40);
//+40是用来调整输出时的高度用的。
xset--;
if(xset
}
public thread textmoverunnable=null;
public void start()
{
if(textmoverunnable==null)
{
textmoverunnable=new thread (this);
textmoverunnable.start ();
}
}
public void run()
{
while(true)
{
try{
repaint();thread.sleep (speed);
}
catch(interruptedexception e) {}
}
}
public void stop()
{
if (textmoverunnable!=null)
{
textmoverunnable.stop();
textmoverunnable=null;
}
}
}
5 修改html内的代码:
<applet
code=applet1.class
name=movetext
width=400
height=100 viewastext>
<param name=label value="this string was passed from the html host.">
<param name=background value="008080">
<param name=foreground value="ffffff">
<param name=string value="你好, 这是一个例子!">
<param name=font value="宋体">
<param name=style value=bold>
<param name=size value=40>
<param name=speed value=20>
</applet>
当你设置好这些后,应该是没有什么问题了吧。此时按下f5键,在弹出的默认浏览器内,你将看到这个例子的效果了。怎么样,没有骗你吧!好了,到此,applet的编写与编译的全部过程你应该掌握了吧。
applet与application虽然有很大的不同,但操作上基本同application的操作差不多,但编辑上还是有一些差别的。因此,这里也给大家举一个例子。这个例子稍为有点复杂,希望看的时候已经学过了一些java的基本知识了。这个例子的目的是要实现一个跑马灯的java applet程序,这个例子的效果如下:

1 选择新建菜单“new”,然后选择“new”下面的选项:applet on html,设置好名称与路径之后,按下确定按钮,系统将自动帮我们生成一两个文件,一个是applet的初始代码文件,另外一个就是html网页文件了。如下图:
2 在工程浏览窗口中双击applet1文件,这时就可以打开这个代码文件了。代码文件比较长,这里不列出,要查看代码,请点击这里。双击page1.html文件,也就打开这个html文件了,这个文件打开时,会显示如下图所示:
这个窗口里面有三个选项了,如果你用过frontpage,那么你将对这个非常的熟悉。这里就像frontpage一样,是一个网页编辑器,工具箱中也有你编辑网页所要的控件。不用多解释,看看source(源代码),打开后,有的可能显示的是该applet的内容。而有的可能显示的是代码,这与vj6的初始设定有关,在applet上面点击鼠标右键,选择“always view as text”后,就可以看到代码了,代码如下:
<applet
code=applet1.class
name=applet1
width=320
height=200 viewastext>
<param name=label value="this string was passed from the html host.">
<param name=background value="008080">
<param name=foreground value="ffffff">
</applet>
这个不用多解释,按照自己的要求改就行了。此时不能点击quick view进行快速预览,因为该applet还没有被编译成class文件。
3 此时将这个applet程序运行,按下f5键,直接运行,那么此时vj6会自动编译此程序,并且用默认的浏览器打开这个html文件并显示出来。
4 这个时候我们就要进行真正的添加代码的工作了。只有添加了必要的代码后,才能达到预期的目标嘛!请打开源代码文件applet1,双击就行!
先要做的工作,当然是设置变量,变量放在主类当中,如下:
font ft; //文字字体对象实例
string thestring, font, style; //移动的字符串、字体类型及样式
int size, speed; //文字的大小,移动速度
int xset, max, min;//文字所在位置的x坐标,右边界和左边界
定义好变量后,就要像给出的源代码一样,加入一些初始的代码了。
private final string param_string="string";
private final string param_font="font";
private final string param_style="style";
private final string param_size="size";
private final string param_speed="speed";
现在要做的事情就是从网页文件html中取得各种参数了,就像初始代码取得参数一样,这些可以参照初始代码而写出来!如下。
string param;
param = getparameter ( param_string);
if(param !=null) thestring= param;//取得字符串参数
param = getparameter ( param_font);
if(param !=null) font= param;//取得字型参数
param= getparameter ( param_size);
if (param !=null) size=integer. parseint ( param);//取得字提大小参数
param= getparameter ( param_style);
if(param!=null) style= param;//取得字型样式参数
param= getparameter ( param_speed);
if(param !=null) speed=integer.parseint (param);//取得文字移动速度参数
int vice_style = font.plain ;
if(style. equalsignorecase ( "bold"))
vice_style=font.bold ;
if (style.equalsignorecase ("italic"))
vice_style=font.italic ;//处理得到的字型参数,忽略大小写
ft=new font(font,vice_style,size);
fontmetrics fm= getfontmetrics(ft);//生成fontmetrics类对象实例
min= -fm.stringwidth (thestring);
max= getsize().width ;
offset= max;
接下来要做的事情,当然是要做最重要的工作了。编写程序的主要部分,字体滚动的过程。这部分的代码就不详细解释了。希望你已经学过java语法了。
public void paint(graphics g)
{
int yset=getsize().height ;//yset为输出字符串的y坐标变量
yset=(yset-size)/2;
g.setfont (ft);
g.setcolor (color.red);
g.drawstring (thestring,xset,yset+40);
//+40是用来调整输出时的高度用的。
xset--;
if(xset
}
public thread textmoverunnable=null;
public void start()
{
if(textmoverunnable==null)
{
textmoverunnable=new thread (this);
textmoverunnable.start ();
}
}
public void run()
{
while(true)
{
try{
repaint();thread.sleep (speed);
}catch(interruptedexception e) {}
}
}
public void stop()
{
if (textmoverunnable!=null)
{
textmoverunnable.stop();
textmoverunnable=null;
}
}
这部分代码应该加在与initform()并列的地方,可以在之前,也可以在之后。在加完这些代码之后,程序的大体就完成了。但如果你此时编译的话,肯定会出现错误,在task list上会显示出来,并且错误为“class 'thread' doesn't have a constructor that matches 'thread(applet1)' (j0082)”这是因为加进去的代码涉及到了线程,而主程序不知道,没有相应的接口,(j0082)为错误的序号,查找相应的工具,有这个错误的详细解释。要解决这个错误,只要在主类后面加上一个接口就可以了。如下:
public class applet1 extends applet implements runnable
注意后面的“implements runnable”。再编译,错误已经解除了,但这时候显示的结果还是和开始的时候显示的效果一样。这是因为还没有在html中设置该设置的内容。
import java.awt.*;
import java.applet.*;
/**
* this class reads param tags from its html host page and sets
* the color and label properties of the applet. program execution
* begins with the init() method.
*/
public class applet1 extends applet implements runnable
{
/**
* the entry point for the applet.
*/
font ft; //文字字体对象实例
string thestring, font, style; //移动的字符串、字体类型及样式
int size, speed; //文字的大小,移动速度
int xset, max, min;//文字每次移动的位移量,右边界和左边界
public void init()
{
initform();
usepageparams();
// todo: add any constructor code after initform call.
}
private final string labelparam = "label";
private final string backgroundparam = "background";
private final string foregroundparam = "foreground";
private final string param_string="string";
private final string param_font="font";
private final string param_style="style";
private final string param_size="size";
private final string param_speed="speed";
/**
* reads parameters from the applet's html host and sets applet
* properties.
*/
private void usepageparams()
{
final string defaultlabel = "default label";
final string defaultbackground = "c0c0c0";
final string defaultforeground = "000000";
string labelvalue;
string backgroundvalue;
string foregroundvalue;
/**
* read the ,
* ,
* and tags from
* the applet's html host.
*/
labelvalue = getparameter(labelparam);
backgroundvalue = getparameter(backgroundparam);
foregroundvalue = getparameter(foregroundparam);
if ((labelvalue == null) || (backgroundvalue == null) ||
(foregroundvalue == null))
{
/**
* there was something wrong with the html host tags.
* generate default values.
*/
labelvalue = defaultlabel;
backgroundvalue = defaultbackground;
foregroundvalue = defaultforeground;
}
/**
* set the applet's string label, background color, and
* foreground colors.
*/
label1.settext(labelvalue);
label1.setbackground(stringtocolor(backgroundvalue));
label1.setforeground(stringtocolor(foregroundvalue));
this.setbackground(stringtocolor(backgroundvalue));
this.setforeground(stringtocolor(foregroundvalue));
string param;
param = getparameter ( param_string);
if(param !=null) thestring= param;//取得字符串参数
param = getparameter ( param_font);
if(param !=null) font= param;//取得字型参数
param= getparameter ( param_size);
if (param !=null) size=integer. parseint ( param);//取得字提大小参数
param= getparameter ( param_style);
if(param!=null) style= param;//取得字型样式参数
param= getparameter ( param_speed);
if(param !=null) speed=integer.parseint (param);//取得文字移动速度参数
int vice_style = font.plain ;
if(style. equalsignorecase ( "bold"))
vice_style=font.bold ;
if (style.equalsignorecase ("italic"))
vice_style=font.italic ;//处理得到的字型参数,忽略大小写
ft=new font(font,vice_style,size);
fontmetrics fm= getfontmetrics(ft);//生成fontmetrics类对象实例
min= -fm.stringwidth (thestring);
max= getsize().width ;
xset= max;
}
/**
* converts a string formatted as "rrggbb" to an awt.color object
*/
private color stringtocolor(string paramvalue)
{
int red;
int green;
int blue;
red = (integer.decode("0x" + paramvalue.substring(0,2))).intvalue();
green = (integer.decode("0x" + paramvalue.substring(2,4))).intvalue();
blue = (integer.decode("0x" + paramvalue.substring(4,6))).intvalue();
return new color(red,green,blue);
}
/**
* external interface used by design tools to show properties of an applet.
*/
public string[][] getparameterinfo()
{
string[][] info =
{
{ labelparam, "string", "label string to be displayed" },
{ backgroundparam, "string", "background color, format /"rrggbb/"" },
{ foregroundparam, "string", "foreground color, format /"rrggbb/"" },
};
return info;
}
label label1 = new label();
/**
* intializes values for the applet and its components
*/
void initform()
{
this.setbackground(color.lightgray);
this.setforeground(color.black);
label1.settext("label1");
this.setlayout(new borderlayout());
this.add("north",label1);
}
public void paint(graphics g)
{
int yset=getsize().height ;
yset=(yset-size)/2;
g.setfont (ft);
g.setcolor (color.red);
g.drawstring (thestring,xset,yset+40);
//+40是用来调整输出时的高度用的。
xset--;
if(xset
}
public thread textmoverunnable=null;
public void start()
{
if(textmoverunnable==null)
{
textmoverunnable=new thread (this);
textmoverunnable.start ();
}
}
public void run()
{
while(true)
{
try{
repaint();thread.sleep (speed);
}
catch(interruptedexception e) {}
}
}
public void stop()
{
if (textmoverunnable!=null)
{
textmoverunnable.stop();
textmoverunnable=null;
}
}
}
<applet
code=applet1.class
name=movetext
width=400
height=100 viewastext>
<param name=label value="this string was passed from the html host.">
<param name=background value="008080">
<param name=foreground value="ffffff">
<param name=string value="你好, 这是一个例子!">
<param name=font value="宋体">
<param name=style value=bold>
<param name=size value=40>
<param name=speed value=20>
</applet>
当你设置好这些后,应该是没有什么问题了吧。此时按下f5键,在弹出的默认浏览器内,你将看到这个例子的效果了。怎么样,没有骗你吧!好了,到此,applet的编写与编译的全部过程你应该掌握了吧。
闽公网安备 35060202000074号