java常用类解析九:Applet(JApplet)详解及示例


1、Applet类及各个方法说明

     Applet类提供一个基本框架,使得applet可以通过Web浏览器来运行,applet没有main方法,它依靠浏览器调用Applet类中的方法。Applet不安全。下面是截取的一段Applet类的源代码: 

[java]   view plain copy
  1. <span style="font-size:16px;">    /** 
  2.      * Called by the browser or applet viewer to inform this applet that it has 
  3.      * been loaded into the system. It is always called before the first time 
  4.      * that the <code>start</code> method is called. 
  5.      * <p> 
  6.      * A subclass of <code>Applet</code> should override this method if it has 
  7.      * initialization to perform. For example, an applet with threads would use 
  8.      * the <code>init</code> method to create the threads and the 
  9.      * <code>destroy</code> method to kill them. 
  10.      * <p> 
  11.      * The implementation of this method provided by the <code>Applet</code> 
  12.      * class does nothing. 
  13.      * 补充:JVM加载applet类,浏览器创建applet,浏览器调用init方法进行初始化,如果Applet的子 
  14.      * 类具有初始化操作应覆盖此方法。通常,该方法实现的功能包括创建用户界面组件、装载图像和音频等资源 
  15.      * 以及从HTML网页的<applet>标记中获取参数 
  16.      */  
  17.     public void init() {  
  18.     }  
  19.   
  20.     /** 
  21.      * Called by the browser or applet viewer to inform this applet that it 
  22.      * should start its execution. It is called after the <code>init</code> 
  23.      * method and each time the applet is revisited in a Web page. 
  24.      * <p> 
  25.      * A subclass of <code>Applet</code> should override this method if it has 
  26.      * any operation that it wants to perform each time the Web page containing 
  27.      * it is visited. For example, an applet with animation might want to use 
  28.      * the <code>start</code> method to resume animation, and the 
  29.      * <code>stop</code> method to suspend the animation. 
  30.      * <p> 
  31.      * Note: some methods, such as <code>getLocationOnScreen</code>, can only 
  32.      * provide meaningful results if the applet is showing. Because 
  33.      * <code>isShowing</code> returns <code>false</code> when the applet's 
  34.      * <code>start</code> is first called, methods requiring 
  35.      * <code>isShowing</code> to return <code>true</code> should be called from 
  36.      * a <code>ComponentListener</code>. 
  37.      * <p> 
  38.      * The implementation of this method provided by the <code>Applet</code> 
  39.      * class does nothing. 
  40.      * 补充:init方法完成后,调用start方法,浏览过别的网页之后回来也调用此方法 
  41.      */  
  42.     public void start() {  
  43.     }  
  44.   
  45.     /** 
  46.      * Called by the browser or applet viewer to inform this applet that it 
  47.      * should stop its execution. It is called when the Web page that contains 
  48.      * this applet has been replaced by another page, and also just before the 
  49.      * applet is to be destroyed. 
  50.      * <p> 
  51.      * A subclass of <code>Applet</code> should override this method if it has 
  52.      * any operation that it wants to perform each time the Web page containing 
  53.      * it is no longer visible. For example, an applet with animation might want 
  54.      * to use the <code>start</code> method to resume animation, and the 
  55.      * <code>stop</code> method to suspend the animation. 
  56.      * <p> 
  57.      * The implementation of this method provided by the <code>Applet</code> 
  58.      * class does nothing. 
  59.      * 补充:离开页面时调用stop方法 
  60.      */  
  61.     public void stop() {  
  62.     }  
  63.   
  64.     /** 
  65.      * Called by the browser or applet viewer to inform this applet that it is 
  66.      * being reclaimed and that it should destroy any resources that it has 
  67.      * allocated. The <code>stop</code> method will always be called before 
  68.      * <code>destroy</code>. 
  69.      * <p> 
  70.      * A subclass of <code>Applet</code> should override this method if it has 
  71.      * any operation that it wants to perform before it is destroyed. For 
  72.      * example, an applet with threads would use the <code>init</code> method to 
  73.      * create the threads and the <code>destroy</code> method to kill them. 
  74.      * <p> 
  75.      * The implementation of this method provided by the <code>Applet</code> 
  76.      * class does nothing. 
  77.      */  
  78.     public void destroy() {  
  79.     }  
  80. </span>  

 

