Sun Java Solaris Communities My SDN Account Join SDN
 
Tutorials & Code Camps

Creating Your First JFC Application

 
Training Index


[Help | Solution | API Docs | Short Course | Magercises]

The Java Foundation Classes (JFC) encompass both the Abstract Window Toolkit (AWT) of old and the new Swing components and architecture. The Swing components can be used like AWT components, including 1.1 event handling. In this Magercise, you will setup the environment to compile, then modify, a basic application that displays a label and two buttons. Selecting a button displays a message to the screen.

Magercise 2 Prerequisites

None.

Skeleton Code

FirstAppl.java

Tasks

  1. If you are using Java Development Kit 1.1.x, you need to add the swingall.jar file to your class path to compile your Swing programs. In the Installation magercise, you installed Swing and set the SWINGHOME environment variable to the installation directory. Now, you need to add the archives to the CLASSPATH.

  2. Starting with the skeleton code, import the swing package.

  3. To have the components appear one on top of each other, modify the skeleton's constructor so that it sets the layout to GridLayout with 3 rows and 1 column.

  4. Create top and bottom JButtons with action event listeners that print "top" and "bottom" to standard output.

  5. Create a JLabel with text "Swing Components are like AWT 1.1". Add it to the first row of the grid.

  6. Add the top and bottom button to the grid.

  7. Save everything and compile the program. Then run it to see the results. Clicking a button displays a message to the screen.

    As a precaution, the Save command appends a "1" to the end of the filename you want to save. This can help prevent you from accidently overwriting your source code.

[Result.gif]

Where help exists, the task numbers above are linked to the step-by-step help page.

The following Java source file represents a solution to this Magercise.

Solution

Magercise 3

Short Course

About This Short Course

Copyright © 1998-1999 MageLang Institute. All Rights Reserved.