The JFrame class and the JApplet class have a lot in common despite some differences. Since they both are subclasses of the Container class, all their user -interface components , layout managers, and event-handling features are the same. Applications, however, are invoked from the static main method by the Java interpreter JVM, and applets are run by the Web browser. The Web browser creates an instance of the applet using the applet's no-arg constructor and controls and executes the applet through the init , start , stop , and destroy methods .
For security reasons, the restrictions listed below are imposed on applets to prevent destructive programs from damaging the system on which the browser is running.
Applets are not allowed to read from, or write to, the file system of the computer. Otherwise, they could damage the files and spread viruses.
Applets are not allowed to run programs on the browser's computer. Otherwise, they might call destructive local programs and damage the local system on the user's computer.
Applets are not allowed to establish connections between the user's computer and any other computer, except for the server where the applets are stored. This restriction prevents the applet from connecting the user's computer to another computer without the user's knowledge.
Note
A new security protocol was introduced in Java 2 to allow trusted applets to circumvent the security restrictions. See http://www.developer.com/java/ent/article.php/3303561 for detailed instructions on how to create trusted applets. |
In general, an applet can be converted to an application without loss of functionality. An application can be converted to an applet as long as it does not violate the security restrictions imposed on applets. You can implement a main method in an applet to enable the applet to run as an application. This feature has both theoretical and practical implications. Theoretically, it blurs the difference between applets and applications. You can write a class that is both an applet and an application. From the standpoint of practicality, it is convenient to be able to run a program in two ways.
It is not difficult to write such programs on your own. Suppose you have an applet named MyApplet . To enable it to run as an application, all you need to do is add a main method in the applet with the implementation, as follows :
1 public static void main(String[] args) { 2 // Create a frame 3 JFrame frame = new JFrame( "Applet is in the frame" ); 4 5 // Create an instance of the applet 6 MyApplet applet = new MyApplet(); 7 8 // Add the applet to the frame 9 frame.add(applet, BorderLayout.CENTER); 10 11 // Invoke init and start 12 applet.init(); 13 applet.start(); 14 15 // Display the frame 16 frame.setLocationRelativeTo( null ); // Center the frame 17 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 18 frame.setSize (300 , 300 ) ; 19 frame.setVisible( true ); 20 }
The main method creates a frame to hold an applet (line 13). When the applet is run from a Web browser, the Web browser invokes the init and start methods of the applet. When the applet is run standalone, you have to manually invoke the init and start methods in order to perform the operations in these methods (lines 12 “13).
You can revise the LoanApplet class in Listing 16.1 to enable LoanApplet to run standalone by adding a main method in Listing 16.3.
1 import java.awt.*; 2 import java.awt.event.*; 3 import javax.swing.*; 4 import javax.swing.border.TitledBorder; 5 6 public class LoanApplet extends JApplet { 7 // Same code in Listing 16.1 from line 7 to line 78 8 ... 9 10 public static void main(String[] args) { 11 // Create a frame 12 JFrame frame = new JFrame( "Applet is in the frame" ); 13 14 // Create an instance of the applet 15 LoanApplet applet = new LoanApplet(); 16 17 // Add the applet to the frame 18 frame.add(applet, BorderLayout.CENTER); 19 20 // Invoke applet's init method 21 applet.init(); 22 23 // Display the frame 24 frame.setLocationRelativeTo( null ); // Center the frame 25 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 26 frame.setSize( 300 , 300 ); 27 frame.setVisible( true ); 28 } 29 } |