Java Reference
In-Depth Information
The flow layout manager then left aligns each row of components in the container. If you run the program
with this definition and resize the window, you should be able to make it look like Figure 17-15 .
FIGURE 17-15
Now the buttons are clearly left aligned. I have reduced the width of the window so two of the buttons
have spilled from the first row to the second because there is insufficient space across the width of the
window to accommodate them all.
Changing the Gap
The flow layout manager in the previous examples applies a default gap of 5 pixels between components
in a row, and between one row and the next. You can choose values for the horizontal and vertical gaps by
using yet another FlowLayout constructor. You can set the horizontal gap to 20 pixels and the vertical gap
to 30 pixels in the last example with the statement:
FlowLayout flow = new FlowLayout(FlowLayout.LEFT, 20, 30);
If you rerun the program with this definition of the layout manager and resize the window, you should
see a window with the buttons arranged as in Figure 17-16 .
FIGURE 17-16
You can also set the gaps between components and rows explicitly by calling the setHgap() or the
setVgap() method. To set the horizontal gap to 35 pixels, you would write:
flow.setHgap(35); // Set the horizontal gap
Don't be misled by this. You can't get differential spacing between components by setting the gap before
adding each component to a container. The last values for the gaps between components that you set for
a layout manager apply to all the components in a container. This is because the layout is recalculated dy-
namically each time the container is displayed. Of course, many different events may necessitate a container
being redisplayed while an application is running. The methods getHgap() and getVgap() return the cur-
rent setting for the horizontal or vertical gap as a value of type int .
 
 
 
 
Search WWH ::




Custom Search