★关于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. )
为什么实际与资料上的不一样呢?
希望大家帮助!多谢了!
但是在实习的时候发现了一个问题:
就是关于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. )
为什么实际与资料上的不一样呢?
希望大家帮助!多谢了!
...全文
相关推荐
Start Applet Demo
这些方法在Applet的不同阶段被自动调用,例如当浏览器加载、显示、隐藏或关闭Applet时。5.
两个applet通信
本文将深入探讨如何实现两个Applet之间的通信,以“发送”(SendApplet)和“接收”(ReceiveApplet)为例,讲解其中涉及的关键知识点。
java applet 简单案例
暂停(stop()):当浏览器窗口失去焦点,或者Applet需要暂停执行时,调用stop()方法。5. 启动(start()):当Applet重新获得焦点时,再次调用start()方法恢复执行。6.
Java中Applet类的方法.pdf
**start()方法**:在`init()`方法之后,浏览器会调用`start()`方法来启动Applet的主线程。这标志着Applet开始执行。
Applet
Applet的生命周期包括了四个关键方法:init(), start(), stop()和destroy()。


Java SE