Java Reference
In-Depth Information
EXAMPLE:
(continued)
As we noted in Chapter 7, this ensures that any initialization that is normally done for
all objects of type
JFrame
will in fact be done. If the base class constructor you call has
no arguments, then it will be called automatically, so we could have omitted the invo-
cation of
super()
in Display 17.4. However, if the base class constructor needs an
argument, as it may in some other situations, then you must include a call to the base
class constructor,
super
.
Note that almost all the initializing for the window
FirstWindow
in Display 17.4 is
placed in the constructor for the class. That is as it should be. The initialization, such
as setting the initial window size, should be part of the class definition and not actions
performed by objects of the class (as they were in Display 17.2). All the initializing
methods, such as
setSize
and
setDefaultCloseOperation
, are inherited from the
class
JFrame
. Because they are invoked in the constructor for the window, the window
itself is the calling object. In other words, a method invocation such as
setSize(WIDTH, HEIGHT);
is equivalent to
this
.setSize(WIDTH, HEIGHT);
Similarly, the method invocations
setDefaultCloseOperation(
JFrame.DO_NOTHING_ON_CLOSE);
and
add(endButton);
are equivalent to
this
.setDefaultCloseOperation(
JFrame.DO_NOTHING_ON_CLOSE);
and
this
.add(endButton);
In the class
FirstWindow
(Display 17.4) we added the title
"First
Window
Class"
to
the window as follows:
setTitle("First Window Class");
You can see where the title is displayed in a
JFrame
by looking at the picture of the
GUI given in Display 17.4.