What are layout managers? Explain Gridbag layout with suitable example.
A layout manager
A layout manager is an object that controls the size and position of the components in the container. Every container object has a layout manager object that controls its layout.
Actually, layout managers are used to arranging the components in a specific manner. It is an interface that is implemented by all the classes of layout managers. There are some classes that represent the layout managers.
The LayoutManagers are used to arrange components in a particular manner. The Java LayoutManagers facilitate us to control the positioning and size of the components in GUI forms. LayoutManager is an interface that is implemented by all the classes of layout managers.
Some of the most commonly used layout managers of java are :
- Flow Layout
- Border Layout
- Grid Layout
- Gridbag layout
- Card Layout
- Group Layout
Gridbag layout
The Gridbag Layout is a flexible layout manager that aligns components vertically and horizontally, without requiring that the components be of the same size. It maintains a dynamic rectangular grid of cells, with each component occupying one or more cells. Each component managed by a grid bag layout is associated with an instance of Grid BagConstraints that specifies how the component is arranged within its display area.
Constructor
Grid BagLayout();//Creates a grid bag layout manager.
Example
import java.awt.*;
public class GridBagDemo extends Frame
{
Button b1,b2,b3, b4,b5;
Grid BagConstraints gbc;
GridBagDemo()
{
setSize(400,250);
setTitle("GridBag Layout Demo");
set Layout (new GridBagLayout());
gbc=new GridBagConstraints();
b1=new Button("Button 1");
gbc.ipadx = 8;
gbc.ipady = 0;
gbc.weightx=0.5;
gbc.weighty = 0.0;
gbc.fill=GridBagConstraints. HORIZONTAL;
gbc.gridwidth = 1;
gbc.insets = new Insets (5,5,5,5);
gbc.gridx = 0; gbc.gridy = 0;
add (b1, gbc);
b2=new Button("Button 2");
gbc.gridx = 1;
add (b2, gbc);
b3=new Button("Button 3");
gbc.gridx 2;
add (b3, gbc);
b4=new Button("This is Long Button 4");
gbc.ipady=40;
gbc.gridwidth=3;
gbc.gridx = 0;
gbc.gridy = 1;
add (b4, gbc);
b5=new Button ("Button 5");
gbc.gridx=0;
gbc.gridy=2;
gbc.ipady=20;
gbc.gridwidth=2;
add (b5, gbc);
setVisible(true);
}
public static void main(String args[])
{
GridBagDemo frame=new Grid BagDemo ();
}
}
Constructor
Grid BagLayout();//Creates a grid bag layout manager.
Example
import java.awt.*;
public class GridBagDemo extends Frame
{
Button b1,b2,b3, b4,b5;
Grid BagConstraints gbc;
GridBagDemo()
{
setSize(400,250);
setTitle("GridBag Layout Demo");
set Layout (new GridBagLayout());
gbc=new GridBagConstraints();
b1=new Button("Button 1");
gbc.ipadx = 8;
gbc.ipady = 0;
gbc.weightx=0.5;
gbc.weighty = 0.0;
gbc.fill=GridBagConstraints. HORIZONTAL;
gbc.gridwidth = 1;
gbc.insets = new Insets (5,5,5,5);
gbc.gridx = 0; gbc.gridy = 0;
add (b1, gbc);
b2=new Button("Button 2");
gbc.gridx = 1;
add (b2, gbc);
b3=new Button("Button 3");
gbc.gridx 2;
add (b3, gbc);
b4=new Button("This is Long Button 4");
gbc.ipady=40;
gbc.gridwidth=3;
gbc.gridx = 0;
gbc.gridy = 1;
add (b4, gbc);
b5=new Button ("Button 5");
gbc.gridx=0;
gbc.gridy=2;
gbc.ipady=20;
gbc.gridwidth=2;
add (b5, gbc);
setVisible(true);
}
public static void main(String args[])
{
GridBagDemo frame=new Grid BagDemo ();
}
}
Comments
Post a Comment