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 ();

}

}


Comments

Popular posts from this blog

Discuss classification or taxonomy of virtualization at different levels.

Explain cloud computing reference model .

Suppose that a data warehouse consists of the three dimensions time, doctor, and patient, and the two measures count and charge, where a charge is the fee that a doctor charges a patient for a visit. a) Draw a schema diagram for the above data warehouse using one of the schemas. [star, snowflake, fact constellation] b) Starting with the base cuboid [day, doctor, patient], what specific OLAP operations should be performed in order to list the total fee collected by each doctor in 2004? c) To obtain the same list, write an SQL query assuming the data are stored in a relational database with the schema fee (day, month, year, doctor, hospital, patient, count, charge)