Component Layout
You may wonder what purpose having to add components to a JPanel serves.
JPanels provide the means to layout your components. You choose the layout manager in the
constructor for the JPanel. The most common Layouts are:
-
FlowLayout
-
BorderLayout
-
BoxLayout
-
GridLayout
-
GridBagLayout
The following does not describe the full use of all the layout managers
and should not be considered the final word on layout managers. I only describe the first
two which are the easiest to use.
The code needed to use a layout manager is like so:
JPanel pane = new JPanel( new BorderLayout() );
and then to add a control to the JPanel:
pane.add(button1, BorderLayout.CENTER); // possibilities are: NORTH, EAST, WEST, CENTER, SOUTH
And that is how to use BorderLayout. To use FlowLayout is easy as it is the default and you
have been using it without even realizing it when we were learning to add components.
To learn how to use the other layout managers go get the great book:
Java Foundation Classes In A Nutshell.