This is the simplest type of layout. Components are added to a container from left to right.If no horizontal space is available for a component, a new row is started. Figure 1 is an
example of a frame container using FlowLayout (the components are actually buttons, but they can be any Swing component).
If the window is dynamically widened by dragging with a mouse,all the components will in due course fit on one row, resulting in Figure 2
FlowLayout is the default layout type for all JPanel objects. Note that there is a default horizontal and vertical gap of 5 pixels between the components. By default, each row of components is center justified. The FlowLayout is an AWT feature, so to abbreviate class names, programs should include the statement
import java.awt.FlowLayout;
To create a FlowLayout container with these defaults, use the following statements:
cp = this.getContentPane();
cp.setLayout(new FlowLayout());
where cp is the current applet or frame content pane. getContentPane and setLayout
are methods in both javax.swing.JApplet and javax.swing.JFrame classes.
To specify an alignment, use the constructor FlowLayout(align), where align is either FlowLayout.LEFT (left justified), FlowLayout.RIGHT (right justified) or FlowLayout.CENTER (center justified, the default). To specify an alignment and component gap sizes, use the constructor FlowLayout(align, horizgap, vertgap), where horizgap is the horizontal gap and vertgap is the vertical gap in pixels.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment