< Day Day Up > |
The FlowLayout [14] class provides a very simple layout manager that is used, by default, by JPanel s. Figure 16 shows an example.
Figure 16. A simple GUI that uses FlowLayout .
FlowLayout puts components in a row, at their preferred size . If the horizontal space in the container is too small to put all of the components in one row, FlowLayout uses multiple rows. If the container is wider than necessary for a row of components, the row is, by default, centered horizontally within it. You can specify that it stick to the left or right side instead by using a FlowLayout constructor that takes an alignment argument. You can also specify how much vertical or horizontal padding is put around the components. Note: FlowLayout never requests more height than is required for one row. For this reason, we don't recommend using FlowLayout if, for some reason, the FlowLayout 's container might start out with less horizontal space than it needs to fit all of the components in a single row. The following code from FlowLayoutDemo.java creates the FlowLayout and the components it manages : contentPane.setLayout(new FlowLayout()); contentPane.add(new JButton("Button 1")); contentPane.add(new JButton("Button 2")); contentPane.add(new JButton("Button 3")); contentPane.add(new JButton("Long-Named Button 4")); contentPane.add(new JButton("5")); The FlowLayout APITable 7 lists the three constructors of the the FlowLayout class. You can find the FlowLayout API documentation online at: http://java.sun.com/j2se/1.4.2/docs/api/java/awt/FlowLayout.html. Table 7. FlowLayout Constructors
Examples That Use FlowLayoutThe following table lists some of the examples that use FlowLayout .
|
< Day Day Up > |