Applet vs application WelcomeApplet java: Applet for displaying a message import javax. swing. X public class WelcomeApplet extends JApplet /*大工 nitia1 ize the app1et public void init() add(new JLabel (" Welcome to Java JLabel center))i import javax. swIng.大 public class My FrameWithlable public static void main(String[] args)i JFrame frame new JFrame("My FrameWithLable")i frame. add (new JLabel(" Welcome to Java", Jlabel center))i frame setsize(400, 300) frame setvisible(true)i frame setDefaultCloseOperation (JFrame EXIT ON CLOSE)i frame. setLocationRelativeTo(null);// New since JDK 1.4 Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 6 Applet vs. Application import javax.swing.*; public class MyFrameWithlable{ public static void main(String[] args) { JFrame frame = new JFrame("MyFrameWithLable"); frame.add(new JLabel("Welcome to Java", JLabel.CENTER)); frame.setSize(400, 300); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setLocationRelativeTo(null); // New since JDK 1.4 } } // WelcomeApplet.java: Applet for displaying a message import javax.swing.*; public class WelcomeApplet extends JApplet { /** Initialize the applet */ public void init() { add(new JLabel("Welcome to Java", JLabel.CENTER)); } }
The Applet Class, cont Applets are created, run, and destroyed by web browser Dont set size for an applet: determined by html file Dont set title for an applet: applets cannot have title bars Can have menus No need to explicitly construct an applet. Construction code placed inside the init method There is no main method An applet cannot be closed. It terminates automatically when the browser No need to call method show An applet is displayed automatically Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 7 The Applet Class, cont. Applets are created, run, and destroyed by web browser – Don’t set size for an applet: determined by HTML file. – Don’t set title for an applet: applets cannot have title bars. Can have menus. – No need to explicitly construct an applet. Construction code placed inside the init method. – There is no main method. – An applet cannot be closed. It terminates automatically when the browser exit. – No need to call method show. An applet is displayed automatically
The Applet Class, cont When the applet is loaded, the Web browser creates an instance of the applet by invoking the applets no-arg constructor The browser uses the init, start stop, and destroy methods to control the applet. By default. these methods do nothing. To perform specific functions, they need to be modified in the user's applet so that the browser can call your code pI roper Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 8 The Applet Class, cont. When the applet is loaded, the Web browser creates an instance of the applet by invoking the applet’s no-arg constructor. The browser uses the init, start, stop, and destroy methods to control the applet. By default, these methods do nothing. To perform specific functions, they need to be modified in the user's applet so that the browser can call your code properly
The Applet Class public class MyApplet extends java applet Applet i /* The no-arg constructor is called by the browser when the Web page containing this applet is initially loaded, or reloaded public MyApplet ()( /** Called by the browser after the applet is loaded public void init() /* Called by the browser after the init() method every time the Web page is visited public void start ()t /* Called by the browser when the page containing this applet becomes inactive public void stop( /* Called by the browser when the Web browser exits * publi d destroy() t /* other methods if necessary
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 9 The Applet Class public class MyApplet extends java.applet.Applet { ... /** The no-arg constructor is called by the browser when the Web page containing this applet is initially loaded, or reloaded */ public MyApplet() { ... } /** Called by the browser after the applet is loaded */ public void init() { ... } /** Called by the browser after the init() method, or every time the Web page is visited */ public void start() { ... } /** Called by the browser when the page containing this applet becomes inactive */ public void stop() { ... } /** Called by the browser when the Web browser exits */ public void destroy() { ... } /** Other methods if necessary... */ }
Browser Calling applet methods oade JVM loads the reload enters web page applet class Browser creates the applet Created B invokes initO aftcr inil Initialized Browser BI rowser y return to pa invokes start invokes stop start k slop!) invokes stop Started Stopped eo to exits another page Browser Browser invokes destroy() Destroyed Liang, Introduction to Java Programming, Sixth Edition, (c)2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved. 0-13-148952-6 10 Browser Calling Applet Methods Browser invokes start() Destroyed Browser invokes destroy() Browser invokes stop() Loaded Initialized Browser invokes init() Started Stopped Created Browser creates the applet JVM loads the applet class Browser invokes stop() Browser invokes start()