Java Reference
In-Depth Information
12.1 Introduction
Java GUI is an excellent pedagogical tool for learning object-oriented programming.
Key
Point
The design of the API for Java GUI programming is an excellent example of how the object-
oriented principle is applied. This chapter serves two purposes. First, it presents the basics of
Java GUI programming. Second, it uses GUI to demonstrate OOP. Specifically, this chapter
introduces the framework of the Java GUI API and discusses GUI components and their rela-
tionships, containers and layout managers, colors, fonts, borders, image icons, and tool tips. It
also introduces some of the most frequently used GUI components.
12.2 Swing vs. AWT
AWT GUI components are replaced by more versatile and stable Swing GUI
components.
Key
Point
We used simple GUI examples to demonstrate OOP in Section 8.6.3, Displaying GUI Com-
ponents. We used the GUI components such as JButton , JLabel , JTextField ,
JRadioButton , and JComboBox . Why do the GUI component classes have the prefix J ?
Instead of JButton , why not name it simply Button ? In fact, there is a class already named
Button in the java.awt package.
When Java was introduced, the GUI classes were bundled in a library known as the
Abstract Windows Toolkit (AWT) . AWT is fine for developing simple graphical user inter-
faces, but not for developing comprehensive GUI projects. In addition, AWT is prone to
platform-specific bugs. The AWT user-interface components were replaced by a more
robust, versatile, and flexible library known as Swing components . Swing components are
painted directly on canvases using Java code, except for components that are subclasses of
java.awt.Window or java.awt.Panel , which must be drawn using native GUI on a spe-
cific platform. Swing components depend less on the target platform and use less of the
native GUI resource. For this reason, Swing components that don't rely on native GUI are
referred to as lightweight components , and AWT components are referred to as heavyweight
components .
To distinguish new Swing component classes from their AWT counterparts, the Swing
GUI component classes are named with a prefixed J . Although AWT components are still
supported in Java, it is better to learn how to program using Swing components, because the
AWT user-interface components will eventually fade away. This topic uses Swing GUI com-
ponents exclusively.
AW T
Swing components
lightweight component
heavyweight component
why prefix J?
12.1 Why are the Swing GUI classes named with the prefix J ?
12.2 Explain the difference between AWT GUI components and Swing GUI components.
Check
Point
12.3 The Java GUI API
The GUI API contains classes that can be classified into three groups: component
classes , container classes , and helper classes .
Key
Point
The hierarchical relationships of the Java GUI API are shown in Figure 12.1. Recall that the
triangular arrow denotes the inheritance relationship, the diamond denotes the composition
relationship, and the filled diamond denotes the exclusive composition relationship. The
object composition relationship was introduced in Section 10.7.
The subclasses of Component are called component classes for creating the user interface.
The classes, such as JFrame , JPanel , and JApplet , are called container classes used to
contain other components. The classes, such as Graphics , Color , Font , FontMetrics ,
and Dimension , are called helper classes used to support GUI components.
component class
container class
helper class
 
 
 
 
 
Search WWH ::




Custom Search