Java Reference
In-Depth Information
The details of how you modify the look-and-feel of a component is beyond the scope of this topic. It is,
however, as well to be aware of the MVC architecture on which the Swing components are based since
it appears quite often in the literature around Java, and should you want to change the look-and-feel of a
component at some time.
Creating a Window
A basic window in Java is represented by an object of the class
Window
in the package
java.awt
.
Objects of the class
Window
are hardly ever used directly since borders and a title bar are fairly basic
prerequisites for a typical application window, and this class provides neither. The library class
JFrame
, defined in
javax.swing
, is a much more useful class for creating a window, since as well as
a title bar and a border, it provides a wealth of other facilities. Its superclasses are shown below.
java.lang
Object
Defines a component which is an
object that can be displayed such
as a button or a scrollbar
Component
Defines a component that can
contain other components
Container
java.awt
Defines a basic window with no
title bar or border
Window
Defines a window with a title
bar or border
Frame
javax.swing
JFrame
A Frame with extended capabilities
The
Component
class is the grandmother of all component classes - it defines the basic properties and
methods shared by all components. We will see later that all the Swing components have the
Component
class as a base. The
Container
class adds the capability for a
Component
object to
contain other components, which is a frequent requirement. Since
JFrame
has
Container
as a
superclass, a
JFrame
object can contain other components. Beyond the obvious need for a window to
be able to contain the components that represent the GUI, a menu bar should contain menus, for
instance, which in turn will contain menu items: a toolbar will obviously contain toolbar buttons, and
there are many other examples. For this reason the
Container
class is also a base for all the classes
that define Swing components.
The
Window
class adds methods to the
Container
class that are specific to a window, such as the
ability to handle events arising from user interaction with the window. The
Frame
class is the original
class in
java.awt
that provided a proper window, with a title bar and a border, with which everyone is
familiar. The
JFrame
class adds functionality to the
Frame
class to support much more sophisticated
facilities for drawing and displaying other components. You can deduce from the hierarchy in the
diagram how a
JFrame
object can easily end up with its 200+ methods as it has five superclasses from
which it inherits members. We aren't going to trawl through all these classes and methods. We'll just
look into the ones we need in context as we go along, and then see how they are applied for real. This
will hopefully teach you the most important methods in this class.