Java Reference
In-Depth Information
Example 10•9: ColumnLayout.java (continued)
*
ColumnLayout.LEFT -- left-justify the components
*
ColumnLayout.CENTER -- horizontally center the components
*
ColumnLayout.RIGHT -- right-justify the components
*
* You never call the methods of a ColumnLayout object. Just create one
* and make it the layout manager for your container by passing it to
* the addLayout() method of the Container object.
*/
public class ColumnLayout implements LayoutManager2 {
protected int margin_height;
protected int margin_width;
protected int spacing;
protected int alignment;
// Constants for the alignment argument to the constructor.
public static final int LEFT = 0;
public static final int CENTER = 1;
public static final int RIGHT = 2;
/** The constructor. See comment above for meanings of these arguments */
public ColumnLayout(int margin_height, int margin_width,
int spacing, int alignment) {
this.margin_height = margin_height;
this.margin_width = margin_width;
this.spacing = spacing;
this.alignment = alignment;
}
/**
* A default constructor that creates a ColumnLayout using 5-pixel
* margin width and height, 5-pixel spacing, and left alignment
**/
public ColumnLayout() { this(5, 5, 5, LEFT); }
/**
* The method that actually performs the layout.
* Called by the Container
**/
public void layoutContainer(Container parent) {
Insets insets = parent.getInsets();
Dimension parent_size = parent.getSize();
Component kid;
int nkids = parent.getComponentCount();
int x0 = insets.left + margin_width; // The base X position
int x;
int y = insets.top + margin_height;
// Start at the top of the column
for(int i = 0; i < nkids; i++) { // Loop through the kids
kid = parent.getComponent(i); // Get the kid
if (!kid.isVisible()) continue; // Skip hidden ones
Dimension pref = kid.getPreferredSize(); // How big is it?
switch(alignment) {
// Compute X coordinate
default:
case LEFT: x = x0; break;
case CENTER: x = (parent_size.width - pref.width)/2; break;
case RIGHT:
x = parent_size.width-insets.right-margin_width-pref.width;
break;
Search WWH ::




Custom Search