|
|
Using the JFC/Swing Packages |
This page tells you how to compile and run a Swing application. If you wish, you can skip directly to A Tour Through "Hello Swing", which guides you through the code for a simple application. If you're interested in using Swing in applets, you'll also want to read Running a Swing Applet.The following instructions assume that you've written and compiled Java programs before. If you haven't, please follow the instructions in The "Hello World" Application
. Once you've successfully compiled and run the "Hello World" application, you should be able to set up and start using your Swing programming environment. Just follow these steps:
- Download the latest JDK release, if you haven't already done so.
- Download the latest JFC release, if you need to.
- Create a program that uses Swing components.
- Compile the program.
- Run the program.
Download the Latest JDK Release
The Swing software works with both JDK 1.1 and JDK 1.2. As a rule, you should download the latest version of either JDK 1.1 or JDK 1.2.Download the Latest JFC Release
If you're using the 1.1 JDK release, you need to download the latest version of JFC 1.1.If you're using JDK 1.2, then you don't necessarily need to download the JFC -- it's already included in the JDK 1.2 release. However, the version in the JDK 1.2 release currently lags the version in the JFC release. For example, the JDK 1.2 Beta 3 Swing classes are based on Swing 1.0.1, but the latest version of JFC 1.1 is based on Swing 1.0.2. For this reason, you'll probably want to download JFC 1.1.
Create a Program that Uses Swing Components
You can use a simple program we provide, calledHelloSwing. It's explained in A Tour Through "Hello Swing". Choose one of these versions:Then save the
HelloSwing.javafor JFC 1.1HelloSwing.javafor JDK 1.2HelloSwingprogram in a file named -- you guessed it --HelloSwing.java.
Note: The only difference between the two versions ofHelloSwingis that the JFC 1.1 version importscom.sun.java.swing.*, while the JDK 1.2 version importsjava.awt.swing.*. If you're using JDK 1.2 with JFC 1.1, then you should use the JFC 1.1 version ofHelloSwing.
Compile a Program that Uses Swing Components
Your next step is to compile the programIf you're compiling with JFC 1.1, you need to add the Swing class archive,
swing.jar, to the class path. You can find theswing.jarfile in the top directory of the JFC release.If you're compiling under JDK 1.2 (without JFC 1.1), then you don't need to worry about the Swing class archive -- it's already included in the 1.2 JDK release. You don't have to specify a class path; just compile as usual.
Here's a general explanation of how to compile a Swing application with JFC 1.1.
- Make a note of where your copy of the JFC (Swing) release is installed. You'll need this to be able to find the Swing class archive,
swing.jar. You might want to set the environment variable SWING_HOME to the top directory of the Swing release.
Note: Don't bother unarchivingswing.jar!
- Make a note of where your copy of the JDK release is installed. You'll need this to be able to find the proper versions of the JDK classes and interpreter. You might want to set the environment variable
JAVA_HOMEto the top directory of the JDK release.The JDK classes are in the
libdirectory of the JDK release, in a file calledclasses.zip. Don't uncompress that file! The Java interpreter is in thebindirectory of the JDK release.
- Compile the application, specifying a classpath that includes the
swing.jarfile, the JDKclasses.zipfile, and the directory containing the program's classes (usually "."). Be sure that the JDKclasses.zipfile and the interpreter you use are from exactly the same release of the JDK!Run the Program
Once the program has successfully compiled, you can run it.If you're using JFC 1.1, you need to make sure that the class path includes not only what you needed to compile the file, but also the archive file for the look and feel the program uses. The JavaTM Look and Feel, which is the default, is in the
swing.jarfile. The Windows look and feel is inwindows.jar, and the CDE/Motif look and feel is inmotif.jar. The "Hello Swing" application uses the Java Look and Feel, so you need onlyswing.jarin the class path.If you're using the Swing classes in the 1.2 Beta 3 JDK release, then you don't have to specify a class path, as long as you use one of the following look and feels: Windows, CDE/Motif, or Java Look and Feel.
What Next?
If you've gotten this far, you're ready to start writing some Swing code! Here's where you might want to go from here:
- A Tour Through "Hello Swing", which describes the
HelloSwingprogram.- Running a Swing Applet, which teaches you how to use Swing in applets.
- The Swing Connection, which you should visit regularly to find the latest news and documentation for Swing.
|
|
Using the JFC/Swing Packages |