guozhang

Java Beginner

Lifecycle of Applet

 

Lifecycle of Applet

    Applet的生命周期中有四个状态:初始态,运行态,停止态和消亡态. Applet的生命周期息息相关的是其以下方法:

void init()

      Called by the browser or applet viewer to inform this applet that it has been loaded into the system.

void start()

      Called by the browser or applet viewer to inform this applet that it should start its execution.

void stop()

     Called by the browser or applet viewer to inform this applet that it should stop its execution.

void destroy()

       Called by the browser or applet viewer to inform this applet that it is being reclaimed and that it should destroy any resources that it has allocated.

 

    Applet首次被加载的时候, init()将被唤起. init()方法在Applet的生命周期内, 仅会被调用一次(就是在Applet被加载的时候). 因此, 这是进行资源初始化(比如界面布局, 数据库连接的获取等等)的最佳场所.

    init()方法被调用之后, 接下来调用的便是start()方法. start()方法的调用将使得Applet的状态成为active. Applet从最小化状态恢复成最大化时[很多参考资料认为浏览器再度回到显示Applet页面时也会执行该方法, 但是经笔者试验发现这种观点似乎有失科学], 该方法均会被唤起. 因此, init()不同的是, 该方法可能会被调用多次.  

stop()方法是start()方法的逆操作. Applet变成最小化时[start()类似, 很多参考资料认为浏览器转到其它页面时,该方法也会被调用], 该方法将被唤起. 通常情况下, stop()方法中完成与start()方法中相反的操作. 例如: Applet处于active状态的时候, 我们打算播放一个音乐片断. 那么在Applet处于非活动状态时, 我们可能希望停止该片断的播送:

public void start(){

    if(musicNotPlay())       //musicNotPlay用来判断音乐片断是否被播放过

         musicClip.play();

    else

         musicClip.resume();

}

 

public void stop(){

    if(!musicNotPlay())

        musicClip.pause();

}

 

    destroy()方法, 顾名思义, 是摧毁Applet时调用的方法. 因为Applet将要被摧毁了, 因此, Applet使用到的资源(比如数据库链接等资源)应该被释放.

1.JPG

范例

这是一个相当简单的范例, 它只是在上述提及的方法中打印出一些信息:

AppletTest.java

//<applet code="AppletTest" width="400" height="300">

//</applet>

 

import javax.swing.*;

 

public class AppletTest extends JApplet{

   public void init(){

      System.out.println("Initializing the applet....");

   }

  

   public void start(){

      System.out.println("Starting the applet....");

   }

  

   public void stop(){

       System.out.println("Stoping the applet...");

   }

  

   public void destroy(){

      System.out.println("Destroying the applet...");

   }

}

 

程序运行效果(使用appletviewer):

2.JPG
程序启动时
(init() start() 被调用)

3.JPG
applet最小化(stop()被调用)

4.JPG
applet恢复(start()再度被调用)

5.JPG
关闭
appletviewer(stop()destory()被调用)

在浏览器中执行

appletviewer中执行的Applet运行得相当令人满意. 下面我们将其移植在浏览器中执行. 测试环境为: Windows XP SP2 + JDK 5.0 + Internet Explorer 6.0.

首先撰写HTML文件, Applet嵌入:

AppletTest.html

<html>

  <head>

     <title>Applet的生命周期</title>

  </head>

  <body>

       下面是一个用来测试的Applet(虽然它没有显示什么东西:))<br>

        <applet code="AppletTest" width="100" height="50">

       </applet>

        <p>用来跳转的链接<br>

           <UL>

              <li><a href="SomeOtherPage.html">Some other page</a></li>

           </UL>

       </p>

  </body>

</html>

 

同时再写一个用来跳转测试的HTML文件:

SomeOtherPage.html

<html>

   <head>

       <title>用做跳转的页面</title>

   </head>

   <body>

      <a href="AppletTest.html">To AppletTest Page</a>

   </body>

</html>

 

    这两个文件都相当简单, 不用再浪费口水J

 

执行效果:

 

6.JPG 
                     执行AppletTest.html  

    7.JPG
              
 控制台中的输出(init() & start())

 


     8.JPG
                      点击链接            

       9.JPG          
                     
控制台中的输出(stop() & destroy())


 
             10.JPG
                     点击链接,回到Applet页面               

        11.JPG
                         重新加载
(init() & start())

            
12.JPG
                最小化浏览器窗口
, 控制台中无反应


  
从上面的图中可以看到, 当浏览器跳转到其它页面时, Applet会被停止并且被摧毁. 当浏览器重新跳转到Applet页面的时候, Applet会被重新加载. 当浏览器窗口被最小化/恢复时, 不会有任何动作被执行到. 这和很多参考资料上的说法相悖.

 

ps: 测试环境WinXP SP2 + JDK5.0+IE6.0

posted on 2005-12-20 15:58 Guo Zhang 阅读(379) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航:
 
<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(1)

随笔档案

文章分类

文章档案

收藏夹

搜索

最新随笔

最新评论

阅读排行榜

评论排行榜