In general, layout managers are objects used in widget toolkits that have the ability to arrange widgets by their relative positions. Java uses layout managers to arrange components in a consistent manner across all windowing platforms.
In this section, we will introduce the following Swing layout managers:
Layout Manager | Description |
---|---|
FlowLayout | Objects that arrange components in a directional flow, much like lines of text in a paragraph. |
GridLayout | Objects that divide the container into equal-sized rectangles and arrange each component into one of these cells. |
BorderLayout | Objects that arrange components to the top, bottom, left, right, and center of a container. |
To demonstrate these layout managers, we will create an Applet that allows the user to select a layout manager from a list and see the effects it has on some Swing components. In this implementation, we use a
BorderLayout
manager to place a drop-down list above a panel that contains serveral Swing components (buttons). When an item in the drop-down list is selected, the layout manager for the panel is changed, and without explicitly setting the bounds of any components, the layout manager rearranges the buttons.