Laying Out Components within a Container |
Here's an applet that shows a FlowLayout in action.
As the above applet shows, FlowLayout puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, FlowLayout uses multiple rows. Within each row, components are centered (the default), left-aligned, or right-aligned as specified when the FlowLayout is created.
Below is the code that creates the FlowLayout and the components it manages. (Here's the whole program. The program runs either within an applet, with the help of AppletButton, or as an application.)
The FlowLayout class has three constructors:setLayout(new FlowLayout()); setFont(new Font("Helvetica", Font.PLAIN, 14)); add(new Button("Button 1")); add(new Button("2")); add(new Button("Button 3")); add(new Button("Long-Named Button 4")); add(new Button("Button 5"));Thepublic FlowLayout() public FlowLayout(int alignment) public FlowLayout(int alignment, int horizontalGap, int verticalGap)alignment
argument must have the valueFlowLayout.LEFT
,FlowLayout.CENTER
, orFlowLayout.RIGHT
. ThehorizontalGap
andverticalGap
arguments specify the number of pixels to put between components. If you don't specify a gap value, FlowLayout acts as if you specified5
for the gap value.
Laying Out Components within a Container |