Java Reference
In-Depth Information
Variable
Description
ipadx
An int value that defines the number of pixels by which the top and bottom
edges of the component are to be expanded. The default value is 0 .
ipady
An int value that defines the number of pixels by which the left and right
edges of the component are to be expanded. The default value is 0 .
If the component is still smaller than its display area in the container, you can specify where it should
be placed in relation to its display area by setting a value for the anchor instance variable of the
GridBagConstraints object. Possible values are NORTH , NORTHEAST , EAST , SOUTHEAST , SOUTH ,
SOUTHWEST , WEST , NORTHWEST , and CENTER , all of which are defined in the GridBagConstraints
class.
The last GridBagConstraints instance variables to consider are weightx and weighty which are
of type double . These determine how space in the container is distributed between components in the
horizontal and vertical directions. You should always set a value for these, otherwise the default of 0
will cause the components to be bunched together adjacent to one another in the center of the
container. The absolute values for weightx and weighty are not important. It is the relative values
that matter. If you set all the values the same (but not zero), the space for each component will be
distributed uniformly. Space is distributed in the proportions defined by the values.
For example, if three components in a row have weightx values of 1.0, 2.0, and 3.0, the first will get
1/6 of the total in the x direction, the second will get 1/3, and the third will get half. The proportion of
the available space that a component gets in the x direction is the weightx value for the component
divided by the sum of the weightx values in the row. This also applies to the weighty values for
allocating space in the y direction.
We'll start with a simple example of placing two buttons in a window, and introduce another way of
obtaining a standard border for a component.
Try It Out - Applying the GridBagConstraints Object
Make the following changes to the previous program and try out the GridBagLayout manager.
import javax.swing.*;
import java.awt.*;
import javax.swing.border.Border;
public class TryGridBagLayout {
// The window object
static JFrame aWindow = new JFrame("This is a Gridbag Layout");
public static void main(String[] args) {
Toolkit theKit = aWindow.getToolkit(); // Get the window toolkit
Dimension wndSize = theKit.getScreenSize(); // Get screen size
// Set the position to screen center & size to half screen size
aWindow.setBounds(wndSize.width/4, wndSize.height/4, // Position
wndSize.width/2, wndSize.height/2); // Size
Search WWH ::




Custom Search