This applet shows how to replace the applet's default layout manager, the FlowLayout manager, with a BorderLayout manager. The BorderLayout manager is then used to place one component, a Button, above another, a TextField.
public void init() {
// Change the layout manager to the BorderLayout manager
setLayout(new BorderLayout());
// Add a Button and a TextField
Button button1 = new Button("Button #1");
add("North", button1);
TextField textfield1 = new TextField("Default Value", 10);
add("South", textfield1);
}
The layout managers supplied with the JDK are:
| FlowLayout | FlowLayout is used to arrange components left to right until no more components fit on the same line. Each line is centered. | |
| BorderLayout | BorderLayout arranges components around the border of a container using the cardinal directions "North", "South", "West", and "East". Any room left over from these regions is assigned to the "Center" region. | |
| GridLayout | Arranges components in an equally spaced grid pattern. | |
| CardLayout | A layout manager for a container that contains several "cards". Only one card is visible at a time, allowing you to flip through the cards. | |
| GridBagLayout | GridBagLayout is the most flexible layout manager. It aligns components vertically and horizontally, without requiring that the components be the same size. Each component is associated with a set of GridBagConstraints which specify the positions, spacing, and growth properties of each component. |