Java Reference
In-Depth Information
Swing enables you to create a Java program with an interface that uses the style of the
native operating system, such as Windows or Solaris, or two styles, Ocean and Metal,
that are unique to Java. Each of these styles is called a
look and feel
because it describes
both the appearance of the interface and how its components function when they are
used.
Swing components are part of the
javax.swing
package, a standard part of the Java class
library. To use a Swing class, you must make it available with an
import
statement or
use a catchall statement such as the following:
import javax.swing.*;
Two other packages used with graphical user interface programming are
java.awt
, the
Abstract Windowing Toolkit (AWT), and
java.awt.event
, event-handling classes that
handle user input.
When you use a Swing component, you work with objects of that component's class.
You create the component by calling its constructor and then calling methods of the com-
ponent as needed for proper setup.
All Swing components are subclasses of the abstract class
JComponent
, which includes
methods to set the size of a component, change the background color, define the font
used for any displayed text, and set up
ToolTips
—explanatory text that appears when you
hover your mouse over the component for a few seconds.
CAUTION
Swing classes inherit from many of the same superclasses as the
Abstract Windowing Toolkit, so it is possible to use Swing and AWT
components together in the same interface. However, in some
cases the two types of components will not be rendered correctly
in a container. To avoid these problems, it's best to always use
Swing components—there's one for every AWT component.
Before components can be displayed in a user interface, they must be added to a
con-
tainer
, a component that can hold other components. Swing containers, which often can
be placed in other containers, are subclasses of
java.awt.Container
. This class includes
methods to add and remove components from a container, arrange components using an
object called a layout manager, and set up borders around the edges of a container.