Explain gridbag layout manager with suitable constructors .

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

}

}


             OR,
Java GridBagLayout
The Java GridBagLayout class is used to align components vertically, horizontally or along their baseline.

The components may not be of the same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells. Each component occupies one or more cells known as its display area. Each component associates an instance of GridBagConstraints. With the help of the constraints object, we arrange the component's display area on the grid. The GridBagLayout manages each component's minimum and preferred sizes in order to determine the component's size. GridBagLayout components are also arranged in the rectangular grid but can have many different sizes and can occupy multiple rows or columns.

Constructor
GridBagLayout(): The parameterless constructor is used to create a grid bag layout manager.

Example 1
FileName: GridBagLayoutExample.java
import java.awt.Button;  
import java.awt.GridBagConstraints;  
import java.awt.GridBagLayout;  
  
import javax.swing.*;  
public class GridBagLayoutExample extends JFrame{  
    public static void main(String[] args) {  
            GridBagLayoutExample a = new GridBagLayoutExample();  
        }  
        public GridBagLayoutExample() {  
    GridBagLayoutgrid = new GridBagLayout();  
            GridBagConstraints gbc = new GridBagConstraints();  
            setLayout(grid);  
            setTitle("GridBag Layout Example");  
            GridBagLayout layout = new GridBagLayout();  
    this.setLayout(layout);  
    gbc.fill = GridBagConstraints.HORIZONTAL;  
    gbc.gridx = 0;  
    gbc.gridy = 0;  
    this.add(new Button("Button One"), gbc);  
    gbc.gridx = 1;  
    gbc.gridy = 0;  
    this.add(new Button("Button two"), gbc);  
    gbc.fill = GridBagConstraints.HORIZONTAL;  
    gbc.ipady = 20;  
    gbc.gridx = 0;  
    gbc.gridy = 1;  
    this.add(new Button("Button Three"), gbc);  
    gbc.gridx = 1;  
    gbc.gridy = 1;  
    this.add(new Button("Button Four"), gbc);  
    gbc.gridx = 0;  
    gbc.gridy = 2;  
    gbc.fill = GridBagConstraints.HORIZONTAL;  
    gbc.gridwidth = 2;  
    this.add(new Button("Button Five"), gbc);  
            setSize(300, 300);  
            setPreferredSize(getSize());  
            setVisible(true);  
            setDefaultCloseOperation(EXIT_ON_CLOSE);  
      
        }  
      
}  


Comments

Popular posts from this blog

What are different steps used in JDBC? Write down a small program showing all steps.

Explain Parallel Efficiency of MapReduce.

Suppose that a data warehouse for Big-University consists of the following four dimensions: student, course, semester, and instructor, and two measures count and avg_grade. When at the lowest conceptual level (e.g., for a given student, course, semester, and instructor combination), the avg_grade measure stores the actual course grade of the student. At higher conceptual levels, avg_grade stores the average grade for the given combination. a) Draw a snowflake schema diagram for the data warehouse. b) Starting with the base cuboid [student, course, semester, instructor], what specific OLAP operations (e.g., roll-up from semester to year) should one perform in order to list the average grade of CS courses for each BigUniversity student. c) If each dimension has five levels (including all), such as “student < major < status < university < all”, how many cuboids will this cube contain (including the base and apex cuboids)?