The layout of components on a container is controlled by a LayoutManager
object. The following are defined in awt:
BorderLayout
divides the container into "Centre", "North", "East", "South" and "West".FlowLayout
makes components flow like text as they are added to the container.GridLayout
uses a table with equal-sized cells.GridBagLayout
is more flexible but harder to use.Swing added BoxLayout
which is easier.
Generally call pack ()
on a container after adding components.
Container c = jframe.getContentPane ();
c.setLayout (new FlowLayout ());
c.add (button1);
c.pack ();
This is a Container that can be placed on a Container, to group controls together. It often provides a simple way of laying things out by just using the default BorderLayout. Eg,
Container c = jframe.getContentPane ();
JPanel top = new JPanel ();
top.add (new JButton ("This"), BorderLayout.WEST);
top.add (new JButton ("That"), BorderLayout.CENTER);
top.add (new JButton ("Other"), BorderLayout.EAST);
c.add (top, BorderLayout.NORTH);
c.pack ();