当前页面:
在线文档首页 >
JDK 5 Documentation v1.4.1, Java 2 SDK 英文文档
Lightweight Components - JDK 5 Documentation v1.4.1, Java 2 SDK 英文文档
Back to Examples of Using the JDK 1.1 AWT
Lightweight Components
Starting with the JDK 1.1 Beta 3 release,
the AWT supports lightweight components.
Lightweight components are essential
in the following situations:
- You want to create a custom component
that isn't rectangular.
- You want to create a custom component
that's partly (or entirely) transparent.
- You want to use multiple components
that can draw in each others' areas.
- Your program contains many components
and would be too inefficient
if implemented with heavyweight components.
This page tells you
how to create lightweight components,
and then gives you links to
examples --
pages with applets
that use lightweight components
(source code included, of course).
For more details about what lightweight components are
and how they work, see
Java AWT: Lightweight UI
Framework.
To create a lightweight component,
you need to create a class that directly extends
one of the following classes:
-
Component
-
Container
- a class that implements a lightweight component --
for example, a non-AWT class that is a subclass of
Component
or
Container
Your class needs to provide
both the look and the feel of the component.
You'll typically need to implement
the following methods:
- One or more constructors.
If the component responds to any events,
such as mouse clicks,
the constructor(s) should invoke
the
enableEvents
method.
- The
paint
method,
so that it draws the component's representation onscreen.
- As appropriate, methods to let
the state of the component be changed programmatically --
for example,
setText
.
Besides changing the component's internal state,
each of these methods should call the
repaint
method
if the component's appearance should change.
If the component's size should change,
then call the invalidate
method
before calling repaint
.
- As appropriate, methods to let
listeners be registered and unregistered for the component.
For example, a button
should implement the
addActionListener
and removeActionListener
methods,
which should add the specified listener using the
AWTEventMulticaster add
and
remove
methods, respectively.
- The
contains
method
if the component responds to events
to only part of its entire possible drawing area.
- Any appropriate
processXxx
methods.
For example, a button would implement
processMouseEvent
to change the button's state
and to generate actionPerformed
messages as appropriate.
Lightweight components place special requirements on
their containers:
- Lightweight components sometimes flash noticeably
unless you put them in a container that performs double buffering.
See the
Gauge example
for an example of a double-buffered container.
- Lightweight components will not appear
if their container implements a
paint
method that doesn't call super.paint
.
In other words,
if you implement a container that performs some drawing --
for example, one that draws a box around its display area --
make sure the container's paint
method
invokes super.paint
!
This section links to four pages,
each with an example
that illustrates an aspect of lightweight components.
Each page includes one applet
and links to the applet's source code.
If you're using a browser that includes support for 1.1beta3,
then your browser will be able to run
the applet on each page.
Otherwise,
you'll need to use another tool,
such as the JDK Applet Viewer,
to view the applet on each page.
Note:
You must use a 1.1 browser such as
HotJava
or the
JDK Applet Viewer
to view the applets on the following pages.
If you have trouble running the applets,
go here.
Here are the examples:
- Round Button Example
- Demonstrates how easy it is to
create a lightweight component.
- Openlook Button Example
- Modifies the Round Button example to show
a more realistic looking lightweight component.
- Spinner Example
- Demonstrates how to animate a lightweight component.
- Gauge Example
- Demonstrates how to animate a lightweight component
using double buffering to eliminate flicker.
Contains sourced code for both 1.1-only
and "Swing" versions of the example.
For more information on Swing, browse the
the Swing release documentation.
Back to Examples of Using the JDK 1.1 AWT