java常用類解析九:Applet(JApplet)詳解及樣本

來源:互聯網
上載者:User

1、Applet類及各個方法說明

     Applet類提供一個基本架構,使得applet可以通過Web瀏覽器來運行,applet沒有main方法,它依靠瀏覽器調用Applet類中的方法。Applet不安全。下面是截取的一段Applet類的原始碼: 

/** * Called by the browser or applet viewer to inform this applet that it has * been loaded into the system. It is always called before the first time * that the <code>start</code> method is called. * <p> * A subclass of <code>Applet</code> should override this method if it has * initialization to perform. For example, an applet with threads would use * the <code>init</code> method to create the threads and the * <code>destroy</code> method to kill them. * <p> * The implementation of this method provided by the <code>Applet</code> * class does nothing. * 補充:JVM載入applet類,瀏覽器建立applet,瀏覽器調用init方法進行初始化,如果Applet的子 * 類具有初始化操作應覆蓋此方法。通常,該方法實現的功能包括建立使用者介面組件、裝載映像和音頻等資源 * 以及從HTML網頁的<applet>標記中擷取參數 */public void init() {}/** * Called by the browser or applet viewer to inform this applet that it * should start its execution. It is called after the <code>init</code> * method and each time the applet is revisited in a Web page. * <p> * A subclass of <code>Applet</code> should override this method if it has * any operation that it wants to perform each time the Web page containing * it is visited. For example, an applet with animation might want to use * the <code>start</code> method to resume animation, and the * <code>stop</code> method to suspend the animation. * <p> * Note: some methods, such as <code>getLocationOnScreen</code>, can only * provide meaningful results if the applet is showing. Because * <code>isShowing</code> returns <code>false</code> when the applet's * <code>start</code> is first called, methods requiring * <code>isShowing</code> to return <code>true</code> should be called from * a <code>ComponentListener</code>. * <p> * The implementation of this method provided by the <code>Applet</code> * class does nothing. * 補充:init方法完成後,調用start方法,瀏覽過別的網頁之後回來也調用此方法 */public void start() {}/** * Called by the browser or applet viewer to inform this applet that it * should stop its execution. It is called when the Web page that contains * this applet has been replaced by another page, and also just before the * applet is to be destroyed. * <p> * A subclass of <code>Applet</code> should override this method if it has * any operation that it wants to perform each time the Web page containing * it is no longer visible. For example, an applet with animation might want * to use the <code>start</code> method to resume animation, and the * <code>stop</code> method to suspend the animation. * <p> * The implementation of this method provided by the <code>Applet</code> * class does nothing. * 補充:離開頁面時調用stop方法 */public void stop() {}/** * 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. The <code>stop</code> method will always be called before * <code>destroy</code>. * <p> * A subclass of <code>Applet</code> should override this method if it has * any operation that it wants to perform before it is destroyed. For * example, an applet with threads would use the <code>init</code> method to * create the threads and the <code>destroy</code> method to kill them. * <p> * The implementation of this method provided by the <code>Applet</code> * class does nothing. */public void destroy() {}

 

從以上描述和代碼中可以看出,瀏覽器通過init、start、stop、destroy方法控制Applet,通常這些方法都是空方法,一般要覆蓋這些方法實現操作。

2、JApplet類樣本

     Applet類沒有考慮與Swing組件一起工作,所以從Applet類擴充出了一個JApplet類。JApplet的內容窗格使用BorderLayout布局管理器。下面是一個JApplet的實際例子:

package demo.others;import java.awt.BorderLayout;import javax.swing.JApplet;import javax.swing.JFrame;import javax.swing.JLabel;public class MyJApplet extends JApplet {private static final long serialVersionUID = 1L;@Overridepublic void init() {// 在init方法中接收來自html頁面上的參數//String message = getParameter("MESSAGE");add(new JLabel("welcome to touch's blog", JLabel.CENTER));}// 用main方法運行JAppletpublic static void main(String[] args) {JFrame frame = new JFrame("Applet is in the frame");MyJApplet myJApplet = new MyJApplet();// main方法裡建立一個架構來放置applet,applet單獨運行時,// 要完成操作必須手動調用init和start方法frame.add(myJApplet, BorderLayout.CENTER);myJApplet.init();frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 300);frame.setVisible(true);}}

下面的例子用html顯示Applet

import java.awt.BorderLayout;import javax.swing.JApplet;import javax.swing.JFrame;import javax.swing.JLabel;public class MyJApplet extends JApplet {private static final long serialVersionUID = 1L;@Overridepublic void init() {// 在init方法中接收來自html頁面上的參數String message = getParameter("MESSAGE");add(new JLabel(message, JLabel.CENTER));}// 用main方法運行JAppletpublic static void main(String[] args) {JFrame frame = new JFrame("Applet is in the frame");MyJApplet myJApplet = new MyJApplet();// main方法裡建立一個架構來放置applet,applet單獨運行時,// 要完成操作必須手動調用init和start方法frame.add(myJApplet, BorderLayout.CENTER);myJApplet.init();frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(300, 300);frame.setVisible(true);}}

 

<html>  <head>    <title>passing string to java Applets</title>  </head>  <body>  <p>this applet gets a message from the HTML</p>  <applet     code ="MyJApplet.class" width=200 height=50 alt="you must have a java 2-enable browser to view the applet"   >   <param name=MESSAGE value="Welcome to touch's blog">   </applet></html>

運行結果:

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.