Friday, 27 March 2015

Layout Managers

LayoutManagers:

The LayoutManagers are used to arrange components in a particular manner. LayoutManager is an interface that is implemented by all the classes of layout managers. There are following classes that represents the layout managers:
  1. java.awt.BorderLayout
  2. java.awt.FlowLayout
  3. java.awt.GridLayout
  4. java.awt.CardLayout
  5. java.awt.GridBagLayout
  6. javax.swing.BoxLayout
  7. javax.swing.GroupLayout
  8. javax.swing.ScrollPaneLayout
  9. javax.swing.SpringLayout etc.

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 frame or window. The BorderLayout provides five constants for each region:
  1. public static final int NORTH
  2. public static final int SOUTH
  3. public static final int EAST
  4. public static final int WEST
  5. public static final int CENTER

Constructors of BorderLayout class:

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

Example of BorderLayout class:

  1. import java.awt.*;  
  2. import javax.swing.*;  
  3.   
  4. public class Border {  
  5. JFrame f;  
  6. Border(){  
  7.     f=new JFrame();  
  8.       
  9.     JButton b1=new JButton("NORTH");;  
  10.     JButton b2=new JButton("SOUTH");;  
  11.     JButton b3=new JButton("EAST");;  
  12.     JButton b4=new JButton("WEST");;  
  13.     JButton b5=new JButton("CENTER");;  
  14.       
  15.     f.add(b1,BorderLayout.NORTH);  
  16.     f.add(b2,BorderLayout.SOUTH);  
  17.     f.add(b3,BorderLayout.EAST);  
  18.     f.add(b4,BorderLayout.WEST);  
  19.     f.add(b5,BorderLayout.CENTER);  
  20.       
  21.     f.setSize(300,300);  
  22.     f.setVisible(true);  
  23. }  
  24. public static void main(String[] args) {  
  25.     new Border();  
}  
BorderLayout class

GridLayout

The GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle.

Constructors of GridLayout class:

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

Example of GridLayout class:


  1. import java.awt.*;  
  2. import javax.swing.*;  
  3.   
  4. public class MyGridLayout{  
  5. JFrame f;  
  6. MyGridLayout(){  
  7.     f=new JFrame();  
  8.       
  9.     JButton b1=new JButton("1");  
  10.     JButton b2=new JButton("2");  
  11.     JButton b3=new JButton("3");  
  12.     JButton b4=new JButton("4");  
  13.     JButton b5=new JButton("5");  
  14.         JButton b6=new JButton("6");  
  15.         JButton b7=new JButton("7");  
  16.     JButton b8=new JButton("8");  
  17.         JButton b9=new JButton("9");  
  18.           
  19.     f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);  
  20.     f.add(b6);f.add(b7);f.add(b8);f.add(b9);  
  21.   
  22.     f.setLayout(new GridLayout(3,3));  
  23.     //setting grid layout of 3 rows and 3 columns  
  24.   
  25.     f.setSize(300,300);  
  26.     f.setVisible(true);  
  27. }  
  28. public static void main(String[] args) {  
  29.     new MyGridLayout();  
  30. }  
  31. }  


FlowLayout

The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the default layout of applet or panel.

Fields of FlowLayout class:

  1. public static final int LEFT
  2. public static final int RIGHT
  3. public static final int CENTER
  4. public static final int LEADING
  5. public static final int TRAILING

Constructors of FlowLayout class:

  1. FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap.
  2. FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.
  3. FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given alignment and the given horizontal and vertical gap.

Example of FlowLayout class:

  1. import java.awt.*;  
  2. import javax.swing.*;  
  3.   
  4. public class MyFlowLayout{  
  5. JFrame f;  
  6. MyFlowLayout(){  
  7.     f=new JFrame();  
  8.       
  9.     JButton b1=new JButton("1");  
  10.     JButton b2=new JButton("2");  
  11.     JButton b3=new JButton("3");  
  12.     JButton b4=new JButton("4");  
  13.     JButton b5=new JButton("5");  
  14.               
  15.     f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);  
  16.       
  17.     f.setLayout(new FlowLayout(FlowLayout.RIGHT));  
  18.     //setting flow layout of right alignment  
  19.   
  20.     f.setSize(300,300);  
  21.     f.setVisible(true);  
  22. }  
  23. public static void main(String[] args) {  
  24.     new MyFlowLayout();  
  25. }  
  26. }  











No comments:

Post a Comment