Powered by Blogger.

Java Program: Using Layout Managers Example

Layout Managers:

In Java programming, the layout manager manages the layout of components in a container, which we can change by calling setLayout() method. The layout manage is responsible for deciding the layout policy and size of each of its container’s child components. The following layout managers are included with the Java programming language:

1. FlowLayout The FlowLayout is the default layout manager of Panel and Applet.
2. BorderLayout The BorderLayout is the default layout manager of Window, Dialog, and Frame.
3. Layout The GridLayout provides flexibility for placing components.
4. CardLayout The CardLayout provides functionality comparable to a primitive tabbed panel.
5. GridBagLayout.

FlowLayout manager uses line-by-line technique to place the components in a container. Each time a line is filled, a new line is started. It does not consider the size of components.

 import java.awt.*;
 import java.awt.event.*;

 public class FlowExample implements WindowListener{
   private Frame f;
   private Button b1, b2, b3;

   public FlowExample(){
     f = new Frame(”FlowLayout”);
     b1 = new Button(”Button 1”);
     b2 = new Button(”Button 2”);
     b3 = new Button(”Button 3”);
   }

   public void launchFrame(){
     f.setLayout(new FlowLayout());
     // f.setLayout(new FlowLayout(FlowLayout.LEFT));
     // f.setLayout(new FlowLayout(FlowLayout.RIGHT));
     // f.setLayout(new FlowLayout(FlowLayout.CENTER));
     // f.setLayout(new FlowLayout(FlowLayout.RIGHT,20,30));
     f.add(b1);
     f.add(b2);
     f.add(b3);
     f.addWindowListener(this);
     f.setSize(250, 150);
     f.setBackground(Color.red);
     f.setVisible(true);
   }

   public void windowClosing(WindowEvent e){
     System.exit(0);
   }

   public void windowOpened(WindowEvent e){}
   public void windowIconified (WindowEvent e){}
   public void windowDeiconified(WindowEvent e){}
   public void windowClosed(WindowEvent e){}
   public void windowActivated(WindowEvent e){}
   public void windowDeactivated(WindowEvent e){}

   public static void main(String[]args){
     FlowExamplefe = new FlowExample();
     fe.launchFrame();
   }
 }

The BorderLayout manager contains five distinct areas: NORTH, SOUTH, EAST, WEST, and CENTER, indicated by BorderLayout.NORTH, and so on:

 import java.awt.*;
 import java.awt.event.*;

 public c lass BorderExample implements WindowListener{
   private Frame f;
   private Button b1, b2, b3, b4, b5;

   public BorderExample(){
     f = new Frame(”FlowLayout”);
     b1 = new Button(”Button 1”);
     b2 = new Button(”Button 2”);
     b3 = new Button(”Button 3”);
     b4 = new Button(”Button 4”);
     b5 = new Button(”Button 5”);
   }

   public void launchFrame(){
     f.add(b1, BorderLayout.NORTH);
     f.add(b2, BorderLayout.SOUTH);
     f.add(b3, BorderLayout.WEST);
     f.add(b4, BorderLayout.EAST);
     f.add( b5,BorderLayout.CENTER);
     f.addWindowListener(this);
     f.setSize(250, 250);
     f.setBackground(Color.red);
     f.setVisible(true);
   }

   public void windowClosing(WindowEvent e){
     System.exit(0);
   }

   public void windowOpened(WindowEvent e){}
   public void windowIconified(WindowEvent e){}
   public void windowDeiconified(WindowEvent e){}
   public void windowClosed(WindowEvent e){}
   public void windowActivated(WindowEvent e){}
   public void windowDeactivated(WindowEvent e){}

   public static void main(String[]args){
     BorderExample be = new BorderExample();
     be.launchFrame();
   }
 }

The GridLayout manager placing components with a number of rows and columns. The following constructor creates a GridLayout with the specified equal size. new GridLayout(int rows, int cols);

 import java.awt.*;
 import java.awt.event.*;

 public class GridExample implements WindowListener{
   private Frame f;
   private Button b1, b2, b3, b4, b5, b6;

   public GridExample(){
     f = new Frame(”FlowLayout”);
     b1 = new Button(”Button 1”);
     b2 = new Button(”Button 2”);
     b3 = new Button(”Button 3”);
     b4 = new Button(”Button 4”);
     b5 = new Button(”Button 5”);
     b6 = new Button(”Button 6”);
   }

   public void launchFrame(){
     f.setLayout(new GridLayout(3,2));
     f.add(b1);
     f.add(b2);
     f.add(b3);
     f.add(b4);
     f.add(b5);
     f.add(b6);
     f.addWindowListener(this);
     f.setSize(300, 300);
     f.setBackground(Color.red);
     f.setVisible(true);
   }

   public void windowClosing(WindowEvent e){
     System.exit(0);
   }

   public void windowOpened(WindowEvent e){}
   public void windowIconified(WindowEvent e){}
   public void windowDeiconified (WindowEvent e){}
   public void windowClosed(WindowEvent e){}
   public void windowActivated(WindowEvent e){}
   public void windowDeactivated(WindowEvent e){}

   public static void main(String[]args){
     GridExample ge = new GridExample() ;
     ge.launchFrame();
   }
 }

1 comment:

  1. I really enjoy the blog.Much thanks again. Really Great.
    Very informative article post. Really looking forward to read more. Will read on…


    oracle online training
    sap fico online training
    dotnet online training
    qa-qtp-software-testing-training-tutorial

    ReplyDelete