2. borderlayout
Borderlayout is the default layout manager for Windows, frames, and dialog. The borderlayout layout manager divides containers into five areas: north, south, east, west, and center. Only one component can be placed in each area. Shows the location and size of each region:
Example 5.5
Import java. AWT .*;
Public class buttondir {
Public static void main (string ARGs []) {
Frame F = new frame ("borderlayout ");
F. setlayout (New borderlayout ());
F. Add ("North", new button ("North "));
// The first parameter indicates adding the button to the North area of the container
F. Add ("South", new button ("South "));
// The first parameter indicates adding the button to the south area of the container
F. Add ("East", new button ("East "));
// The first parameter indicates adding the button to the East region of the container
F. Add ("West", new button ("West "));
// The first parameter indicates adding the button to the west area of the container
F. Add ("center", new button ("center "));
// The first parameter indicates adding the button to the center area of the container
F. setsize (200,200 );
F. setvisible (true );
}
}
When borderlayout is used, if the container size changes, the variation rule is: the relative position of the component remains unchanged, and the size changes. For example, if the container is higher, the North and South regions remain unchanged, and the west, center, and east regions become higher. If the container is wider, the west and east regions remain unchanged, and the north, center, and South regions become wider. Not all regions have components. If there are no components in the surrounding areas (west, east, north, and South), the components are added in the center area. However, if there are no components in the center area, leave it blank, as shown in the following figure:
Components missing in the North Region
Components are missing in the north and center regions.
3. gridlayout
Each component in the container is displayed in a grid layout, occupying the container space on average.
Example 5.6
Import java. AWT .*;
Public class buttongrid {
Public static void main (string ARGs []) {
Frame F = new frame ("gridlayout ");
F. setlayout (New gridlayout (3, 2 ));
// Containers are evenly divided into three rows, two columns, and six cells in total.
F. Add (New button ("1"); // Add it to the first row
F. Add (New button ("2"); // Add to the next grid of the first row
F. Add (New button ("3"); // Add it to the first row of the second row
F. Add (New button ("4"); // Add it to the next cell of the second row
F. Add (New button ("5"); // Add the first row to the third row
F. Add (New button ("6"); // Add it to the next grid of the third row
F. setsize (200,200 );
F. setvisible (true );
}
}