Layouts In Java

Introduction

 
The layout is used to enhance the look and feel of the application. To arrange the components in a container, the various layout classes can be used such as Flow layout and Border Layout. These layouts use relative positioning to place the components on the container, which means the components automatically adjust their position according to the frame size. For example, Setlayout is a method used to set the layout of the containers. Java provides the various layouts.
 
Some important layouts:
  • Flow Layout
  • Border Layout
  • Grid Layout
  • Grid Bag Layout
  • Box Layout
  • Group Layout

Flow Layout

 
It places components in a sequence one after the other component on a row. By default, the components are aligned in the center and are placed from left to right and from top to bottom. The orientation of the component can be changed by the setComponentOrientation() method. The Flow layout class provides the various constructors that can be used to create an instance.
 

Border Layout

 
Border Layout divides the container into five regions: North, south, East, West, Center. Each region can contain only a single component. Border Layout arranges and resizes the component to place them in the five regions. It is the default layout of the frame container. By default, the components are added to the center region.
It can be specified by the Border Layout.NORTH.
 
The border layout class provides the various constructors that can be used to create an instance.
 
Program
  1. import java.awt.BorderLayout;  
  2. import java.swing.JButton;  
  3. import javax.swing.JFrame;  
  4. public class BorderLayoutDemo extends JFrame {  
  5.     JButton reset, add update, delete;  
  6.     public BorderLayoutDemo() {  
  7.         reset = new JButton("Reset");  
  8.         add = new JButton("Add");  
  9.         update = new JButton("Update");  
  10.         delete = new JButton("Delete");  
  11.         setVisible(true);  
  12.         setSize(300300);  
  13.         setTitle("BorderLayout Demo");  
  14.         setLayout(new BorderLayout());  
  15.         add(reset, BorderLayout.EAST);  
  16.         add(add, BorderLayout.WEST);  
  17.         add(update, BorderLayout.NORTH);  
  18.         add(delete, BorderLayout.SOUTH);  
  19.     }  
  20.     public static void main(String args[]) {  
  21.         BorderLayoutDemo obj = new BorderLayoutDemo();  
  22.     }  
  23. }  

Grid Layout

 
GridLayout divides the container into rows and columns. The intersection of the row and the column is called a cell. The cell can contain only one component. The position of the component in a grid is determined by the order in which components are added to the grid.
 
The GridLayout class provides the various constructs that can be used to create an instance of the Grid layout.
 
Program
  1. import javax.awt.GridLayout;  
  2. import javax.swing.JButton;  
  3. import javax.swing.Jframe;  
  4. public class GridLayoutDemo extends JFrame {  
  5.     JButton red, Blue, green, Black, white, cyan;  
  6.     public GridLayoutDemo() {  
  7.         red = new JButton("Red");  
  8.         blue = new JButton("Blue");  
  9.         green = new JButton("Green");  
  10.         white = new JButton("Black");  
  11.         white = new JButton("White");  
  12.         cyan = new JButton(cyan);  
  13.         setVisible(true);  
  14.         setVisible(300300);  
  15.         setTitle(GridLayout Demo);  
  16.         setLayout(new GridLayout(23));  
  17.         add(red);  
  18.         add(blue);  
  19.         add(green);  
  20.         add(black);  
  21.         add(white);  
  22.         add(cyan);  
  23.     }  
  24.     public static void main(String args[]) {  
  25.         GridLayoutDemo obj = new GridLayout();  
  26.     }  
  27. }  

GridBag Layout

 
GridBagLayout places components in a grid of rows and columns, allowing specified components to span multiple rows or columns. GridBagLayout places components within the cell of a grid and used the component's preferred sizes to determine the size of the cells.
 
The GridBagLayout class provides the constructor that can be used to create an instance of the GridBagLayout class.
 
GridBagLAyout can be used effectively by customizing the GridBagConsatrints object. You can set one or more instance variables to customize the gridBagConstraints.
 

Box Layout

 
BoxLayout arranges multiple components either vertically, such as a stack, or horizontally, such as a row. It does not affect the placing of the components when the frame is resized. BoxLayout works with the following axis parameters.
 
X_AXIS: places components horizontally from left to right.
 
Program
  1. import javax.swing.BoxLayout;  
  2. import javax.swing.JButton;  
  3. import javax.swing.JFrame;  
  4. public class BoxLayoutDemo extends JFrame {  
  5.     JButton red, blue, green, black, white, cyan;  
  6.     public BoxLayoutDemo() {  
  7.         red = new JButton("Red");  
  8.         blue = new JButton("Blue");  
  9.         green = new JButton("Green");  
  10.         Black = new JButton("Black");  
  11.         White = new JButton("White");  
  12.         cyan = new JButton("cyan");  
  13.         setLayout(new BoxLayout(this.getContentPane(), BoxLayout.Y_AXIS));  
  14.         setVisible(true);  
  15.         setSize(300300);  
  16.         setTitle("BoxLayout Demo");  
  17.         add(red);  
  18.         add(blue);  
  19.         add(green);  
  20.         add(black);  
  21.         add(white);  
  22.         add(cyan);  
  23.     }  
  24.     public static void main(String args[]) {  
  25.         BoxLayoutDemo obj = new BoxLayoutDemo();  
  26.     }  
  27. }  

GroupLayout

 
GroupLayout is used to group various UI components in order to position them in a container. Each group may contain any number of elements, where element can be a group, UI components, A gap.
 
The GroupLayout class provides the constructor that can be used to create the instance of the GroupLayout class. 


Similar Articles