|
|
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, don't download the JFC -- it's already included in the JDK 1.2 release.
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". Please download the fileHelloSwing.javaand save it in a file. The spelling and capitalization of the file's name must be exactly like this:HelloSwing.java.Compile a Program that Uses Swing Components
Your next step is to compile the program.If 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, 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.
Note: If you can't compileHelloSwing.java, it's probably due either to having the wrong files in your class path (if you're using JDK 1.1) or to a package name mismatch. Currently, Swing 1.1 Beta 3 is the only release that uses the package names used inHelloSwing.java. Here is how to changeHelloSwing.javato use the old package names:See Swing Package Names for more information about differences in package names.//import javax.swing.*; //comment out this line import com.sun.java.swing.*; //uncomment this line
- 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 JDK 1.2, 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 |