The JApplet class .The applet class is an aWT class and is not designed to work with Swing components o To use Swing components in Java applets, it is necessary to create a Java applet that extends javax. swing JApplet, which is a subclass ofiava. applet, applet oJApplet inherits all the methods from the applet class. In addition, it provides support for laying out Swing components Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 16 The JApplet Class ⚫The Applet class is an AWT class and is not designed to work with Swing components. ⚫To use Swing components in Java applets, it is necessary to create a Java applet that extends javax.swing.JApplet, which is a subclass of java.applet.Applet. ⚫JApplet inherits all the methods from the Applet class. In addition, it provides support for laying out Swing components
First Simple applet // WelcomeApplet java: Applet for displaying a message import javax. swing. public class WelcomeApplet extends JApplet i ★★工 nitia1 ize the app1et*/ public void inito t getcontentPane(). add(new JLabel ("Welcome to Java JLabel CENTER)) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 17 First Simple Applet // WelcomeApplet.java: Applet for displaying a message import javax.swing.*; public class WelcomeApplet extends JApplet { /** Initialize the applet */ public void init() { getContentPane().add(new JLabel("Welcome to Java", JLabel.CENTER)); } }
First Simple applet <html> <head> <title>Welcome Java Applet</title> </head> <body> <applet code 'WelcomeApplet class width 350 height 200> </applet> </body </html> WelcomeApplet Run Applet viewer
Liang,Introduction to Java Programming,revised by Dai-kaiyu 18 First Simple Applet <html> <head> <title>Welcome Java Applet</title> </head> <body> <applet code = "WelcomeApplet.class" width = 350 height = 200> </applet> </body> </html> WelcomeApplet Run Applet Viewer
Writing Applets o Always extends the Japplet class, which is a subclass of Applet for Swing components o Override inito, start, stope, and destroyo if necessary. By default, these methods are empty o Add your own methods and data if necessary o Applets are always embedded in an HTML page Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 19 Writing Applets ⚫ Always extends the JApplet class, which is a subclass of Applet for Swing components. ⚫ Override init(), start(), stop(), and destroy() if necessary. By default, these methods are empty. ⚫ Add your own methods and data if necessary. ⚫ Applets are always embedded in an HTML page