Suns Java Swing Tutorial.pdf

(4526 KB) Pobierz
Getting Started with Swing
Getting Started with Swing
The Java TM Tutorial
Less on
Contents
Trail : Creating a GUI with JFC/Swing
Lesson: Getting Started with Swing
This lesson gives you a quick introduction to using the JFC Swing packages to create a
program's GUI. First, it tells you what the JFC and Swing are. Next, it helps you get the
necessary releases and use them to compile and run a Swing program. Then it shows you
how to run applets. Finally, it takes you on a whirlwind tour of a simple Swing application.
If you're more interested in learning concepts than in running programs right now, feel free
to skip part or all of this lesson and to go to the next lesson, Swing Features and Concepts (in
.
Then return here when you're ready to start programming.
This section gives you a little history about the JFC and Swing. If you've developed
programs using AWT components, you'll probably be interested in the description of the
differences between AWT and Swing components.
To write programs using the Swing components, you must first download the appropriate
JDK and JFC releases. Then you can follow this section's instructions to compile and run the
small Swing application provided.
To write Swing applets, you must first be able to run them. This section provides two
applets, with instructions for running them.
This section takes you through the code for a small Swing application. You'll see the code
that a Swing application absolutely must have, and you'll learn about some commonly used
Swing features.
Less on
Contents
http://web2.java.sun.com/docs/books/tutorial/uiswing/start/index.html [16/06/2000 3:20:52 PM]
Page
Page
37926101.009.png 37926101.010.png
About the JFC and Swing
The Java TM Tutorial
Trail : Creating a GUI with JFC/Swing
Lesson : Getting Started with Swing
About the JFC and Swing
This section answers four questions:
What are the JFC and Swing?
l
Which releases contain the Swing API?
l
What Swing packages should I use?
l
How are Swing components different from AWT components?
What Are the JFC and Swing?
JFC is short for Java TM Foundation Classes, which encompass a group of features to help
people build graphical user interfaces (GUIs). The JFC was first announced at the 1997
JavaOne developer conference and is defined as containing the following features:
The Swing Components
Include everything from buttons to split panes to tables. You can see mugshots of all
Pluggable Look and Feel Support
Gives any program that uses Swing components a choice of looks and feels. For
example, the same program can use either the Java TM look and feel or the Windows
look and feel. We expect many more look-and-feel packages -- including some that
use sound instead of a visual "look" -- to become available from various sources.
Accessibility API
Enables assistive technologies such as screen readers and Braille displays to get
information from the user interface.
Java 2D TM API (Java 2 Platform only)
Enables developers to easily incorporate high-quality 2D graphics, text, and images in
applications and in applets.
Drag and Drop Support (Java 2 Platform only)
Provides the ability to drag and drop between a Java application and a native
application.
The first three JFC features were implemented without any native code, relying only on the
API defined in JDK 1.1. As a result, they could and did become available as an extension to
JDK 1.1. This extension was released as JFC 1.1, which is sometimes called "the Swing
http://web2.java.sun.com/docs/books/tutorial/uiswing/start/swingIntro.html (1 of 6) [16/06/2000 3:21:13 PM]
l
37926101.011.png 37926101.012.png 37926101.001.png
About the JFC and Swing
release." The API in JFC 1.1 is often called "the Swing API."
Note: "Swing" was the codename of the project that developed the new
components. Although it's an unofficial name, it's frequently used to refer to the
new components and related API. It's immortalized in the package names for
the Swing API, which begin with javax.swing .
This trail concentrates on the Swing components. We help you choose the appropriate ones
for your GUI, tell you how to use them, and give you the background information you need
to use them effectively. We discuss the Pluggable look and feel and Accessibility support
when they affect how you write programs that use Swing components. This trail does not
cover the JFC features that appear only in the Java 2 Platform. For information about those,
refer to 2D Graphic s and to the JFC Home Page .
The following snapshots show three views of a GUI that uses Swing components. Each
picture shows the same program, but with a different look and feel. The program, called
Converter , is discussed in detail at the end of the next lesson, Swing Features and
Java look and feel
CDE/Motif look and feel
Windows look and feel
Which Releases Contain the Swing API?
The Swing API is available in two forms:
As a core part of the Java 2 Platform (standard edition of either v 1.2 or v 1.3)
l
JFC 1.1 (for use with JDK 1.1)
Which release you use depends on whether you need to use JDK 1.1 or the Java 2 Platform,
and on whether you're willing to be a beta tester for SDK v 1.3. It's a bit simpler to use the
Java 2 Platform because the JFC is built into the Java 2 Platform and you don't need to add
libraries to be able to use the Swing API. However, if you need to use JDK 1.1, then adding
http://web2.java.sun.com/docs/books/tutorial/uiswing/start/swingIntro.html (2 of 6) [16/06/2000 3:21:13 PM]
l
37926101.002.png 37926101.003.png 37926101.004.png 37926101.005.png
About the JFC and Swing
the Swing API (using JFC 1.1) isn't difficult. Instructions for doing both are in Compiling
This trail describes the Swing 1.1 API, which is the version present in the Java 2 Platform
v 1.2 and in the release called "JFC 1.1 (with Swing 1.1)." Except where noted, the code in
this trail works unchanged with either release and subsequent compatible releases, such as
SDK v 1.3 and JFC 1.1 (with Swing 1.1.1).
Sun has released many versions of JFC 1.1, which are identified by the version of Swing
API they contain. One previous version, for example, was called "JFC 1.1 (with
Swing 1.0.3)." The last JFC 1.1 release was Swing version 1.1.1. It had the same API as
Swing 1.1, but added many bug fixes, some performance improvements, and a few new
capabilities such as HTML text in labels that required no API changes.
The following table shows some of the important releases containing Swing API. Bold font
indicates the releases typically used in shipping products.
Swing API
Version
Corresponding
JFC 1.1 Release
Corresponding
Java 2 Platform
Version (Standard
Edition)
Comments
Swing 1.0.3
JFC 1.1
(with Swing 1.0.3)
none
The release of JFC 1.1
included in Java
Plug-in TM 1.1.1 .
Swing 1.1
The first releases
containing the final
Swing 1.1 API
supported for use in
shipping products. Java
Plug-in 1.1.2 and Java
Plug-in 1.2 provide
applet support for
JDK 1.1 + Swing 1.1
and Java 2 Platform
v 1.2, respectively.
Note: This is the
API this trail
covers.
JFC 1.1
(with Swing 1.1)
v 1.2 , v 1.2.1
http://web2.java.sun.com/docs/books/tutorial/uiswing/start/swingIntro.html (3 of 6) [16/06/2000 3:21:13 PM]
37926101.006.png 37926101.007.png
About the JFC and Swing
Swing 1.1.1
JFC 1.1
(with Swing 1.1.1)
Adds performance
enhancements, many
bug fixes, and selected
new functionality
(requiring no API
changes) to Swing 1.1.
Java Plug-in 1.1.3 and
Java Plug-in 1.2.2
provide applet support
for JDK 1.1 +
Swing 1.1.1 and Java 2
Platform v 1.2.2,
respectively.
Note: This trail
includes notes
about this API.
Note: This is the last
release that supports
JDK 1.1.
v 1.2.2
no separate
"Swing" version
number
none
v 1.3 Beta
Adds significant
performance
enhancements and bug
fixes, along with some
new features and API
additions. For more
information, see the
, especially Swing
Features . Java Plug-in
1.3 Beta provides
applet support for this
release.
What Swing Packages Should I Use?
The Swing API is powerful, flexible -- and immense. For example, the 1.1 version of the
API has 15 public packages: javax.accessibility , javax.swing ,
javax.swing.border , javax.swing.colorchooser , javax.swing.event ,
javax.swing.filechooser , javax.swing.plaf ,
javax.swing.plaf.basic , javax.swing.plaf.metal ,
javax.swing.plaf.multi , javax.swing.table , javax.swing.text ,
javax.swing.text.html , javax.swing.tree , and javax.swing.undo .
Fortunately, most programs use only a small subset of the API. This trail sorts out the API
for you, giving you examples of common code and pointing you to methods and classes
you're likely to need. Most of the code in this trail uses only one or two Swing packages:
javax.swing
l
javax.swing.event (not always required)
http://web2.java.sun.com/docs/books/tutorial/uiswing/start/swingIntro.html (4 of 6) [16/06/2000 3:21:13 PM]
l
37926101.008.png
Zgłoś jeśli naruszono regulamin