Page 713 - Introduction to Programming with Java: A Problem Solving Approach
P. 713

                hi
i
s
s
i
mp
p
o
or
r
t
ts
s
c
c
l
la
a
s
s
s
16.17 How GUI Classes Are Grouped Together 679
 }
// end class ColorChooser
//*************************************************************
public static void main(String[] args)
{
}
new ColorChooser();
Figure 16.14c ColorChooser program—part C
16.17 How GUI Classes Are Grouped Together
Throughout this chapter, you’ve used Java’s pre-built GUI classes from Sun’s API library. For example, you used the JFrame class for creating a window, the JButton class for creating a button, and the Color class for creating a color. In this section, we describe how Sun’s pre-built GUI classes are grouped and organized.
Subpackages
The Java API is a huge class library that adds functionality to the core Java language. To simplify things,
the classes are organized into a hierarchy of packages where each package contains a group of classes. To
avoid having too many classes in one package, packages are often split into subpackages. A subpackage Apago PDF Enhancer
is a group of classes from within a larger group of classes. For example, rather than putting all the GUI classes (and there are lots of them!) within the java.awt package, the folks at Sun split off the GUI event- handling classes and put them in their own subpackage, java.awt.event. To import all the classes in the java.awt package and the java.awt.event subpackage, do this:
import java.awt.*;
import java.awt.event.*;
Since the java.awt.event subpackage contains java.awt in its name, is it OK to omit the java.awt.event import statement and do just the following?
import java.awt.*;
No, you must import java.awt and java.awt.event separately. Think of the java.awt package and the java.awt.event subpackage as completely separate entities. The fact that they share the com- mon name “java.awt” is irrelevant as far as the compiler is concerned. The compiler treats them as separate packages. So then why the shared name? The shared name helps programmers remember that the classes in java.awt.event are conceptually related to the classes in java.awt.
We’ve been referring to java.awt.event as a “subpackage.” It’s just as common to refer to it as a “package.” We’ll use both terms since both are valid.
The AWT and Swing Libraries
In Sun’s first Java compiler, all GUI classes were bundled into one library known as the Abstract Window- ing Toolkit (AWT). The AWT’s GUI commands generate GUI components that look different on different platforms. In other words, if your program instantiates an AWT button component, the button will have a
      T
im
T
h
s
e
e
s
si
in
nt
t
h
he
ej
tp
 j
a
.
av
v
a
a.
.a
aw
w
t
p
a
ac
c
k
k
a
ag
g
e
e
o
on
nl
ly
y
.
 









   711   712   713   714   715