Java FlowLayout
Java FlowLayout
The FlowLayout class is the simplest of layout managers.It lays out components in a manner similar to the way words are laid out on a page, from left to right (in a flow)until there's no more room, then onto the next row.
Constructors of FlowLayout class
Type of Constructor | Constructor Description |
---|---|
FlowLayout() | It creates a flow layout with centered alignment and a default horizontal and vertical gap. |
FlowLayout(int align) | It creates a flow layout with the given alignment and a default horizontal and vertical gap. |
FlowLayout(int align, int hgap, int vgap) | It creates a flow layout with the given alignment and the given horizontal and vertical gap. |
The int align argument of the constructor having the following type:
1. FlowLayout.LEFT
2. FlowLayout.RIGHT
3. FlowLayout.CENTER
The hgap and vgap argument of the constructor give horizontal and vertical gap between the components in pixels.
Example of FlowLayout
import javax.swing.*;
import java.awt.*;
class DemoFlowLayout extends JFrame
{
JButton b1,b2,b3,b4,b5,b6,b7;
FlowLayout flow;
DemoFlowLayout()
{
b1=new JButton("Java");
b2=new JButton("C++");
b3=new JButton("Oracle");
b4=new JButton("Html");
b5=new JButton("CSS");
b6=new JButton("JavaScript");
b7=new JButton("Python");
add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);
flow=new FlowLayout(FlowLayout.LEFT);
setLayout(flow);
setTitle("FlowLayout Demo");
setSize(500,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
public class FlowLayoutEx
{
public static void main(String[] args)
{
DemoFlowLayout fl=new DemoFlowLayout();
}
}
Output
The code above will display :