Java applets
Slide 1
How applets work
An applet is a special type of application that can be downloaded from an Internet or intranet server and run on the client’s computer within a web browser. The Java Plug-in is included as part of the JRE. It is a browser plug-in that allows a web browser to use a version of the JRE that’s newer than the one that’s included with the browser. For a client machine to run applets, a current version of the JRE and Java Plug-in must be installed on the client. If a user attempts to run an applet without first installing the current version of the Java Plug-in, a prompt will be displayed indicating that the Java Plug-in must be downloaded.
applet.Applet javax.swing.JApplet
Four methods of the Applet class
Method
public void init() public void start()
public void stop() public void destroy()
Description Called when the browser first loads the applet. Called after the init method and every time the user moves to the web page for the applet. Called before the destroy method and every time the user moves to another web page. Called when the user exits the browser.
Slide 6
The inheritance hierarchy and methods of an applet
To create a Swing applet, you define a class that extends the JApplet class. Then, you can override the init, start, stop, and destroy methods of the Applet class as needed. These methods are called automatically by the browser, and they control the execution of the applet.
Slide 7
The code for the Future Value Calculator applet
import java.awt.*; import javax.swing.*; public class FutureValueApplet extends JApplet { public void init() { JPanel panel = new FutureValuePanel(); this.add(panel); } }
Slide 8
How to modify the code for the FutureValuePanel class so it can be used with an applet
public class FutureValuePanel extends JPanel implements ActionListener { // the declarations should not include an Exit button public FutureValuePanel() { // the constructor should not create an Exit button } public void actionPerformed(ActionEvent e) { // the ActionListener should not respond to the // Exit button } }
Slide 9
How to code the HTML page for an applet
The Hypertext Markup Language (HTML) is the language that’s used to create web pages. Each HTML tag begins with the tag name and ends with the tag name prefixed by a forward slash. Within a tag, you can set...