Wednesday, 27 August 2014

LayoutManagers in java

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:

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();  
  26. }  
  27. }  


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:

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:

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. }  

    BoxLayout class:

    The BoxLayout is used to arrange the components either vertically or horizontally. For this purpose, BoxLayout provides four constants. They are as follows:

    Note: BoxLayout class is found in javax.swing package.

    Fields of BoxLayout class:

    1. public static final int X_AXIS
    2. public static final int Y_AXIS
    3. public static final int LINE_AXIS
    4. public static final int PAGE_AXIS

    Constructor of BoxLayout class:

    1. BoxLayout(Container c, int axis): creates a box layout that arranges the components with the given axis.

    Example of BoxLayout class with Y-AXIS:

    BoxLayout class
    1. import java.awt.*;  
    2. import javax.swing.*;  
    3.   
    4. public class BoxLayoutExample1 extends Frame {  
    5.  Button buttons[];  
    6.   
    7.  public BoxLayoutExample1 () {  
    8.    buttons = new Button [5];  
    9.     
    10.    for (int i = 0;i<5;i++) {  
    11.       buttons[i] = new Button ("Button " + (i + 1));  
    12.       add (buttons[i]);  
    13.     }  
    14.   
    15. setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));  
    16. setSize(400,400);  
    17. setVisible(true);  
    18. }  
    19.   
    20. public static void main(String args[]){  
    21. BoxLayoutExample1 b=new BoxLayoutExample1();  
    22. }  
    23. }  


    Example of BoxLayout class with X-AXIS:

    BoxLayout class example
    1. import java.awt.*;  
    2. import javax.swing.*;  
    3.   
    4. public class BoxLayoutExample2 extends Frame {  
    5.  Button buttons[];  
    6.   
    7.  public BoxLayoutExample2() {  
    8.    buttons = new Button [5];  
    9.     
    10.    for (int i = 0;i<5;i++) {  
    11.       buttons[i] = new Button ("Button " + (i + 1));  
    12.       add (buttons[i]);  
    13.     }  
    14.   
    15. setLayout (new BoxLayout(this, BoxLayout.X_AXIS));  
    16. setSize(400,400);  
    17. setVisible(true);  
    18. }  
    19.   
    20. public static void main(String args[]){  
    21. BoxLayoutExample2 b=new BoxLayoutExample2();  
    22. }  
    23. }  


    CardLayout class

    The CardLayout class manages the components in such a manner that only one component is visible at a time. It treats each component as a card that is why it is known as CardLayout.

    Constructors of CardLayout class:

    1. CardLayout(): creates a card layout with zero horizontal and vertical gap.
    2. CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and vertical gap.

    Commonly used methods of CardLayout class:

    • public void next(Container parent): is used to flip to the next card of the given container.
    • public void previous(Container parent): is used to flip to the previous card of the given container.
    • public void first(Container parent): is used to flip to the first card of the given container.
    • public void last(Container parent): is used to flip to the last card of the given container.
    • public void show(Container parent, String name): is used to flip to the specified card with the given name.

    Example of CardLayout class:

    CardLayout class
    1. import java.awt.*;  
    2. import java.awt.event.*;  
    3.   
    4. import javax.swing.*;  
    5.   
    6. public class CardLayoutExample extends JFrame implements ActionListener{  
    7. CardLayout card;  
    8. JButton b1,b2,b3;  
    9. Container c;  
    10.     CardLayoutExample(){  
    11.           
    12.         c=getContentPane();  
    13.         card=new CardLayout(40,30);  
    14. //create CardLayout object with 40 hor space and 30 ver space  
    15.         c.setLayout(card);  
    16.           
    17.         b1=new JButton("Apple");  
    18.         b2=new JButton("Boy");  
    19.         b3=new JButton("Cat");  
    20.         b1.addActionListener(this);  
    21.         b2.addActionListener(this);  
    22.         b3.addActionListener(this);  
    23.               
    24.         c.add("a",b1);c.add("b",b2);c.add("c",b3);  
    25.                           
    26.     }  
    27.     public void actionPerformed(ActionEvent e) {  
    28.     card.next(c);  
    29.     }  
    30.   
    31.     public static void main(String[] args) {  
    32.         CardLayoutExample cl=new CardLayoutExample();  
    33.         cl.setSize(400,400);  
    34.         cl.setVisible(true);  
    35.         cl.setDefaultCloseOperation(EXIT_ON_CLOSE);  
    36.     }  
    37. }  

No comments:

Post a Comment