BorderLayout the container into 5 areas of East (eastern), SOUTH (south), West (Western), North (northern), CENTER (medium), and common components can be placed in any of these 5 regions. The layout diagram for the BorderLayout layout manager is as follows.
When changing the container size using BorderLayout, the SOUTH (south), North (northern) and center (middle) regions are adjusted horizontally while the East (eastern), West (western) and center (middle) areas are adjusted vertically.
Use BorderLayout to note two points:
1. When you add a component to the BorderLayout layout manager, you need to define which zone to add to, and if not, add to the middle area by default.
2. If you add more than one component to the same zone, the component that you put in will overwrite the component you put in first.
Frame, Dialog, scrollpane default use borderlayout layout manager, BorderLayout has the following two constructors:
1. BorderLayout (): Creates BorderLayout layout manager using the default horizontal spacing, vertical spacing.
2. BorderLayout (int hgap, int vgap): Creates a BorderLayout layout manager using the specified horizontal spacing, vertical spacing.
When you add a component to a container that uses the BorderLayout layout manager, you should use several static constants in the BorderLayout class to define which zone to add to. BorderLayout has several static constants: East (Eastern), SOUTH (south), West (Western), North (northern), CENTER (middle).
/**
* Program demonstrated the use of BorderLayout * *
package codes11;
Import Java.awt.BorderLayout;
Import Java.awt.Button;
Import Java.awt.Frame;
public class Borderlayouttest {public
static void Main (string[] args) {
frame f = new Frame ("Test Window");
Set the frame container to use the BorderLayout layout manager
f.setlayout (new BorderLayout (5));
F.add (New button ("North"), Borderlayout.north);
F.add (New button ("South"), Borderlayout.south);
The default is added to the middle area
f.add (New button ("Medium"));
F.add (New button ("West"), borderlayout.west);
F.add (New button ("East"), Borderlayout.east);
F.pack ();
F.setvisible (True);
}
Run the program, the results are as follows
When using the BorderLayout layout manager, the components in each zone try to occupy the entire area, so the middle button is larger.
/**
* The following program did not add a component to the West zone, but added a panel container to the center area that contained a text box and a button.
* The program does not add components to the West zone, but there are still 5 components in the window because the center area adds a panel, and the Panel contains 2 components.
* *
package CODES11;
Import Java.awt.BorderLayout;
Import Java.awt.Button;
Import Java.awt.Frame;
Import Java.awt.Panel;
Import Java.awt.TextField;
public class BorderLayoutTest2 {public
static void Main (string[] args) {
frame f = new Frame ("Test Window");
F.setlayout (New BorderLayout (5));
F.add (New button ("South"), Borderlayout.south);
F.add (New button ("North"), Borderlayout.north);
Create a Panel object
panel p = new Panel ();
Add two components to the panel
p.add (new TextField);
P.add ("Click Me") (New button);
Add a panel container
F.add (p) to the middle area by default;
F.add (New button ("East"), Borderlayout.east);
F.pack ();
F.setvisible (True);
}
Run the program, the results are as follows: