Building and Running Applets

   


The first two programs presented in this book are Java applications, stand-alone programs like any native programs. On the other hand, as we mentioned in the last chapter, most of the hype about Java comes from its ability to run applets inside a web browser. We want to show you how to build and run an applet from the command line. Then we will load the applet into the applet viewer that comes with the JDK. Finally, we will display it in a web browser.

First, open a shell window and go to the directory CoreJavaBook/v1ch2/WelcomeApplet, then enter the following commands:

 javac WelcomeApplet.java appletviewer WelcomeApplet.html 

Figure 2-10 shows what you see in the applet viewer window.

Figure 2-10. WelcomeApplet applet as viewed by the applet viewer


The first command is the now-familiar command to invoke the Java compiler. This compiles the WelcomeApplet.java source into the bytecode file WelcomeApplet.class.

This time, however, you do not run the java program. You invoke the appletviewer program instead. This program is a special tool included with the JDK that lets you quickly test an applet. You need to give this program an HTML file, rather than the name of a Java class file. The contents of the WelcomeApplet.html file are shown below in Example 2-3.

Example 2-3. WelcomeApplet.html
  1. <html>  2.    <head>  3.       <title>WelcomeApplet</title>  4.    </head>  5.    <body>  6.       <hr/>  7.       <p>  8.          This applet is from the book  9.          <a href="http://www.horstmann.com/corejava.html">Core Java</a> 10.          by <em>Cay Horstmann</em> and <em>Gary Cornell</em>, 11.          published by Sun Microsystems Press. 12.       </p> 13.       <applet code="WelcomeApplet.class" width="400" height="200"> 14.          <param name="greeting" value ="Welcome to Core Java!"/> 15.       </applet> 16.       <hr/> 17.       <p><a href="WelcomeApplet.java">The source.</a></p> 18.    </body> 19. </html> 

If you are familiar with HTML, you will notice some standard HTML instructions and the applet tag, telling the applet viewer to load the applet whose code is stored in WelcomeApplet.class. The applet viewer ignores all HTML tags except for the applet tag.

The other HTML tags show up if you view the HTML file in a Java 2-enabled browser. Unfortunately, the browser situation is a bit messy.

  • Mozilla (and Netscape 6 and above) support Java 2 on Windows, Linux, and Mac OS X. To experiment with applets, just download the latest version and make sure Java is enabled.

  • Some versions of Internet Explorer have no support for Java at all. Others only support the very outdated Microsoft Java Virtual Machine. If you run Internet Explorer on Windows, go to http://java.com and install the Java Plug-in.

  • If you have a Macintosh running OS X, then Safari and Internet Explorer are integrated with the Macintosh Java implementation, which supports JDK 1.4 at the time of this writing. OS 9 supports only the outdated version 1.1.

Provided you have a browser that supports the Java 2 platform, you can try loading the applet inside the browser.

1.

Start your browser.

2.

Select File -> Open File (or the equivalent).

3.

Go to the CoreJavaBook/v1ch2/WelcomeApplet directory.

You should see the WelcomeApplet.html file in the file dialog. Load the file. Your browser now loads the applet, including the surrounding text. It will look something like Figure 2-11.

Figure 2-11. Running the WelcomeApplet applet in a browser


You can see that this application is actually alive and willing to interact with the Internet. Click on the Cay Horstmann button. The applet directs the browser to display Cay's web page. Click on the Gary Cornell button. The applet directs the browser to pop up a mail window, with Gary's e-mail address already filled in.

Notice that neither of these two buttons works in the applet viewer. The applet viewer has no capabilities to send mail or display a web page, so it ignores your requests. The applet viewer is good for testing applets in isolation, but you need to put applets inside a browser to see how they interact with the browser and the Internet.

TIP

You can also run applets from inside your editor or integrated development environment. In Emacs, select JDE -> Run Applet from the menu. In Eclipse, use the Run -> Run as -> Java Applet menu option.


Finally, the code for the applet is shown in Example 2-4. At this point, do not give it more than a glance. We come back to writing applets in Chapter 10.

In this chapter, you learned about the mechanics of compiling and running Java programs. You are now ready to move on to Chapter 3, where you will start learning the Java language.

Example 2-4. WelcomeApplet.java
  1. import javax.swing.*;  2. import java.awt.*;  3. import java.awt.event.*;  4. import java.net.*;  5.  6. public class WelcomeApplet extends JApplet  7. {  8.    public void init()  9.    { 10.       setLayout(new BorderLayout()); 11 12.       JLabel label = new JLabel(getParameter("greeting"), SwingConstants.CENTER); 13.       label.setFont(new Font("Serif", Font.BOLD, 18)); 14.       add(label, BorderLayout.CENTER); 15. 16.       JPanel panel = new JPanel(); 17. 18.       JButton cayButton = new JButton("Cay Horstmann"); 19.       cayButton.addActionListener(makeURLActionListener( 20.          "http://www.horstmann.com")); 21.       panel.add(cayButton); 22. 23.       JButton garyButton = new JButton("Gary Cornell"); 24.       garyButton.addActionListener(makeURLActionListener( 25.          "mailto:gary@thecornells.com")); 26.       panel.add(garyButton); 27. 28.       add(panel, BorderLayout.SOUTH); 29.    } 30. 31.    private ActionListener makeURLActionListener(final String u) 32.    { 33.       return new 34.          ActionListener() 35.          { 36.             public void actionPerformed(ActionEvent event) 37.             { 38.                try 39.                { 40.                   getAppletContext().showDocument(new URL(u)); 41.                } 42.                catch(MalformedURLException e) 43.                { 44.                   e.printStackTrace(); 45.                } 46.             } 47.          }; 48.    } 49. } 


       
    top



    Core Java 2 Volume I - Fundamentals
    Core Java(TM) 2, Volume I--Fundamentals (7th Edition) (Core Series) (Core Series)
    ISBN: 0131482025
    EAN: 2147483647
    Year: 2003
    Pages: 132

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net