0% found this document useful (0 votes)
16 views14 pages

Swing

Uploaded by

jeetanshasri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views14 pages

Swing

Uploaded by

jeetanshasri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Layout Mangers in Java

Java Swing
Java Layout Managers

• The Layout Managers are used to arrange


components in a particular manner. The Java
Layout Managers facilitates us to control the
positioning and size of the components in GUI
forms. Layout Manager is an interface that is
implemented by all the classes of layout
managers. There are the following classes that
represent the layout managers:
• java.awt.BorderLayout
• java.awt.FlowLayout
• java.awt.GridLayout
• java.awt.CardLayout
Java BorderLayout
• import java.awt.*;
• import javax.swing.*;

• public class Border
• {
• JFrame f = new Jframe()”myframe”;;
• Border()
• {

• // creating buttons
• JButton b1 = new JButton("NORTH");
• JButton b2 = new JButton("SOUTH");
• JButton b3 = new JButton("EAST");
• JButton b4 = new JButton("WEST");
• JButton b5 = new JButton("CENTER");

• f.add(b1, BorderLayout.NORTH);
• f.add(b2, BorderLayout.SOUTH);
• f.add(b3, BorderLayout.EAST);
• f.add(b4, BorderLayout.WEST);
• f.add(b5, BorderLayout.CENTER);

• f.setSize(300, 300);
• f.setVisible(true);
• }
• public static void main(String[] args) {
• Border b= new Border();
• }
• }
Java GridLayout

• The Java GridLayout class is used to arrange the components in


a rectangular grid. One component is displayed in each
rectangle.
• Constructors of GridLayout class
• GridLayout(): creates a grid layout with one column per
component in a row.
• GridLayout(int rows, int columns): creates a grid layout with
the given rows and columns but no gaps between the
components.
• GridLayout(int rows, int columns, int hgap, int vgap): creates a
grid layout with the given rows and columns along with given
horizontal and vertical gaps.
• import java.awt.*;
• import javax.swing.*;
• public class MyGridLayout{
• JFrame f;
• MyGridLayout(){
• f=new JFrame();
• JButton b1=new JButton("1");
• JButton b2=new JButton("2");
• JButton b3=new JButton("3");
• JButton b4=new JButton("4");
• JButton b5=new JButton("5");
• JButton b6=new JButton("6");
• JButton b7=new JButton("7");
• JButton b8=new JButton("8");
• JButton b9=new JButton("9");
• // adding buttons to the frame
• f.add(b1); f.add(b2); f.add(b3);
• f.add(b4); f.add(b5); f.add(b6);
• f.add(b7); f.add(b8); f.add(b9);

• // setting grid layout of 3 rows and 3 columns
• f.setLayout(new GridLayout(3,3));
• f.setSize(300,300);
• f.setVisible(true);
• }
• public static void main(String[] args) {
• new MyGridLayout();
• }
• }
Java FlowLayout

• The Java FlowLayout class is used to arrange the components in a line, one after
another (in a flow). It is the default layout of the applet or panel.
• Fields of FlowLayout class
• public static final int LEFT
• public static final int RIGHT
• public static final int CENTER
• public static final int LEADING
• public static final int TRAILING
• Constructors of FlowLayout class
• FlowLayout(): creates a flow layout with centered alignment and a default 5
unit horizontal and vertical gap.
• FlowLayout(int align): creates a flow layout with the given alignment and a
default 5 unit horizontal and vertical gap.
• FlowLayout(int align, int hgap, int vgap): creates a flow layout with the given
alignment and the given horizontal and vertical gap.
• mport java.awt.*;
• import javax.swing.*;

• public class MyFlowLayout{
• JFrame f;
• MyFlowLayout(){
• f=new JFrame();

• JButton b1=new JButton("1");
• JButton b2=new JButton("2");
• JButton b3=new JButton("3");
• JButton b4=new JButton("4");
• JButton b5=new JButton("5");


• // adding buttons to the frame
• f.add(b1); f.add(b2); f.add(b3); f.add(b4); f.add(b5);

• // setting flow layout of right alignment
• f.setLayout(new FlowLayout(FlowLayout.RIGHT));

• f.setSize(300,300);
• f.setVisible(true);
• }
• public static void main(String[] args) {
• new MyFlowLayout();
• }
• }
Java CardLayout

• The Java 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
• CardLayout(): creates a card layout with zero
horizontal and vertical gap.
• CardLayout(int hgap, int vgap): creates a card
layout with the given horizontal and vertical gap.

You might also like