Java Reference
In-Depth Information
A component added to the east or west will get its preferred width, but
its height will be the height of the container minus any components in
the north or south.
■■
A component added to the center gets neither its preferred height nor
width, but instead will be the size of the remaining space not filled by
components in the other four regions.
■■
If you want a Frame or JFrame to use BorderLayout, you do not need to
invoke setLayout() because they use BorderLayout by default. If your container
does not have BorderLayout, you need to instantiate a new BorderLayout
object using one of its two constructors:
public BorderLayout(). Creates a new BorderLayout with a horizontal
and vertical gap of five units between components.
public BorderLayout(int hgap, int vgap). Creates a BorderLayout object
with the specified horizontal and vertical gap.
The following BorderLayoutDemo program demonstrates both how to add
components to a container with BorderLayout and how the five regions look.
In the program, a Button object is added to each of the five regions. The output
is shown in Figure 12.6.
import java.awt.*;
public class BorderLayoutDemo extends Frame
{
private Button north, south, east, west, center;
public BorderLayoutDemo(String title)
{
super(title);
north = new Button(“North”);
south = new Button(“South”);
east = new Button(“East”);
west = new Button(“West”);
center = new Button(“Center”);
this.add(north, BorderLayout.NORTH);
this.add(south, BorderLayout.SOUTH);
this.add(east, BorderLayout.EAST);
this.add(west, BorderLayout.WEST);
this.add(center, BorderLayout.CENTER);
}
public static void main(String [] args)
{
Frame f = new BorderLayoutDemo(“BorderLayout demo”);
f.pack();
f.setVisible(true);
}
}
Search WWH ::




Custom Search