The Boundary layout manager divides the layout of a container into five locations: CENTER, EAST, WEST, North, south. This corresponds to the following: North, Lower South, Levocetirizine (WEST), right East (east), Middle (CENTER), as shown in.
Characteristics:
L can place the component in any of these five positions, if no position is specified, the default position is center.
L South and North position controls occupy one row, and the width of the control is automatically filled with the entire row. The east, west, and middle positions occupy one row; if there is no control in the eastern, Western, south, and North locations, the intermediate controls will automatically fill the entire screen. In Jaudon, West, south, and North locations where there is no control, the middle position control automatically occupies the position without the control.
It is the default layout for Windows, the Contents pane of the Frame, and dialog boxes.
Common building functions and methods
| Construction Method Summary |
| BorderLayout () |
Constructs a new border layout with no spacing between components (the default spacing is 0 pixels). |
| BorderLayout (int hgap, int vgap) |
Constructs a border layout with the specified component (Hgap is horizontal, Vgap is vertical). |
| Method Summary |
| int Gethgap () |
Returns the horizontal spacing between components. |
| int Getvgap () |
Returns the vertical spacing between components. |
| void Removelayoutcomponent (Component comp) |
Removes the specified component from this border layout. |
| void Sethgap (int hgap) |
Sets the horizontal spacing between components. |
| void Setvgap (int vgap) |
Sets the vertical spacing between components. |
Example 1:
//Borderlayoutdemo.javaImportjavax.swing.*;Importjava.awt.*; Public classBorderlayoutdemoextendsJFrame { PublicBorderlayoutdemo () {//constructors, initializing object values//set to Border layout, 5 pixels between components in both horizontal and vertical spacessetlayout (NewBorderLayout (5, 5)); SetFont (NewFont ("Helvetica", Font.plain, 14)); Getcontentpane (). Add ("North",NewJButton ("North"));//Add a button to a windowGetcontentpane (). Add ("South",NewJButton ("South")); Getcontentpane (). Add ("East",NewJButton ("East")); Getcontentpane (). Add ("West",NewJButton ("West")); Getcontentpane (). Add ("Center",NewJButton ("Center")); } Public Static voidMain (String args[]) {Borderlayoutdemo F=NewBorderlayoutdemo (); F.settitle ("Boundary Layout"); F.pack (); F.setvisible (true); F.setdefaultcloseoperation (Jframe.exit_on_close); F.setlocationrelativeto (NULL);//to center the form on the display }}
The program execution results are as follows:
Comment out the east, west, south, north, and middle positions add the statement of the button, leaving the other statements to understand the characteristics of the border layout.
If you want more complex layouts, you can add intermediate containers in east, west, south, north, and middle, and then lay out the intermediate containers, and add the corresponding components to achieve the effect of the copy tween.
Example 2: Add 9 buttons in an intermediate position.
//Borderlayoutdemo1.javaImportjavax.swing.*;Importjava.awt.*; Public classBorderLayoutDemo1extendsJFrame {JPanel P=NewJPanel (); PublicBorderlayoutdemo () {setlayout (NewBorderLayout (5,5)); SetFont (NewFont ("Helvetica", Font.plain, 14)); Getcontentpane (). Add ("North",NewJButton ("North")); Getcontentpane (). Add ("South",NewJButton ("South")); Getcontentpane (). Add ("East",NewJButton ("East")); Getcontentpane (). Add ("West",NewJButton ("West")); //The settings panel is centered on the streaming layout, and the component has a horizontal and vertical spacing of 5 pixelsP.setlayout (NewFlowLayout (1,5,5)); //using the Loop Add button, note that each time you add the button object name is B//but each time the button is created with new, all the different button objects are represented. for(inti=1;i<10;i++){ //string.valueof (i), converts a number to a stringJButton b=NewJButton (string.valueof (i)); P.add (b); //Add a button to the panel} getcontentpane (). Add ("Center", p);//Add a panel to an intermediate position } Public Static voidMain (String args[]) {BorderLayoutDemo1 F=NewBorderLayoutDemo1 (); F.settitle ("Boundary Layout"); F.pack (); //make a form adaptive build sizef.setvisible (true); F.setdefaultcloseoperation (Jframe.exit_on_close); F.setlocationrelativeto (NULL);//to center the form on the display }}
Program execution Effect:
Java graphical interface Design--BorderLayout of layout Manager (boundary layout)