Explain grid layout manager with suitable constructors and demonstrate it by through sample code.

 GRID LAYOUT

It arranges components in a two-dimensional grid. It simply makes a bunch of t components equal in size and displays them in the requested number of rows and columns. All are of equal size and cover the total space of the container. Components are added from the first row to la row and first column to the last column.

Commonly used Constructors

GridLayout(); // creates a grid layout with one column per component in a row.

GridLayout(int rows, int cols); // creates a grid layout with the given rows and columns but no gaps between the components. GridLayout(int rows, int cols, int horz_gap, int vert_gap); // creates a grid layout with the given rows and columns along with given horizontal and vertical gaps.

Zero value of parameter rows & cols indicates the infinite number of rows and columns respectively And parameters horz_gap & vert gap defines the horizontal and vertical gap between components We can add components to a container by using the following form of add() method. 

void add(Component c)

Example

import java.awt.*;

public class GridLayout Demo extends Frame

{

Button b1,b2, b3, b4,b5;

GridLayout Demo()

{

setSize(400, 200);

setTitle("GridLayout Demo"); setLayout (new GridLayout (0,3));

b1=new Button("Button1");

b2=new Button("This is Long Button2");

b3=new Button("Button3");

b4=new Button("Button4");

b5=new Button("Button5");

add (b1);

add (b2);

add(b3);

add (b4); add (b5);

setVisible(true);

}public static void main(String args[])

{ Grid Layout Demo frame-new Grid Layout Demo();

}

}


               OR,
Java GridLayout
The Java GridLayout class is used to arrange the components in a rectangular grid. One component is displayed in each rectangle.

Constructors of GridLayout class
GridLayout(): creates a grid layout with one column per component in a row.
GridLayout(int rows, int columns): creates a grid layout with the given rows and columns but no gaps between the components.
GridLayout(int rows, int columns, int hgap, int vgap): creates a grid layout with the given rows and columns along with given horizontal and vertical gaps.

Example of GridLayout class: Using GridLayout() Constructor
The GridLayout() constructor creates only one row. The following example shows the usage of the parameterless constructor.

FileName: GridLayoutExample.java

// import statements  
import java.awt.*;    
import javax.swing.*;    
    
public class GridLayoutExample  
{    
JFrame frameObj;    
  
// constructor  
GridLayoutExample()  
{    
frameObj = new JFrame();    
  
// creating 9 buttons  
JButton btn1 = new JButton("1");    
JButton btn2 = new JButton("2");    
JButton btn3 = new JButton("3");    
JButton btn4 = new JButton("4");    
JButton btn5 = new JButton("5");    
JButton btn6 = new JButton("6");    
JButton btn7 = new JButton("7");    
JButton btn8 = new JButton("8");    
JButton btn9 = new JButton("9");    
    
// adding buttons to the frame  
// since, we are using the parameterless constructor, therfore;   
// the number of columns is equal to the number of buttons we   
// are adding to the frame. The row count remains one.  
frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);  
frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);  
frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);    
  
// setting the grid layout using the parameterless constructor    
frameObj.setLayout(new GridLayout());    
  
  
frameObj.setSize(300, 300);    
frameObj.setVisible(true);    
}  
  
// main method  
public static void main(String argvs[])   
{    
new GridLayoutExample();    
}    
}    
Output:

Example of GridLayout class: Using GridLayout(int rows, int columns) Constructor
FileName: MyGridLayout.java

import java.awt.*;    
import javax.swing.*;    
public class MyGridLayout{    
JFrame f;    
MyGridLayout(){    
    f=new JFrame();    
    JButton b1=new JButton("1");    
    JButton b2=new JButton("2");    
    JButton b3=new JButton("3");    
    JButton b4=new JButton("4");    
    JButton b5=new JButton("5");    
    JButton b6=new JButton("6");    
    JButton b7=new JButton("7");    
    JButton b8=new JButton("8");    
    JButton b9=new JButton("9");    
     // adding buttons to the frame       
    f.add(b1); f.add(b2); f.add(b3);  
    f.add(b4); f.add(b5); f.add(b6);  
    f.add(b7); f.add(b8); f.add(b9);    
  
    // setting grid layout of 3 rows and 3 columns    
    f.setLayout(new GridLayout(3,3));    
    f.setSize(300,300);    
    f.setVisible(true);    
}    
public static void main(String[] args) {    
    new MyGridLayout();    
}    
}    
Output:


Example of GridLayout class: Using GridLayout(int rows, int columns, int hgap, int vgap) Constructor
The following example inserts horizontal and vertical gaps between buttons using the parameterized constructor GridLayout(int rows, int columns, int hgap, int vgap).

FileName: GridLayoutExample1.java

// import statements  
import java.awt.*;    
import javax.swing.*;    
    
public class GridLayoutExample1  
{    
  
JFrame frameObj;    
  
// constructor  
GridLayoutExample1()  
{    
frameObj = new JFrame();    
  
// creating 9 buttons  
JButton btn1 = new JButton("1");    
JButton btn2 = new JButton("2");    
JButton btn3 = new JButton("3");    
JButton btn4 = new JButton("4");    
JButton btn5 = new JButton("5");    
JButton btn6 = new JButton("6");    
JButton btn7 = new JButton("7");    
JButton btn8 = new JButton("8");    
JButton btn9 = new JButton("9");    
    
// adding buttons to the frame  
// since, we are using the parameterless constructor, therefore;   
// the number of columns is equal to the number of buttons we   
// are adding to the frame. The row count remains one.  
frameObj.add(btn1); frameObj.add(btn2); frameObj.add(btn3);  
frameObj.add(btn4); frameObj.add(btn5); frameObj.add(btn6);  
frameObj.add(btn7); frameObj.add(btn8); frameObj.add(btn9);    
// setting the grid layout   
// a 3 * 3 grid is created with the horizontal gap 20   
// and vertical gap 25  
frameObj.setLayout(new GridLayout(3, 3, 20, 25));    
frameObj.setSize(300, 300);    
frameObj.setVisible(true);    
}  
// main method  
public static void main(String argvs[])   
{    
new GridLayoutExample();    
}    
}    
Output:


Comments

Popular posts from this blog

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

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)?

Discuss classification or taxonomy of virtualization at different levels.