Return to Unit Menu | Java Main Page | | Terms of Use  

Creating Applets for your Web Pages
(It is assumed that you are familiar with creating web pages and have a familiarity with HTML code.)

An applet is a Java application that runs on a web page.
In order to run an applet, you must have:
1. a compiled Java applet in a .class file.
    (place applets in the same directory as the HTML file)
2. HTML code that contains an applet markup tag

Converting an Application to an Applet
We have been using the class GBFrame to provide the framework for our BreezuGUI-based applications.  To create applets, we will use a similar class called GBApplet.

Examine this converted code.  Note the changes.

import java.awt.*;
import BreezyGUI.*;

public class OuncesPounds extends GBApplet
     Label poundsLabel = addLabel ("Pounds",1,1,1,1);
     Label ouncesLabel = addLabel ("Ounces",2,1,1,1);

     DoubleField poundsField = addDoubleField (0 ,1,2,1,1);
     DoubleField ouncesField = addDoubleField (0 ,2,2,1,1);

     Button poundsButton = addButton ("Compute Pounds",3,1,1,1);
     Button ouncesButton = addButton ("Compute Ounces", 3,2,1,1);

     public void buttonClicked (Button buttonObj)
          double pounds, ounces;
          if (buttonObj == ouncesButton)
                pounds = poundsField.getNumber();
                ounces = 16 * pounds;
                ounces = ouncesField.getNumber();
// There is no main!
//In addition, do not use the
setTitle method.

Now adjust your HTML code:
An applet markup tag looks like:

<applet code="applet_name.class" width = 250 height = 150> </applet>

The width and height are measured in pixels and define the size of the box that will appear on the screen.


Let's look at the html code adjustment for our program.
<title>Ounces to Pounds Conversions</title>

<applet code="OuncesPounds.class" width=200 height=150> </applet>



Return to Unit Menu | Java Main Page | | Terms of Use