Java Reference
In-Depth Information
you can use one of the many layout managers of the AWT and Swing APIs,
including the following:
java.awt.FlowLayout. Lays out components in a left-to-right flow, with
each component given its preferred size. A Panel has FlowLayout by
default.
java.awt.BorderLayout. Divides a container into five regions, allowing
one component to be added to each region. A Frame and the content
pane of a JFrame have BorderLayout by default.
java.awt.GridLayout. Divides a container into a grid of rows and
columns, with one component added to each region of the grid and
each component having the same size.
java.awt.GridBagLayout. Divides a container into regions similar to
GridLayout, except that components do not need to be the same size.
Components can span more than one row or column.
java.awt.CardLayout. Each component added to the container is treated
as a card, with only one card being visible at a time (similar to a deck of
cards).
javax.swing.BoxLayout. Allows components to be laid out vertically or
horizontally. BoxLayout is similar to GridBagLayout, but it is generally
easier to use.
javax.swing.SpringLayout. Lays out components with a specified
distance between the edges of each component.
javax.swing.OverlayLayout. Displays components over the top of each
other, similarly to CardLayout. This is a useful layout manager for creat-
ing tabbed panes.
Any container can use any layout manager. Notice that Frame objects and the
content pane of JFrame objects have BorderLayout by default. However, you
can assign them any layout manager you need. Similarly, Panel objects have
FlowLayout by default, but a Panel can be assigned any other layout manager.
We will not discuss all these layout managers in this topic, but I will show
you the more commonly used ones, including FlowLayout, BorderLayout,
GridLayout, and BoxLayout. After you get a feel for using these layout man-
agers, it will be easier for you to learn how to use the other layout managers.
FlowLayout Manager
The java.awt.FlowLayout class represents a layout manager that aligns com-
ponents in a left-to-right flow, such as words in a sentence. FlowLayout has the
following properties:
Search WWH ::




Custom Search