现在的位置: 首页 > 综合 > 正文

JAVA中Applet的生命周期

2013年10月19日 ⁄ 综合 ⁄ 共 3060字 ⁄ 字号 评论关闭

 

An applet is a special kind of Java program that a browser enabled with Java technology can download from the internet and run. An applet is typically embedded inside a web
- page and runs in the context of the browser. An applet must be a subclass of the java.applet.Applet 
class
, which provides the standard 
interface
 between the applet and the browser environment.

applet是一种特殊的JAVA程序,允许带有Java技术的浏览器能够从Internet下载它并运行。applet是典型的嵌入到网页里面并在浏览器上下文中运行的。applet必须是java.applet.Applet类的子类,java.applet.Applet提供applet和浏览器环境之间的接口标准。

Swing provides a special subclass of Applet, called javax.swing.JApplet, which should be used 
for
 all applets that use Swing components to construct their GUIs.

Swing提供一个特殊的applet子类,叫做javax.swing.JApplet,当所有的applet要用到Swing部件来构造他们的GUI时,那么它就需要用到它。

By calling certain methods, a browser manages an applet life cycle, 
i
f
 an applet is loaded in a web page. 
如果applet已经加载到一个网页中,那么通过调用applet的某一方法,浏览器可以控制applet的生命周期。

上面讲了applet的一些知识,接下来的就是applet的生命周期的讲解了:

方法地地Life Cycle of an Applet: Basically, there are four methods in the Applet class on which any applet is built. 
Applet的生命周期:基本上,在Applet类的每个实例中有四个方法。

init: This method is intended for whatever initialization is needed for your applet. It is called after the param attributes of the applet tag. 
init:这个往往用来执行你的applet需要做的所有初始化工作,当在applet标记中的param属性读入后开始调用。

start: This method is automatically called after init method. It is also called whenever user returns to the page containing the applet after visiting other pages. 
start:此方法在init方法调用完后自动调用,它是执行访问其它页面后用户重新返回到包含applet的页面中时的操作。

stop: This method is automatically called whenever the user moves away from the page containing applets. You can use this method to stop an animation. 
stop:此方法当用户从包含applet的页面中离开时自动调用,你能够用此方法来停止你的动画。

destroy: This method is only called when the browser shuts down normally. 
destroy:此方法仅当正常地关闭浏览器时被调用。

Thus, the applet can be initialized once and only once, started and stopped one or more times in its life, and destroyed once and only once.
因此,applet仅能用init一次,能够在它的生命中被start和stop一次或多次,同时也仅能被destroy一次。

最后还用一个例子吧:

 1/**//*
 2 * Java(TM) SE 6 Version 
 3 */

 4
 5import
 java.applet.Applet;
 6import
 java.awt.Graphics;
 7

 8//
No need to extend JApplet, since we don't add any components;
 9//we just paint.

10public class Simple extends Applet {
11

12
    StringBuffer buffer;
13

14    public void init() 
{
15        buffer = new
 StringBuffer();
16        addItem("initializing "
);
17    }

18
19    public void start() 
{
20        addItem("starting "
);
21    }

22
23    public void stop() 
{
24        addItem("stopping "
);
25    }

26
27    public void destroy() 
{
28        addItem("preparing for unloading"
);
29    }

30
31    private void addItem(String newWord) 
{
32
        System.out.println(newWord);
33
        buffer.append(newWord);
34
        repaint();
35    }

36
37    public void paint(Graphics g) 
{
38    //Draw a Rectangle around the applet's display area.

39        g.drawRect(00
40           getWidth() - 1
,
41           getHeight() - 1
);
42

43    //Draw the current string inside the rectangle.

44        g.drawString(buffer.toString(), 515);
45    }

46}

抱歉!评论已关闭.