Applet is - Application of double identity

  Abstract: The Applet - Application of double identity 

  : 

  Applet & Application of double identity of the Class 

  Applet (small applications) on the web, landscaping and rich web content, from the browser to manage their life cycle (Life Cycle), which generates the (new), initialization (init), running (start), and stop (stop) and Destruction (destroy).    Write applet must be inherited from java.applet.Applet category, and in accordance with the need Heavy init () (applet was read by the system when the browser calls only call once initialized appet), the start () (browser running applet when calling), stop () (browser applet stop at the call), destroy () (applet were destroyed when the call, where the release of some of the resources needed to release) method.    As in the example is a simple, nothing to do applet: 

  / ** 
  * @ (#) MyApplet.java 
  * @ Author fancy 
  * / 

  Import java.applet .*; 

  Public class MyApplet extends Applet (/ /! Must java.applet.Applet inheritance! 

  ) 
  Application (Application) is to use the running order java java applications, it has a specific main () method to importers, to manage its life cycle.    An application must be written to achieve the above-mentioned specific main () method.    The main () method must be public (public), static (static), void (no return value), and must demand a String [] type parameters.    As in the example is a simple, the application does not do anything: 

  / ** 
  * @ (#) MyApplication.java 
  * @ Author fancy 
  * / 

  (Public class MyApplication 

  Public static void main (String [] args) (/ /! Must have a pulic static void 
  Main (String []) method! 
  ) 

  ) 
  Introduced a simple applet and application elements, we have to enter the following theme - to write a class, that is, it is applet application. 

  Based on the above application applet and the characteristics and requirements of this type must be inherited from java.applet.Applet, and the realization of the public static void main (String []), the following cases, it also does not do anything: 

  / ** 
  * @ (#) MyAppletApplication.java 
  * @ Author fancy 
  * / 

  Import java.applet .*; 

  Public class MyAppletApplication extends Applet (/ /! â‘  inherited from java.applet.Applet! 

  Public static void main (String [] args) (/ /! â‘¡ achieved public static void main (String []) method! 
  ) 

  ) 
  Above all is said does not do anything, what should we do if how do? 

  The applet is a Web browser to manage their life cycle, that is generated by the browser applet examples and call it inherited from java.applet.Applet class init (), start (), stop () and destroy () method and the application is by the self-management, life cycle generation needs its own example and call correlation method.    Therefore, we must be under the category of MyAppletApplication need to achieve init (), start (), stop () and destroy () method, the browser to its life-cycle management; At the same time, must also be in that particular main () method Generation of such examples (object), and call for the life cycle management of the relevant methods.    If 

  / ** 
  * @ (#) MyAppletApplication.java 
  * @ Author fancy 
  * / 

  Import java.applet .*; 

  Public class MyAppletApplication extends Applet ( 

  Public static void main (String [] args) ( 
  MyAppletApplication app = new yAppletApplication (); / / create an instance â‘  
……
  App.init (); / / initialization â‘¡ 
……
  App.start (); / / â‘¢ running 
……
  App.stop (); / / stop operation â‘£ 
……
  App.destroy (); / / ⑤ destruction 
……
  ) 

  ) 
  In fact, it is not enough to do these because applet Add a website, from the browser to their drawing (show), and in the application process, we need to use its mapping related components.    Fortunately, java.applet.Applet from java.awt.Componnet inheritance, it is an AWT components (Component), we need only to add it to a Window Frame or it can be realized in the drawing.    At this point, all the issues have already been resolved, NEXT cases: 

  / ** 
  * @ (#) MyAppletApplication.java 
  * @ Author fancy 
  * / 

  Import java.applet .*; 
  Import java.awt .*; 
  Import java.awt.event .*; 

  Public class MyAppletApplication extends Applet (/ / inheritance from java.applet.Applet 

  Private List list; 

  Public void init () (/ / initialization 
  List = new List (); 
  Add (list); 
  List.addItem ( "Initializing"); 
  System.out.println ( "Initializing"); 
  ) 

  Public void start () (/ / start operation 
  List.addItem ( "Starting"); 
  System.out.println ( "Starting"); 
  ) 

  Public void stop () (/ / End operation 
  List.addItem ( "Stopping") / / This can not see results 
  System.out.println ( "Stopping"); 
  ) 

  Public void destroy () (/ / destruction, 
  List.addItem ( "Destroying") / / This can not see results 
  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 (app)); 
  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); 
  ) 

  ) 

  ) 
  Note: The above example, in the stop () and destroy () add to the list is not effective, because this applet has been discontinued or is destroyed, its status as the same application.    In addition, all System.out.println () statements in the case of the applet is also fail to see. 

  Note: not all small applications may also have applications, because some small applications in the function can not be used in applications, such as Applet.getCodeBase (), Applet.getDocumentBase () in the Application Application , we will throw abnormal.    Some can be used in the Application of the content, because of security problems, we can not use the Applet, after all, Applet is posted on the Web, and we need a higher level of security. 
  Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd 
  ↑ Back 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply