Layout Managers are used to arrange components in a specific order. The Java Layout Managers allow us to control the positioning and size of GUI form components. Layout Manager is an interface that is implemented by all layout manager classes.
We can find these layouts in the window builder using the Eclipse IDE.
The Border Layout is used to organize the components into five distinct regions: north, south, east, west, and center. Each region (area) may only have one component. It is the standard frame or window layout. 5 types of constants in Border Layout are:
package gui1;
import java.awt.*;
import javax.swing.*;
public class Coderz
{
JFrame frame;
Coderz()
{
frame=new JFrame();
JButton box1=new JButton("NORTH-code");;
JButton box2=new JButton("SOUTH-code");;
JButton box3=new JButton("EAST-code");;
JButton box4=new JButton("WEST-code");;
JButton box5=new JButton("CENTER-code");;
frame.add(box1,BorderLayout.NORTH);
frame.add(box2,BorderLayout.SOUTH);
frame.add(box3,BorderLayout.EAST);
frame.add(box4,BorderLayout.WEST);
frame.add(box5,BorderLayout.CENTER);
frame.setSize(400,400);
frame.setVisible(true);
}
public static void main(String[] args)
{
new Coderz();
}
}
The Java FlowLayout class is used to arrange the components one after the other in a line (in a flow). It is the applet or panel’s default layout.
3 types of constructors in the Flow Layout are:
package gui1;
import java.awt.*;
import javax.swing.*;
public class Coderz{
JFrame f;
Coderz(){
f=new JFrame();
JButton b1=new JButton("C");
JButton b2=new JButton("C++");
JButton b3=new JButton("JAVA");
JButton b4=new JButton("SQL");
JButton b5=new JButton("RUBY");
// 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 Coderz();
}
}
To arrange the components in a rectangular grid, the Java GridLayout class is used. Each rectangle displays one component.3 types of constructors in Grid Layout are :
package gui1;
import java.awt.*;
import javax.swing.*;
public class Coderz{
JFrame f;
Coderz(){
f=new JFrame();
JButton b1=new JButton("A");
JButton b2=new JButton("B");
JButton b3=new JButton("C");
JButton b4=new JButton("D");
JButton b5=new JButton("E");
JButton b6=new JButton("F");
JButton b7=new JButton("G");
JButton b8=new JButton("H");
JButton b9=new JButton("I");
// 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 Coderz();
}
}
To arrange the components vertically or horizontally, the Java BoxLayout class is used. The BoxLayout class provides four constants for this purpose.
package gui1;
import java.awt.*;
import javax.swing.*;
public class Coderz extends Frame {
Button buttons[];
public Coderz () {
buttons = new Button [5];
for (int i = 0;i<5;i++) {
buttons[i] = new Button ("Button " + (i + 1));
// adding the buttons so that it can be displayed
add (buttons[i]);
}
// the buttons will be placed horizontally
setLayout (new BoxLayout (this, BoxLayout.Y_AXIS));
setSize(400,400);
setVisible(true);
}
// main method
public static void main(String args[]){
Coderz b=new Coderz();
}
}
The Java Card Layout class manages the components in such a way that just one component is visible at a time. It treats each component as a card that’s why it’s referred to as Card Layout. Constructors of CardLayout Class:
package gui1;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Coderz extends JFrame implements ActionListener{
CardLayout card;
JButton b1,b2,b3;
Container c;
Coderz(){
c=getContentPane();
card=new CardLayout(40,30);
//create CardLayout object with 40 hor space and 30 ver space
c.setLayout(card);
b1=new JButton("Java");
b2=new JButton("is Object Oriented");
b3=new JButton("Language");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);c.add("b",b2);c.add("c",b3);
}
public void actionPerformed(ActionEvent e) {
card.next(c);
}
public static void main(String[] args) {
Coderz cl=new Coderz();
cl.setSize(400,400);
cl.setVisible(true);
cl.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
When the button named ‘java’ is clicked, we get
When the button named ‘is object oriented’is clicked, we get
Note: also read about the Queue Interface in Java
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…