Introduction
In today's article you learn about Layouts
of AWT in Java.
Layouts In AWT
Layouts allow you to format components on the screen in a
platform-independent way. Without layouts, you would be forced to place
components at explicit locations on the screen, creating obvious problems for
programs that need to run on multiple platforms. There's no guarantee that a
TextArea or a Scrollbar or any other component will be the same size on each
platform in fact, you can bet they won't be. In an effort to make your Java
creations portable across multiple platforms, Sun created a LayoutManager
interface that defines methods to reformat the screen based on the current
layout and component sizes.
Layout managers try to give programs a consistent and reasonable
appearance, regardless of the platform, the screen size, or actions the user
might take. Every container has a LayoutManager that is responsible for
positioning the component objects within it, regardless of the platform or the
screen size. Layout managers eliminate the need to compute component placement
on your own, which would be a losing proposition since the size required for any
component depends on the platform on which it is displayed.
The standard JDK provides 5 classes that
implement the LayoutManager interface. They are
FlowLayout,
GridLayout,
BorderLayout,
CardLayout,
and
GridBagLayout.
1. FlowLayout
The FlowLayout is the default layout for the
Panel class, that includes its most famous subclass, Applet. When there are too
many components to put, they "wrap" to a new row, similar to a word processor
with word wrap enabled. When you add components to the screen, they move left to
right (centered within the applet) based upon the order added and the width of
the applet. If you resize an applet then the component's move will change based
upon the new width and height. The following shows an example of both before
and after resizing.
Here is the code of this program:
import java.awt.*;
import java.awt.event.*;
public class FLExample
{
public static void main(String[] args)
{
Frame frame= new Frame("FlowLayout
Frame");
Panel pa= new Panel();
Button ba1= new Button();
Button ba2=new Button();
Button ba3=new Button();
Button ba4=new Button();
Button ba5=new Button();
frame.add(pa);
pa.setLayout(new FlowLayout());
pa.add(new Button("India"));
pa.add(new Button("Pakistan"));
pa.add(new Button("Japan"));
pa.add(new Button("China"));
pa.add(new Button("Countries"));
frame.setSize(300,300);
frame.setVisible(true);
frame.addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
}
Output
2. GridLayout
You start at row one, column one, thne move
across the row until it's full, then continue on to the next row. The GridLayout
is widely used for arranging components in rows and columns. GridLayout can
reposition or resize objects after adding or removing components. As with
FlowLayout, the order in which you add components is relevant. However, unlike
FlowLayout, the underlying components are resized to adjust the row-column area,
if possible. Whenever the area is resized, the components within it are
resized. The following shows an example of both before and after resizing.
The same as FlowLayout, some changes exist in Line 3, Line 7 &
Line 15.
Line 3: public class GLExample
Line 7: Frame frame= new Frame("GridLayout Frame");
Line 15: pa.setLayout(new GridLayout());
Output
3. BorderLayout
When you add a component to the layout, you must
specify which area to place it in. BorderLayout is one of the more unusual
layouts provided. It is the default layout for Window, along with its children,
Frame and Dialog. BorderLayout provides 5 areas to hold components. These areas
are named after the four different borders of the screen, North, South, East,
and West, with any remaining space going into the Center area. The order in
which components are added to the screen is not important, although you can have
only one component in each area. The following shows a BorderLayout that has one
button in each area, before and after resizing.
Here is the code of this program:
The same as FlowLayout, some changes exist in Line 3, Line 7 &
Line 15-21.
Line 3-public class BLExample
Line 7- Frame frame= new Frame("BorderLayout Frame");
Line 15-21
pa.setLayout(new BorderLayout());
pa.add(new Button("India"), BorderLayout.NORTH);
pa.add(new Button("Pakistan"), BorderLayout.SOUTH);
pa.add(new Button("Japan"), BorderLayout.EAST);
pa.add(new Button("China"), BorderLayout.WEST);
pa.add(new Button("Countries"), BorderLayout.CENTER);
Output
4. CardLayout
A CardLayout usually manages several
components, displaying one of them at a time and hiding the rest. CardLayout
lets you assign names to the components it manages and lets you jump to a
component by name. With a little work, you can use the CardLayout to create
tabbed dialog boxes or property sheets, that are not currently part of AWT. The
CardLayout is a bit on the strange side. All the components are given the same
size. Usually, the CardLayout manages a group of Panels (or some other
container), and each Panel contains several components of its own. You can also
cycle through components in order.
Here is the code of this program:
The same as FlowLayout, some changes exist in
Line 3, Line 7 and Line 15:
Line 3-public class CLExample
Line 7: Frame frame= new Frame("CardLayout Frame");
Line 15: pa.setLayout(new CardLayout());
Output:
5. GridBagLayout
You provide all the details of each component
through instances of the GridBagConstraints class. GridBagLayout is the most
sophisticated and complex of the layouts provided in the development kit. With
the GridBagLayout, you can organize components in multiple rows and columns,
stretch specific rows or columns when space is available, and anchor objects in
different corners. The following shows an example of a GridBagLayout.
The same as FlowLayout, some changes exist in
Line3, Line 7 & Line 15.
Line 3-public class CBLExample
Line 7- Frame frame= new Frame("GridBagLayout Frame");
Line 15- pa.setLayout(new GridBagLayout());
Output