19.09.2015 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

676 Chapter 18 Applets and Multimedia<br />

signed applet<br />

VideoNote<br />

Run applets standalone<br />

create frame<br />

create applet<br />

add applet<br />

show frame<br />

run standalone<br />

✓Point✓ Check<br />

Key<br />

Point<br />

Note<br />

You can create signed applets <strong>to</strong> circumvent the security restrictions. See Supplement<br />

III.S, Signed Applets, for detailed instructions on how <strong>to</strong> create signed applets.<br />

18.5 List some security restrictions on applets.<br />

18.5 Enabling Applets <strong>to</strong> Run as Applications<br />

You can add a main method in the applet <strong>to</strong> enable the applet <strong>to</strong> run as a standalone<br />

application.<br />

Despite some differences, the JFrame class and the JApplet class have a lot in <strong>com</strong>mon.<br />

Since they both are subclasses of the Container class, all their user-interface <strong>com</strong>ponents,<br />

layout managers, and event-handling features are the same. Applications, however, are<br />

invoked from the static main method by the <strong>Java</strong> interpreter, and applets are run by the Web<br />

browser. The Web browser creates an instance of the applet using the applet’s no-arg construc<strong>to</strong>r<br />

and controls and executes the applet.<br />

In general, an applet can be converted in<strong>to</strong> an application without loss of functionality. An<br />

application can be converted in<strong>to</strong> an applet as long as it does not violate the security restrictions<br />

imposed on applets. You can implement a main method in an applet <strong>to</strong> enable the applet<br />

<strong>to</strong> run as an application. This feature has both theoretical and practical implications. Theoretically,<br />

it blurs the difference between applets and applications: You can write a class that is<br />

both an applet and an application. From the standpoint of practicality, it is convenient <strong>to</strong> be<br />

able <strong>to</strong> run a program both ways.<br />

How do you write such programs? Suppose you have an applet named MyApplet. To<br />

enable it <strong>to</strong> run as an application, you only need <strong>to</strong> add a main method in the applet, as follows:<br />

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

// Create a frame<br />

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

}<br />

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

MyApplet applet = new MyApplet();<br />

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

frame.add(applet, BorderLayout.CENTER);<br />

// Display the frame<br />

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

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

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

frame.setVisible(true);<br />

You can revise the DisplayLabel class in Listing 18.1 <strong>to</strong> enable it <strong>to</strong> run as a standalone<br />

application (often abbreviated as “run standalone”) by adding a main method, as shown in<br />

Listing 18.3.<br />

LISTING 18.3<br />

New DisplayLabel.java with a main Method<br />

1 import javax.swing.*;<br />

2<br />

3 public class DisplayLabel extends JApplet {<br />

4 public DisplayLabel() {<br />

5 add(new JLabel("Great!", JLabel.CENTER));<br />

6 }<br />

7

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

Saved successfully!

Ooh no, something went wrong!