Java Reference
In-Depth Information
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 . Since the Container class is a sub class 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 will be thrown.
The JComponent class is the base for all the Swing components used in a window as part of the GUI,
so, since 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 will, therefore, also inherit the methods from the
Container and Component classes. It also inherits methods from the old Applet class, which it
extends and improves upon. You should 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 only
contains the one class plus three related interfaces, but we won't need to use it directly. We will always
be using 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.
Since the JDialog class is derived directly from the Window class, you can only create a JDialog
object in an application 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. Since 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 will be located relative to its parent.
Note that while we will discuss applets based on the JApplet class in this topic, there is still a
significant role for applets based on the more restricted capabilities of the Applet class. This is because
as yet browsers do not support Java 2 applets by default. Both Netscape Navigator and Microsoft
Internet Explorer require the Java Plug-In from Sun to be installed before a Java2 applet can be
executed. You can download the Java Plug-In from the Sun Java web site at
http://java.sun.com/products .
As we 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 since they are derived ultimately from the Component class. However, things are not quite as
simple as that. You don't add the components for your application or applet GUI directly to the JFrame or
JApplet object for your program. Let's look at how it actually works in practice.
Search WWH ::




Custom Search