ptg7068951
170 HOUR 13:Building a Simple User Interface
Swing includes everything you need to write programs that use a GUI.
With Java’s user interface classes, you can create a GUI that includes all the
following and more:
. Buttons, check boxes, labels, and other simple components
. Text fields, sliders, and other more complex components
. Pull-down menus and pop-up menus
. Windows, frames, dialog boxes, panels, and applet windows
Using Components
In Java, every part of a GUI is represented by a class in the Swing package.
There is a JButtonclass for buttons, a JWindowclass for windows, a
JTextFieldclass for text fields, and so on.
To create and display an interface, you create objects, set their variables,
and call their methods. The techniques are the same as those you used dur-
ing the previous three hours as you were introduced to object-oriented
programming (OOP).
When you are putting a GUI together, you work with two kinds of objects:
components and containers. Acomponentis an individual element in a user
interface, such as a button or slider. Acontaineris a component that you
can use to hold other components.
The first step in creating an interface is to create a container that can hold
components. In an application, this container is often a window or a frame.
Windows and Frames
Windowsand frames are containers that can be displayed in a user inter-
face and hold other components. Windowsare simple containers that do
not have a title bar or any of the other buttons normally along the top edge
of a GUI. Framesare windows that include all the common windowing fea-
tures users expect to find when they run software—such as buttons to
close, expand, and shrink the window.
You create these containers using Swing’s JWindowand JFrameclasses. To
make the Swing package of classes available in a Java program, use the fol-
lowing statement:
importjavax.swing.*;