The Boundary layout manager divides the layout of a container into five locations:CENTER,EAST,WEST,North,south. In turn, the following are:North, Lower South, Levocetirizine(WEST), right East (east),CENTER, for example, as seen.
Characteristics:
L can place the component in the random one of these five positions, assuming the position is not specified, the default position is CENTER.
L South and North position controls occupy one row, and the width of the control fills itself with the entire line. East, west and middle place occupy a row ; Jaudon, West, south, and north are no controls, and the intermediate controls themselves are covered by the entire screen. In Jaudon, West, south, and North locations where there are no controls, the middle position control will itself actively occupy positions without controls.
It is the default layout for forms, the contents pane of the Frame, and dialog boxes.
1 . 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. |
Instance:
Borderlayoutdemo.java
Import javax.swing.*;
Import java.awt.*;
public class Borderlayoutdemo extends JFrame {
Public Borderlayoutdemo () {// constructor, initializing object values
// set to Border layout, 5 pixels between components in both horizontal and vertical spaces
SetLayout (New BorderLayout (5,5));
SetFont (New Font ("Helvetica", Font.plain, 14));
Getcontentpane (). Add ("North", New JButton ("North")); Adding a button to a form
Getcontentpane (). Add ("South", New JButton ("South"));
Getcontentpane (). Add ("East", New JButton ("East"));
Getcontentpane (). Add ("West", New JButton ("West"));
Getcontentpane (). Add ("center", New JButton ("center"));
}
public static void Main (String args[]) {
Borderlayoutdemo f = new Borderlayoutdemo ();
f.settitle (" Border layout ");
F.pack ();
F.setvisible (TRUE);
F.setdefaultcloseoperation (Jframe.exit_on_close);
F.setlocationrelativeto (NULL); to center the window
}
}
The results of the program run are as follows:
Gaze at the east, west, south, north, and middle positions to add a button to the statement, leaving the other statements to understand the characteristics of the border layout.
If you want a more complex layout to be able to add intermediate containers in the east, west, south, north, and middle, the intermediate containers are then laid out, and the corresponding components are added, the effect of the copy tween is reached.
Example two: Add 9 buttons in the middle position.
Borderlayoutdemo1.java
Import javax.swing.*;
Import java.awt.*;
public class BorderLayoutDemo1 extends JFrame {
JPanel p=new JPanel ();
Public Borderlayoutdemo () {
SetLayout (New BorderLayout (5,5));
SetFont (New Font ("Helvetica", Font.plain, 14));
Getcontentpane (). Add ("North", New JButton ("North"));
Getcontentpane (). Add ("South", New JButton ("South"));
Getcontentpane (). Add ("East", New JButton ("East"));
Getcontentpane (). Add ("West", New JButton ("West"));
// Setup Panel is centered on streaming layout, component horizontal, vertical spacing is 5 pixels
P.setlayout (New FlowLayout (1,5,5));
// use loop to add button, note that the button object name is b each time you join
// But the button is generated each time with new, all representing a different button object.
for (int i=1;i<10;i++) {
//string.valueof (i), converts a number to a string
JButton b=new JButton (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 void Main (String args[]) {
BorderLayoutDemo1 f = new BorderLayoutDemo1 ();
f.settitle (" Border layout ");
F.pack (); make window Adaptive build size
F.setvisible (TRUE);
F.setdefaultcloseoperation (Jframe.exit_on_close);
F.setlocationrelativeto (NULL); to center the window
}
}
Program Run effect :
30. Java Graphical interface design--BorderLayout of layout manager (Border layout)