从以上描述和代码中可以看出,浏览器通过init、start、stop、destroy方法控制Applet,通常这些方法都是空方法,一般要覆盖这些方法实现操作。

2、JApplet类示例

     Applet类没有考虑与Swing组件一起工作,所以从Applet类扩展出了一个JApplet类。JApplet的内容窗格使用BorderLayout布局管理器。下面是一个JApplet的实际例子:

[java]   view plain copy
  1. <span style="font-size:16px;">package demo.others;  
  2.   
  3. import java.awt.BorderLayout;  
  4.   
  5. import javax.swing.JApplet;  
  6. import javax.swing.JFrame;  
  7. import javax.swing.JLabel;  
  8.   
  9. public class MyJApplet extends JApplet {  
  10.     private static final long serialVersionUID = 1L;  
  11.   
  12.     @Override  
  13.     public void init() {  
  14.         // 在init方法中接收来自html页面上的参数  
  15.         //String message = getParameter("MESSAGE");  
  16.         add(new JLabel("welcome to touch's blog", JLabel.CENTER));  
  17.     }  
  18.   
  19.     // 用main方法运行JApplet  
  20.     public static void main(String[] args) {  
  21.         JFrame frame = new JFrame("Applet is in the frame");  
  22.         MyJApplet myJApplet = new MyJApplet();  
  23.         // main方法里创建一个框架来放置applet,applet单独运行时,  
  24.         // 要完成操作必须手动调用init和start方法  
  25.         frame.add(myJApplet, BorderLayout.CENTER);  
  26.         myJApplet.init();  
  27.   
  28.         frame.setLocationRelativeTo(null);  
  29.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  30.         frame.setSize(300300);  
  31.         frame.setVisible(true);  
  32.     }  
  33. }  
  34. </span>  


下面的例子用html显示Applet

[java]   view plain copy
  1. <span style="font-size:16px;">import java.awt.BorderLayout;  
  2.   
  3. import javax.swing.JApplet;  
  4. import javax.swing.JFrame;  
  5. import javax.swing.JLabel;  
  6.   
  7. public class MyJApplet extends JApplet {  
  8.     private static final long serialVersionUID = 1L;  
  9.   
  10.     @Override  
  11.     public void init() {  
  12.         // 在init方法中接收来自html页面上的参数  
  13.         String message = getParameter("MESSAGE");  
  14.         add(new JLabel(message, JLabel.CENTER));  
  15.     }  
  16.   
  17.     // 用main方法运行JApplet  
  18.     public static void main(String[] args) {  
  19.         JFrame frame = new JFrame("Applet is in the frame");  
  20.         MyJApplet myJApplet = new MyJApplet();  
  21.         // main方法里创建一个框架来放置applet,applet单独运行时,  
  22.         // 要完成操作必须手动调用init和start方法  
  23.         frame.add(myJApplet, BorderLayout.CENTER);  
  24.         myJApplet.init();  
  25.   
  26.         frame.setLocationRelativeTo(null);  
  27.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  28.         frame.setSize(300300);  
  29.         frame.setVisible(true);  
  30.     }  
  31. }  
  32. </span>  

 

[html]   view plain copy
  1. <span style="font-size:16px;"><html>  
  2.   <head>  
  3.     <title>passing string to java Applets</title>  
  4.   </head>  
  5.   <body>  
  6.   <p>this applet gets a message from the HTML</p>  
  7.   <applet  
  8.      code ="MyJApplet.class"  
  9.      width=200  
  10.      height=50  
  11.      alt="you must have a java 2-enable browser to view the applet"  
  12.    >  
  13.    <param name=MESSAGE value="Welcome to touch's blog">  
  14.    </applet>  
  15. </html></span>  


运行结果:

 

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM