Getting Started with Swing |
Here are the steps for compiling and running your first Swing program with JDK 1.1 and JFC/Swing:
- Download the latest JDK 1.1 release, if you haven't already done so.
- Download the latest JFC 1.1 release.
- Create a program that uses Swing components.
- Compile the program.
- Run the program.
Download the Latest JDK 1.1 Release
You can download the reference implementation of JDK 1.1 for free from java.sun.com. Just go to the appropriate page for your platform -- Solaris or Win32.Download the Latest JFC/Swing Release
You can download the latest JFC 1.1 release from the JFC home page. This trail describes the Swing 1.1 version of JFC 1.1.Create a Program That Uses Swing Components
You can use a simple program we provide, calledSwingApplication
. Please download and save this file:SwingApplication.java
. The spelling and capitalization of the file's name must be exactly "SwingApplication.java".Compile a Program That Uses Swing Components
Your next step is to compile the program. Here's a general explanation of how to compile a Swing application with JDK 1.1.
- Make a note of where your copy of the JFC 1.1 (Swing 1.1) release is installed. The Swing class archive file,
swing.jar
, is in the top directory of this release. You might want to create an environment variable calledSWING_HOME
that contains the path of the top directory of the JFC 1.1 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 create an environment variable,
JAVA_HOME
, and set it to the top directory of the JDK release.The JDK classes are in the
lib
directory of the JDK release, in a file calledclasses.zip
. Don't uncompress that file! The interpreter for the Java programming language is in thebin
directory of the JDK release.
- Compile the application, specifying a class path that includes the
swing.jar
file, the JDKclasses.zip
file, and the directory containing the program's classes (usually "."). Be sure that the JDKclasses.zip
file and the compiler you use are from exactly the same release of the JDK!The following example shows how to compile
SwingApplication
on a UNIX system. It assumes that you've set up the environment variablesJAVA_HOME
andSWING_HOME
.$JAVA_HOME/bin/javac -classpath .:$SWING_HOME/swing.jar:$JAVA_HOME/lib/classes.zip SwingApplication.javaIf you choose not to use the environment variables, you might instead use a command like this:
Here's an example of compiling on Win32:javac -classpath .:/home/me/swing-1.1/swing.jar:/home/me/jdk1.1.7/lib/classes.zip SwingApplication.javaHere's an alternative that doesn't use environment variables:%JAVA_HOME%\bin\javac -deprecation -classpath .;%SWING_HOME%\swing.jar;%JAVA_HOME%\lib\classes.zip SwingApplication.javajavac -deprecation -classpath .;C:\java\swing-1.1\swing.jar;C:\java\jdk1.1.7\lib\classes.zip SwingApplication.java
Note: If you can't compileSwingApplication.java
, it's probably due either to having the wrong files in your class path or to using a version of JFC 1.1 that has old Swing API. You should be able to use the programs in this book without change once you update to the most recent JFC 1.1 release. Before Swing 1.1 Beta 3, the Swing API used different package names. Here is how to changeSwingApplication.java
to 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
Run the Program
Once the program has successfully compiled, you can run it.Make sure that the interpreter's 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 Java Look & Feel, which is the default, is in the
swing.jar
file. The Windows Look & Feel is inwindows.jar
, and the CDE/Motif Look & Feel is inmotif.jar
. You aren't limited to these look-and-feel options: you can use any look and feel designed for use with the Swing 1.1 API.This application uses the Java Look & Feel, so you need only
swing.jar
in the class path. Thus, the command for running it is similar to the command for compiling it. Just substitutejava
forjavac
, and remove the.java
suffix. For example, on UNIX:java -classpath .:/home/me/swing-1.1/swing.jar:/home/me/jdk1.1.7/lib/classes.zip SwingApplicationHere's an example of running an application that uses the Windows Look & Feel:
While you're developing your application, you can simplify the class path by using%JAVA_HOME%\bin\java -classpath .;%SWING_HOME%\swing.jar;%JAVA_HOME%\lib\classes.zip;%SWING_HOME%\windows.jar SomeClassswingall.jar
, which includes all the classes in the JFC 1.1 release. So instead of puttingswing.jar
andwindows.jar
in your class path, for example, you can just put inswingall.jar
.
Important: Avoid usingswingall.jar
in your final application. It contains information used by builders, as well as more look-and-feel packages than a typical application uses. [PENDING: check] You can save space by simply using theswing.jar
file plus any look-and-feel archives that you need.
Getting Started with Swing |