一个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(xSet3回顶部
查看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(xSet4回顶部
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的编写与编译的全部过程你应该掌握了吧。
|