Documentation

The Java™ Tutorials
Hide TOC
Life Cycle of an Applet小程序的生命周期
Trail: Deployment
Lesson: Java Applets
Section: Getting Started With Applets

Life Cycle of an Applet小程序的生命周期

An applet can react to major events in the following ways:小程序可以通过以下方式对重大事件做出反应:

This section introduces a new applet, Simple, that uses all of these methods. 本节介绍一个新的applet Simple,它使用了所有这些方法。Unlike Java applications, applets do not need to implement a main method.与Java应用程序不同,小程序不需要实现main方法。

Here is the Simple applet.下面是一个Simple小程序。


Note:  If you don't see the example running, you might need to enable the JavaScript interpreter in your browser so that the Deployment Toolkit script can function properly如果没有看到示例正在运行,可能需要在浏览器中启用JavaScript解释器,以便部署工具包脚本能够正常运行.

The following is the source code for the Simple applet. 下面是这个Simple小程序的源代码。This applet displays a descriptive string whenever it encounters a major milestone in its life, such as when the user first visits the page the applet is on.每当小程序在其生命周期中遇到一个重要里程碑时,例如当用户第一次访问小程序所在的页面时,该小程序就会显示一个描述性字符串。

import java.applet.Applet;
import java.awt.Graphics;

//No need to extend JApplet, since we don't add any components;
//we just paint.
public class Simple extends Applet {

    StringBuffer buffer;

    public void init() {
        buffer = new StringBuffer();
        addItem("initializing... ");
    }

    public void start() {
        addItem("starting... ");
    }

    public void stop() {
        addItem("stopping... ");
    }

    public void destroy() {
        addItem("preparing for unloading...");
    }

    private void addItem(String newWord) {
        System.out.println(newWord);
        buffer.append(newWord);
        repaint();
    }

    public void paint(Graphics g) {
	//Draw a Rectangle around the applet's display area.
        g.drawRect(0, 0, 
		   getWidth() - 1,
		   getHeight() - 1);

	//Draw the current string inside the rectangle.
        g.drawString(buffer.toString(), 5, 15);
    }
}

Note: In this example, the Applet class is extended, not the Swing JApplet class, as Swing components do not need to be added to this applet. 在本例中,Applet类被扩展,而不是Swing JApplet类,因为Swing组件不需要添加到此小程序中。

Loading the Applet加载小程序

As a result of the applet being loaded, you should see the text "initializing... starting...". 加载小程序后,您应该会看到文本“初始化…启动…”。When an applet is loaded, here's what happens:加载小程序时,会发生以下情况:

Leaving and Returning to the Applet's Page离开并返回小程序页面

When the user leaves the page, for example, to go to another page, the browser stops and destroys the applet. 例如,当用户离开页面转到另一个页面时,浏览器会停止并销毁小程序。The state of the applet is not preserved. 小程序的状态不会被保留。When the user returns to the page, the browser initializes and starts a new instance of the applet.当用户返回页面时,浏览器初始化并启动小程序的新实例。

Reloading the Applet重新加载小程序

When you refresh or reload a browser page, the current instance of the applet is stopped and destroyed and a new instance is created.刷新或重新加载浏览器页面时,小程序的当前实例将停止并销毁,并创建一个新实例。

Quitting the Browser退出浏览器

When the user quits the browser, the applet has the opportunity to stop itself and perform a final cleanup before the browser exits.当用户退出浏览器时,小程序有机会自行停止,并在浏览器退出之前执行最终清理。

Download source code for the Simple Applet example to experiment further.下载Simple小程序示例的源代码以进行进一步实验。


Previous page: Methods for Milestones
Next page: Applet's Execution Environment