Explanation of common Java classes 9: explanation and examples of Applet (japplet)

Source: Internet
Author: User

1. Description of the Applet Class and each method

The Applet Class provides a basic framework that allows the applet to run through a web browser. The applet does not have the main method, and the browser calls the method in the Applet Class. The applet is not secure. The following is the source code of the intercepted Applet Class:

/*** 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> shocould override this method if it has * initialization to perform. for example, an applet with threads wocould use * The <code> init </code> method to create the T Hreads and the * <code> destroy </code> method to kill them. * <p> * The implementation of this method provided by the <code> applet </code> * class does nothing. * supplement: The JVM loads the Applet Class, the browser creates the applet, And the browser calls the init Method for initialization. If the applet's child * class has an initialization operation, the method should be overwritten. Generally, the functions implemented by this method include creating user interface components, loading resources such as images and audios *, and obtaining parameters */Public void Init () from the <APPLET> tag of an HTML webpage () {}/*** called by the browser or applet viewer to inform this applet that it * shoshould 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> shocould 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> shocould be called from * A <code> componentlistener </code>. * <p> * The implementation of this method provided by the <code> applet </code> * class does nothing. * supplement: After the init method is complete, call the start method. After browsing other webpages, you can call this method */Public void start () {}/*** called by the browser or applet viewer to inform this applet that it * shoshould 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> shocould 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. * supplement: Call the stop method when leaving the page */Public void stop () {}/*** called by the browser or applet viewer to inform this applet that it is * being reclaimed and that it shoshould 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> shocould override this method if it has * any operation that it wants to perform before it is destroyed. for * example, an applet with threads wocould 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 (){}

 

From the above description and code, we can see that the browser controls the applet through the init, start, stop, and destroy methods. These methods are usually empty methods, and these methods should be overwritten to implement the operation.

2. japplet Class Example

The Applet Class is not considered to work with the swing component, so a japplet class is extended from the Applet Class. The content pane of japplet uses the borderlayout layout manager. The following is an example of a 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 () {// receive parameters from the HTML page in the init method // string message = getparameter ("message"); add (New jlabel ("Welcome to touch's blog ", jlabel. center);} // run jappletpublic static void main (string [] ARGs) {jframe frame = new jframe ("applet is in the frame") using the main method "); myjapplet = new myjapplet (); // create a framework in the main method to place the applet. When the applet runs independently, // You must manually call the init and start Methods frame to complete the operation. add (myjapplet, borderlayout. center); myjapplet. init (); frame. setlocationrelativeto (null); frame. setdefaclocloseoperation (jframe. exit_on_close); frame. setsize (300,300); frame. setvisible (true );}}

The following example shows how to use HTML to display the 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 () {// The init method receives the string message = getparameter ("message"); add (New jlabel (message, jlabel. center);} // run jappletpublic static void main (string [] ARGs) {jframe frame = new jframe ("applet is in the frame") using the main method "); myjapplet = new myjapplet (); // create a framework in the main method to place the applet. When the applet runs independently, // You must manually call the init and start Methods frame to complete the operation. add (myjapplet, borderlayout. center); myjapplet. init (); frame. setlocationrelativeto (null); frame. setdefaclocloseoperation (jframe. exit_on_close); frame. setsize (300,300); frame. setvisible (true );}}

 

Running result:

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.