0% found this document useful (0 votes)
11 views5 pages

Java Layout Managers Explained

The document describes various layout managers in Java Swing, including GridLayout, FlowLayout, BorderLayout, and BoxLayout, providing examples of how to implement each. GridLayout organizes components in a uniform grid, FlowLayout arranges components in a row based on their preferred sizes, BorderLayout divides the container into five regions, and BoxLayout arranges components either vertically or horizontally. Each layout manager is illustrated with sample code demonstrating its usage.

Uploaded by

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

Java Layout Managers Explained

The document describes various layout managers in Java Swing, including GridLayout, FlowLayout, BorderLayout, and BoxLayout, providing examples of how to implement each. GridLayout organizes components in a uniform grid, FlowLayout arranges components in a row based on their preferred sizes, BorderLayout divides the container into five regions, and BoxLayout arranges components either vertically or horizontally. Each layout manager is illustrated with sample code demonstrating its usage.

Uploaded by

extinct715
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

GRID LAYOUT

It places components in a grid of cells in a number of rows & columns with each
component taking all of the available space within itself & each cell is the same
size.

EXAMPLE:
public class Main{

public static void main(String[] args) {

JFrame frame = new JFrame();

[Link](JFrame.EXIT_ON_CLOSE);

[Link](500, 500);

[Link](new GridLayout(3,3,0,0));

[Link](new JButton("1"));

[Link](new JButton("2"));

[Link](new JButton("3"));

[Link](new JButton("4"));

[Link](new JButton("5"));

[Link](new JButton("6"));

[Link](new JButton("7"));

[Link](new JButton("8"));

[Link](new JButton("9"));

[Link](true);

FLOW LAYOUT
The flow layout it places components in a row sized at their preferred size if the
horizontal space in the container is too small then the flow layout class uses the
next available row.

EXAMPLE:
public class Main{

public static void main(String[] args) {

JFrame frame = new JFrame();

[Link](JFrame.EXIT_ON_CLOSE);

[Link](500, 500);
[Link](new FlowLayout([Link],0,0));

[Link](new JButton("1"));

[Link](new JButton("2"));

[Link](new JButton("3"));

[Link](new JButton("4"));

[Link](new JButton("5"));

[Link](new JButton("6"));

[Link](new JButton("7"));

[Link](new JButton("8"));

[Link](new JButton("9"));

[Link](true);

BORDER LAYOUT
let's discuss border layouts a border layout is used within a container to place
components in one of five major areas they are north ,south, west, east and center
so here's some code
that i've written for an example

EXAMPLE:
public class Main{

public static void main(String[] args) {

JFrame frame = new JFrame();

[Link](JFrame.EXIT_ON_CLOSE);

[Link](500, 500);

[Link](new BorderLayout(10,10));

[Link](true);

JPanel panel1 = new JPanel();

JPanel panel2 = new JPanel();


JPanel panel3 = new JPanel();

JPanel panel4 = new JPanel();

JPanel panel5 = new JPanel();

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link](new BorderLayout());

[Link](new Dimension(100,100));

[Link](new Dimension(150,100));

[Link](new Dimension(150,100));

[Link](new Dimension(100,100));

[Link](new Dimension(100,100));

JPanel panel6 = new JPanel();

JPanel panel7 = new JPanel();

JPanel panel8 = new JPanel();

JPanel panel9 = new JPanel();

JPanel panel10 = new JPanel();

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);

[Link]([Link]);
[Link](new BorderLayout());

[Link](new Dimension(50,50));

[Link](new Dimension(50,50));

[Link](new Dimension(50,50));

[Link](new Dimension(50,50));

[Link](new Dimension(50,50));

[Link](panel6,[Link]);

[Link](panel7,[Link]);

[Link](panel8,[Link]);

[Link](panel9,[Link]);

[Link](panel10,[Link]);

[Link](panel1,[Link]);

[Link](panel2,[Link]);

[Link](panel3,[Link]);

[Link](panel4,[Link]);

[Link](panel5,[Link]);

BOX LAYOUT
It is used to arrange the components either vertically or horizontally.

EXAMPLE:
public class Demo {

public static void main(String[] args)


{

[Link](true);

JFrame frame = new JFrame("BoxLayout Example X_AXIS");

JButton jbtn1, jbtn2, jbtn3, jbtn4, jbtn5;


[Link](JFrame.EXIT_ON_CLOSE);

JPanel panel = new JPanel();

BoxLayout boxlayout = new BoxLayout(panel, BoxLayout.X_AXIS);

[Link](boxlayout);

[Link](new EmptyBorder(new Insets(100, 150, 100, 150)));

jbtn1 = new JButton("Button 1");

jbtn2 = new JButton("Button 2");

jbtn3 = new JButton("Button 3");

jbtn4 = new JButton("Button 4");

jbtn5 = new JButton("Button 5");

[Link](jbtn1);

[Link](jbtn2);

[Link](jbtn3);

[Link](jbtn4);

[Link](jbtn5);

[Link](panel);

[Link]();

[Link](true);
}
}

You might also like