Java Reference
In-Depth Information
Having all components with the same alignment is relatively easy to visualize, but what
would happen if the components had different alignments? For instance, if the small button
had x and y alignments of 0.0f, the medium button had alignments of 0.5f, and the large button
had alignments of 1.0f, where would everything appear? Well, the first thing the layout manager
calculates is the axis point. Based on the specific alignment of each button, the axis point would
be the upper-left corner of the small button, the middle of the medium button, and the bottom-
right corner of the large button. The position of the axis point within the container would then
be the average of the alignment values multiplied by the dimensions of the container. The
average of 0, 0.5, and 1 for both directions places the axis point at the center of the container.
The components are then placed and layered from this position, as Figure 10-19 shows.
Figure 10-19. Three buttons managed by an OverlayLayout with 0.0, 0.5, and 1.0 x and y
alignments
When you set up overlaid components, make sure that the optimizedDrawingEnabled
property of the container of the components is set to false . This ensures proper repainting and
event propagation.
To try out the OverlayLayout manager, use the source that follows in Listing 10-4. It
provides selectable buttons to demonstrate interactively the effect of varying the alignment
values. Initially, the program has everything centered.
Listing 10-4. OverLayout Example
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class OverlaySample {
public static final String SET_MINIMUM = "Minimum";
public static final String SET_MAXIMUM = "Maximum";
public static final String SET_CENTRAL = "Central";
public static final String SET_MIXED = "Mixed";
Search WWH ::




Custom Search