|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
Writing the Standalone ApplicationIt is not much work at all to convert an applet into a standalone Java application. To do so, follow these steps:
Listing 37.12 shows a completed Java application. Listing 37.12 HelloGUI.javaA Simple Standalone Java Application /** * HelloGUI: Standalone Java application * * NOTES: * Compare this standalone Java application * with HelloApplet applet. * * @version 1.0, DATE: 07.07.98 * @author Mike Morgan */ import java.awt.*; // User Interface components public class HelloGUI extends Frame { private static Font f; private void drawCenteredString (Graphics g, String s) { Dimension d = getSize (); FontMetrics fm = getFontMetrics (f); int x = (d.width - fm.stringWidth (s)) / 2; int y = d.height - ( (d.height - fm.getHeight ()) / 2 ); g.drawString (s, x, y); } // paint: window refresh code goes here public void paint (Graphics g) { g.setFont (f); g.setColor (Color.red); drawCenteredString (g, Hello from Java!); } // main: application initialization code goes here public static void main (String[] args) { Frame theFrame = new HelloGUI (); theFrame.setTitle (Hello World!); theFrame.setSize (250, 100); f = new Font (Helvetica, Font.BOLD, 24); theFrame.show (); } } The biggest practical difference between applets running on a Web page and standalone applications is that the application can generally be trusted and allowed to do more: open files, establish TCP/IP connections with arbitrary computers, and so on. Applets, on the other hand, are restricted from doing anything that might compromise the security of the host computer. The restricted environment that Java-enabled browsers set up for applets to run in is called a sandbox. To learn more about the sandbox, see Executable Content and Security on p. 1142. Compiling and Running the Standalone ApplicationYou can compile and run the standalone application from the command line: javac HelloGUI.java java HelloGUI If you were to run this application in AppletViewer, the output will look like what you saw earlier in this chapter as Figure 37.5.
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement. |