Java Reference
In-Depth Information
CLASS USE
JFrame
This is used as the basic Java application window. An object of this class has a title bar and provision for
adding a menu. You can also add other components to it. You usually subclass this class to create a win-
dow class specific to your application. You then are able to add GUI components or draw in this window
if required, as you later see.
JWindow
An object of this class type is a window with no title bar or window management icons. One typical use
for a
JWindow
object is for a subsidiary application window that is displayed on a secondary display
where two or more displays are attached to a system.
JDialog
You use this class to define a dialog window that is used for entering data into a program in various ways.
You usually code the creation of a dialog in response to some menu item or button being selected.
JApplet
This is the base class for a Java 2 applet — which is a program designed to run embedded in a web page.
All your Java 2 applets have this class as a base. You can draw in a
JApplet
and also add menus and oth-
er components.
JComponent
The subclasses of
JComponent
define a range of standard components such as menus, buttons, check-
boxes, and so on. You use these classes to create the GUI for your application or applet.
All the classes derived from
Container
can contain other objects of any of the classes derived from
Component
and are referred to generically as
containers
. Because the
Container
class is a subclass of the
Component
class, every container object is a
Component
, too, so a container can contain other containers.
The exception is the
Window
class and its subclasses, as objects of type
Window
(or of a subclass type) can't
be contained in another container. If you try to do this, an exception is thrown. The
JComponent
class is the
base for all the Swing components used in a window as part of the GUI, and because this class is derived
from
Container
, all of the Swing components are also containers.
As you can see, the
JApplet
class, which is a base class for all Swing applets, is derived from
Component
via the
Container
class. An applet does therefore inherit methods from the
Container
and
Component
classes. It also inherits methods from the old
Applet
class, which it extends. Note that the
JApplet
,
JFrame
,
and
JDialog
classes and the
JComponent
class and its subclasses are all in the package
javax.swing
. The
Applet
class is in
java.applet
, and all the others are in
java.awt
. The package
java.applet
is tiny —
it contains only the one class plus three related interfaces, but you don't need to use it directly. You always
use the
JApplet
class to define an applet, as it's significantly better than
Applet
.
Window and Frame Components
The basic difference between a
JFrame
object and a
Window
object is that a
JFrame
object represents the
main window for an application, whereas a
Window
object does not — you always need a
JFrame
object
before you can create a
Window
object.
Because the
JDialog
class is derived directly from the
Window
class, you can create a
JDialog
object in
an application only in the context of a
JFrame
object. Apart from the default constructor, the constructors
for the
JDialog
class generally require a
JFrame
object to be passed as an argument. This
JFrame
object
is referred to as the
parent
of the
JDialog
object. A
JFrame
object has a border, is resizable, and has the
ability to hold a built-in menu bar. Because a
JFrame
object is the top-level window in an application, its
size and location are defined relative to the screen. A
JDialog
object with a
JFrame
object as a parent is
located relative to its parent.
As I said, the
JApplet
,
JFrame
, and
JDialog
classes are all containers because they have
Container
as
a base class and therefore, in principle, can contain any kind of component. They are also all components
themselves because they are derived ultimately from the
Component
class. However, things are not quite as