Applets are used in web pages, beautify and enrich web pages, managed by the browser, Life Cycle, that is, Generate (New), Initiation (Init), Run (STOP) ) And destruction (Destroy) and so on. Write the applet, you must inherit from the java.applet.applet class, and reload init () as needed (Applet is called by the browser, only once, initialize the APPET), start () (Browser Run Applet Call), stop () () When the browser stops Applets), DESTROY () (the Applet is called, where some resource needs to be released here). As the following example is a simple, what doesn't do anything don't do.
/ **
* @ (#) Myapplet.java
* @Author fancy
* /
Import java.applet. *;
Public class myapplet extends applet {//! Must inherit from java.applet.applet!
}
Application (application) is a Java application that can run using the java command, which manages its lifecycle with a specific main () method. Writing an Application must implement the above-described specific main () method. The main () method must be a public (public), Static (static), VOID (no reference), and must require a string [] type parameter. As the following example is a simple, what does not do anything:
/ **
* @ (#) MyApplication.java
* @Author fancy
* /
Public class myapplication {
Public static void main (String [] args) {//! There must be a Pulic Static void main (String []) method!
}
}
After briefly introduced the key points of Applet and Application, we have to enter the topic - write a class, which is applet and Application.
According to the features and requirements of the above Applet and Application, this class must inherit from java.applet.applet, and implements the public static void main (String []) method, such as the following example, it does not do anything:
/ **
* @ (#) MyAppletApplication.java
* @Author fancy
* /
Import java.applet. *;
Public class myappletApplication extends applet {//! 1 inherits from java.applet.applet!
Public static void main (string [] args) {//! 2 implements public static void main (String []) method!
}
}
The various classes said above don't do anything, what should I do if I want to do?
Since Applet is a browser manages its lifecycle, it generates an applet instance and calls it from Java.applet.Applet class, and Application is Manage life cycles by yourself, you need to generate instances and call related methods. So our MyAppletApplication class must implement init (), start (), stop (), and destroy (), etc., so that the browser is managing its life cycle; at the same time, it must also be in that particular main () method. Generate instances (objects) of this class, and calls the relevant methods for the life cycle of the management period. Such as/**
* @ (#) MyAppletApplication.java
* @Author fancy
* /
Import java.applet. *;
Public class myappletApplication extends applet {
Public static void main (String [] args) {
MyAppletApplication App = New YAPPLICATION (); // 1 Generate Instance
......
App.init (); // 2 Initialization
......
App.Start (); // 3 start running
......
app.stop (); // 4 Stop operation
......
app.destroy (); // 5 destroy
......
}
}
In fact, these still is still not enough, because an applet is placed in a web page, which is responsible for drawing (display) by the browser, and in the application, we need to draw it using the relevant part. Fortunately, java.applet.applet is inherited from java.awt.componnet, which is an AWT component (Component), we only need to add it to a Frame or Window to achieve it. At this point, all the problems have been solved, please see the case:
/ **
* @ (#) MyAppletApplication.java
* @Author fancy
* /
Import java.applet. *;
Import java.awt. *;
Import java.awt.event. *;
Public class myappletApplication extends applet {// inherited from java.applet.applet
PRIVATE LIST
Public void init () {// initialization
List = new list ();
Add (List);
List.additem ("inTILALIZING");
System.out.println ("Initializing");
}
Public void start () {// Start running
List.additem ("starting");
System.out.println ("starting");
}
Public void stop () {// End the run
List.additem ("stopping"); // This sentence can not see the effect
System.out.println ("stopping");
}
Public void destroy () {// When destroy
List.additem ("destroying"); // This sentence does not see the effect
System.out.println ("destroying");
Public static void main (String [] args) {
MyAppletApplication App = New MyAppletApplication ();
app.init ();
app.start ();
Frame frame = new frame ();
Frame.Add (app);
Frame.AddWindowListener (New MyWindowListener (AP));
Frame.setsize (200, 150);
Frame.show ();
}
Public static class myWindowlistener extends windowadapter {
Applet applet;
Public myWindowListener (Applet applet) {
THIS.Applet = applet;
}
Public void windowclosing (windowevent event) {
applet.stop ();
applet.destroy ();
System.exit (0);
}
}
}
Remarks: In the above example, add items for the List () and destroy () are not seen, because the applet has stopped or destroyed, it is the same as Application. In addition, all System.out.Println () statements can be seen when the previous example acts as an applet.
Note: Not all small applications may also be an application, because there are some features in the applet cannot be used in applications, such as applet.getCodeBase (), applet.getdocumentbase (), etc. in Application It will be throwing it. Some content you can use in Application, due to security issues, you can't use in Applets. After all, Applet is to be published online and require higher security.