Java Reference
In-Depth Information
When you create a container, its default layout manager is the FlowLayout .
You can query a container to obtain one of its components using getComponent or
getComponentAt ; the former method accepts an index into the list of components, while
the latter takes either a Point or x and y coordinates and returns the component at the
specified location. You can also test if a container contains a specific component by
invoking the container's isAncestorOf method.
Typically, you use containers to group components during the creation of your user
interface, which itself may inherit from the Panel or Window classes. The typical pattern is
to create a container's components in its constructor or in a method that its constructor
invokes, such as an initComponents method. Because creating user interfaces by hand can
be tedious, consider using something like NetBeans, which provides both a source and
design view of Container classes.
Tip In NetBeans, select New JPanel Form… or New JFrame Form when right-clicking a package in
the Projects pane, then choose the Design view for the new document. You can change the base class of
your container in the Source view to be a java.awt.Panel or java.awt.Frame , and the palette NetBeans
provides includes the AWT components just below the Swing components.
Because of the limitations that many consumer electronics devices have, the
relationship between the Window class and the underlying window may be somewhat
tenuous. This is because the location, size, and label of top-level windows are under
the control of the native window manager. The methods that let you adjust a con-
tainer's title, bounds, and position provide guidelines to the native window manager,
rather than absolutes that the manager must obey. As a result, the window manager
may ignore these requests or modify them in order to present the Window appropriately.
On some platforms, these may be asynchronous operations; interfaces such as
getLocation , getSize , and getLocationOnScreen may not reflect the actual geometry of
the Window . Moreover, the window manager may ignore the decoration, title, and resiz-
ability of a Frame .
Using AWT Components
The various subclasses of java.awt.Component are where your user interface's rubber
meets the road. As you saw in Figure 10-2, the hierarchy of components includes the
basic widgets you need to create most user interfaces, and you can always create your
own either by composing several components in a container or by drawing a compo-
nent's contents and handling events directly.
 
Search WWH ::




Custom Search