★关于applet中的start和stop两个方法的“调用时机”问题★
最近刚开始学习applet.
但是在实习的时候发现了一个问题:
就是关于start和stop两个方法的“调用时机”问题:
资料上说:
“
启动方法start():
系统在调用完init()方法之后将自动调用start()方法,此后每当返回Applet所在的
主页或是恢复浏览器窗口时系统都将调用一次start()方法。该方法中包含Applet的主体内容。”
“停止方法stop():
每当离开Applet所在的主页或是图标化浏览器窗口时系统都将调用一次stop()方法。
显然start()方法与stop()方法都可以被多次调用,并且两种方法是穿插调用的,每一次对stop()方法的调用都是在一次对start()方法的调用之后。”
于是我就写了个程序试试,但是与书上说的不一样?
代码如下:
import java.awt.Graphics;
import java.applet.Applet;
public class HelloWorldlet extends Applet
{
String strText;
int i_start;
int i_stop;
int i_paint;
public void init()
{
strText = "Hello Apllet World!";
i_start = 0;
i_stop = 0;
i_paint = 0;
}
public void start()
{
i_start++;
}
public void stop()
{
i_stop++;
}
public void paint(Graphics g)
{
i_paint++;
String strOut = strText;
g.drawString(strOut,25,20);
strOut = "The time(s) of start is(are) " + i_start;
g.drawString(strOut,25,35);
strOut = "The time(s) of start is(are) " + i_stop;
g.drawString(strOut,25,50);
strOut = "The time(s) of start is(are) " + i_paint;
g.drawString(strOut,25,65);
}
}
运行的效果是:无论我是第一次加载页面,还是离开页面再回来或者还是窗口最小化后再还原start总计只执行一次?stop一次也不执行?为什么啊?
(即显示效果是:i_start is 1,i_stop is 0)
是不是我对“离开Applet所在的主页”这句话理解不对?
于是我去查JAVA帮助手册,说的也是这个意思
(关于start:It is called after the init method and each time the applet is revisited in a Web page.
关于stop:It is called when the Web page that contains this applet has been replaced by another page, and also just before the applet is to be destroyed. )
为什么实际与资料上的不一样呢?
希望大家帮助!多谢了!