Java GUI Layout Manager
Appearance of components added to a panel are a function of:
- Layout Manager chosen
- Order and how add components to the panel
Kinds:
FlowLayout |
Adds components one at a time, left to right, top to down. |
GridLayout |
GridLayout(3,2) is like a table with 3 rows and 2 columns...thus
have 6 places to add components. |
BorderLayout |
N,S,E,W,CENTER positions.
add("North", new Button("Clear"));
|
CardLayout |
cards are different panels that can be added one at a time and
displayed one at a time. See your TEXT.
|
GridBagLayout |
See YOUR TEXT |
Create a Layout Manager for a Panel
public boid init() {
setLayout(new FlowLayout());
//now add your components
}
|
This will center components as added.
setLayout(new FlowLayout(FlowLayout.LEFT, 30, 10)); will align
items to the left, put 30 point gaps in horizontal direction between items
and 10 point gaps between vertical placement of items.
Exercise
|