2.2小试身手
2.2.1起始页时间和日期
介绍两个类:
1.Date
创建一个实例Date timeNow=new Date();
2.Font
创建一个实例Font msgFont=new Font("TimesRoman",Font.ITALIC,30);
example:
import java.awt.*;
import java.util.Date;
public class ShowDate extends java.applet.Applet{
Date timeNow=new Date();
Font msgFont=new Font("TimesRoman",Font.ITALIC,30);
public void paint(Graphics g){
g.setFont(msgFont);
g.setColor(Color.bule);
g.drawString(timeNow.toString(),5,50);
}
}
2.2.2在起始页中加入applet
1.html中有关的代码
[applet code="ShowDate.class" width=600 height=80]
[/applet]//把[]变成<>
2.codebase的使用
当class文件与起始页文件不在同一个目录时,使用codebase说明
[applet code="ShowDate.class" width=600 height=80]
codebase="\myjava\class"(class的路径)
[/applet]//把[]变成<>
2.2.3
向applet传递参数的两个步骤:
1.在起始页中要有
标签
2.在applet中要有getParameter方法
在起始页中有:
在applet中有:
String titil=getParameter(rem);
在显示时间的命令中加入title:
g.drawString(rem+timeNow.toString(),5,50);
2.2.4
利用一个可以显示运行字符串的类,显示自己的字符串(htmlpara.html)
[applet code="htmlpara.class" width=300 height=200]
[param name=MESSAGE value="this is a test"]
[param name=FONT value="BOLD"]
[param name=POINT_SIZE value=20>
example:
public void init(){
String parameter;
parameter=getParameter("MESSAGE");
if(parameter!=null)
message=parameter;
parameter=getParameter("FONT");
if(parameter!=null)
font_to_use=parameter;
parameter=getParameter("POINT_SIZE")
if(parameter!=null)
point_size=Integer.parseInt(parameter);
}