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