19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

18.6 Applet Life-Cycle Methods 677<br />

8<br />

9<br />

public static void main(String[] args) {<br />

// Create a frame<br />

10 JFrame frame = new JFrame("Applet is in the frame");<br />

11<br />

12 // Create an instance of the applet<br />

13 DisplayLabel applet = new DisplayLabel();<br />

14<br />

15 // Add the applet <strong>to</strong> the frame<br />

16 frame.add(applet);<br />

17<br />

18 // Display the frame<br />

19 frame.setSize(300, 100);<br />

20 frame.setLocationRelativeTo(null); // Center the frame<br />

21 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);<br />

22 frame.setVisible(true);<br />

23 }<br />

24 }<br />

new main method<br />

When the applet is run from a Web browser, the browser creates an instance of the applet and<br />

displays it. When the applet is run standalone, the main method is invoked <strong>to</strong> create a frame<br />

(line 10) <strong>to</strong> hold the applet. The applet is created (line 13) and added <strong>to</strong> the frame (line 16).<br />

The frame is displayed in line 22.<br />

Note that you can add an applet <strong>to</strong> a container, but not a frame <strong>to</strong> a container. A frame is a<br />

<strong>to</strong>p-level container that cannot be embedded in another container.<br />

18.6 How do you add <strong>com</strong>ponents <strong>to</strong> a JApplet? What is the default layout manager of<br />

JApplet?<br />

18.7 Can you place a frame in an applet?<br />

18.8 Can you place an applet in a frame?<br />

18.9 What are the differences between applications and applets? How do you run an<br />

application, and how do you run an applet? Is the <strong>com</strong>pilation process different for<br />

applications and applets?<br />

18.6 Applet Life-Cycle Methods<br />

The Web browser controls and executes applets using the applet life-cycle methods.<br />

Applets are actually run from the applet container, which is a plug-in of a Web browser. A<br />

plug-in is a software <strong>com</strong>ponent that can be added in<strong>to</strong> a larger software <strong>to</strong> provide additional<br />

functions. The Applet class contains the init(), start(), s<strong>to</strong>p(), and destroy()<br />

methods, known as the life-cycle methods. These methods are called by the applet container <strong>to</strong><br />

control the execution of an applet. They are implemented with an empty body in the Applet<br />

class, so they do nothing by default. You may override them in a subclass of Applet <strong>to</strong> perform<br />

desired operations. Figure 18.5 shows how the applet container calls these methods.<br />

✓Point✓ Check<br />

Key<br />

Point<br />

applet container<br />

Applet container<br />

creates the applet<br />

Applet container<br />

invokes init()<br />

Applet container<br />

invokes start()<br />

Applet container<br />

invokes s<strong>to</strong>p()<br />

Loaded<br />

Created<br />

Initialized<br />

Started<br />

S<strong>to</strong>pped<br />

Applet container<br />

loads the applet<br />

FIGURE 18.5<br />

Applet container<br />

invokes start()<br />

Destroyed<br />

The applet container uses the init, start, s<strong>to</strong>p, and destroy methods <strong>to</strong> control the applet.<br />

Applet container<br />

invokes destroyed()

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!