June 2013
Beginner
1007 pages
33h 32m
English
FlowLayout is a simple
layout manager that tries to arrange components at their preferred sizes,
from left to right and top to bottom in the container. A FlowLayout can have a specified row
justification of LEFT, CENTER, or RIGHT and a fixed
horizontal and vertical padding. By default, a flow layout uses CENTER justification, meaning that all
components are centered within the area allotted to them. FlowLayout is the default for JPanels.
The following example adds five buttons to the content pane of a
JFrame using the default FlowLayout:
//file: Flow.javaimportjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassFlowextendsJPanel{publicFlow(){// FlowLayout is default layout manager for a JPaneladd(newJButton("One"));add(newJButton("Two"));add(newJButton("Three"));add(newJButton("Four"));add(newJButton("Five"));}publicstaticvoidmain(String[]args){JFrameframe=newJFrame("Flow");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setSize(400,75);frame.setLocation(200,200);Flowflow=newFlow();frame.setContentPane(flow);frame.setVisible(true);}}
The result is shown in Figure 19-2.

Figure 19-2. A flow layout
Try resizing the window. If it is made narrow enough, some of the buttons will spill over to a second or third row.