Explain border layout / border layout manager with suitable constructors and demonstrate it by using suitable program

 BORDER LAYOUT

The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the default layout of a frame or window. The four sides are referenced to as north, south, east, and west. The middle area is called the center. North, south, east, and west are narrow fixed-width regions whereas the center is a large region.

Commonly used Constructors

BorderLayout(); // creates a border layout but with no gaps between the components.

BorderLayout(int horz_gap, int vert_gap); // creates a border layout with the horizontal and vertical gaps between the components.

Constants for Regions

BorderLayout.CENTER

BorderLayout.EAST

BorderLayout.NORTH

Border Layout SOUTH,

BorderLayout. WEST.

When adding components, we will use these constants with the following form of add method.

void add(Component comp, object_region)

Example of BorderLayout() constrcutor

import java.awt.*;

public class BorderLayout Demo extends Frame

{

Button b1,b2, b3, b4,b5;

BorderLayout Demo ()

{

setSize(500, 250);

setTitle("Border Layout Demo"); setLayout (new BorderLayout());

b1=new Button("Button1");

b2=new Button("Button2");

b3=new Button ("Button3"); b4=new Button ("Button4");

b5-new Button ("Button5");

add (b1, BorderLayout. NORTH);

add (b2, BorderLayout.SOUTH);

add (b3, BorderLayout.EAST);

add (b4, Border Layout.WEST);

add (b5, BorderLayout.CENTER);

setVisible(true);

}

public static void main(String args[])

{

Border Layout Demo frame=new Border Layout Demo();

}

}


                  OR,
Java BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south, east, west, and center. Each region (area) may contain one component only. It is the default layout of a frame or window. The BorderLayout provides five constants for each region:

public static final int NORTH
public static final int SOUTH
public static final int EAST
public static final int WEST
public static final int CENTER

Constructors of BorderLayout class:
BorderLayout(): creates a border layout but with no gaps between the components.
BorderLayout(int hgap, int vgap): creates a border layout with the given horizontal and vertical gaps between the components.

Example of BorderLayout class: Using BorderLayout() constructor
FileName: Border.java

import java.awt.*;    
import javax.swing.*;    
    
public class Border   
{    
JFrame f;    
Border()  
{    
    f = new JFrame();    
        
     // creating buttons  
    JButton b1 = new JButton("NORTH");; // the button will be labeled as NORTH   
    JButton b2 = new JButton("SOUTH");; // the button will be labeled as SOUTH  
    JButton b3 = new JButton("EAST");; // the button will be labeled as EAST  
    JButton b4 = new JButton("WEST");; // the button will be labeled as WEST  
    JButton b5 = new JButton("CENTER");; // the button will be labeled as CENTER  
        
    f.add(b1, BorderLayout.NORTH); // b1 will be placed in the North Direction    
    f.add(b2, BorderLayout.SOUTH);  // b2 will be placed in the South Direction    
    f.add(b3, BorderLayout.EAST);  // b2 will be placed in the East Direction    
    f.add(b4, BorderLayout.WEST);  // b2 will be placed in the West Direction    
    f.add(b5, BorderLayout.CENTER);  // b2 will be placed in the Center    
        
    f.setSize(300, 300);    
    f.setVisible(true);    
}    
public static void main(String[] args) {    
    new Border();    
}    
}    
Output:
Example of BorderLayout class: Using BorderLayout(int hgap, int vgap) constructor

The following example inserts horizontal and vertical gaps between buttons using the parameterized constructor BorderLayout(int hgap, int gap)

FileName: BorderLayoutExample.java

// import statement  
import java.awt.*;    
import javax.swing.*;    
public class BorderLayoutExample  
{     
JFrame jframe;    
// constructor  
BorderLayoutExample()  
{  
    // creating a Frame   
    jframe = new JFrame();    
    // create buttons  
    JButton btn1 = new JButton("NORTH");    
    JButton btn2 = new JButton("SOUTH");    
    JButton btn3 = new JButton("EAST");   
    JButton btn4 = new JButton("WEST");    
    JButton btn5 = new JButton("CENTER");  
     // creating an object of the BorderLayout class using   
     // the parameterized constructor where the horizontal gap is 20   
     // and vertical gap is 15. The gap will be evident when buttons are placed   
     // in the frame  
    jframe.setLayout(new BorderLayout(20, 15));  
    jframe.add(btn1, BorderLayout.NORTH);    
    jframe.add(btn2, BorderLayout.SOUTH);    
    jframe.add(btn3, BorderLayout.EAST);    
    jframe.add(btn4, BorderLayout.WEST);    
    jframe.add(btn5, BorderLayout.CENTER);    
    jframe.setSize(300,300);    
    jframe.setVisible(true);    
}    
// main method  
public static void main(String argvs[])   
{    
    new BorderLayoutExample();    
}    
}    


Comments

Popular posts from this blog

Suppose that a data warehouse consists of the four dimensions; date, spectator, location, and game, and the two measures, count and charge, where charge is the fee that a spectator pays when watching a game on a given date. Spectators may be students, adults, or seniors, with each category having its own charge rate. a) Draw a star schema diagram for the data b) Starting with the base cuboid [date; spectator; location; game], what specific OLAP operations should perform in order to list the total charge paid by student spectators at GM Place in 2004?

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

Explain Parallel Efficiency of MapReduce.