CS 2121 Introduction to Java:
Laboratory Exercise 3

February 12, 10 points


Overview

In this exercise you will make write an applet that displays 6 useful Swing components. The components are configured with some initial data, but no behavior except for built-in behavior.

For each component, you will need to declare a local variable in an applet init() method, initialize it by invoking a constructor, and add it to the applet's content pane.

Doing this exercise requires creating a class named ComponentsApplet which extends the JApplet class. As with all applets, you will also need to write a HTML file to tell the appletviewer program what to do.

The steps below describe construction of the program, broken down into small editing changes.

Step 1 - Write the HTML File

This is just like Step 1 of Laboratory Exercise 2 except that the name of the class should be ComponentsApplet, the width should be 300, and the height should be 300.

Step 2 - Begin Defining the ComponentsApplet Class

This is just like Step 2 of Laboratory Exercise 2 except that the name of the class should be ComponentsApplet.

Step 3 - Add a JLabel to the Applet

You can add a JLabel by adding the following lines of code in the init() method.

    getContentPane.setLayout(new FlowLayout());

    JLabel label = new JLabel("My Label");
    getContentPane().add(label);

After adding this code, you can recompile your applet and run it with the appletviewer program. You should see a label in the applet.

Step 4 - Add More Components to the Applet

Add the following components to the applet.

See Swing Components and Containers for information about constructing components from the various classes.

You may want to recompile and exectute your code after adding each component. This will make it easier to isolate errors if you have any.

Step 5 - Have Your Applet Validated

Get the attention of your instructor or TA and run your applet with appletviewer for him.