flowlayout in java

www.igif‮it‬dea.com

FlowLayout is a layout manager in Java that arranges the components in a container from left to right, top to bottom. It is the default layout manager for JPanel and can also be used for JFrame.

The following example shows how to use FlowLayout:

import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class FlowLayoutExample {
    public static void main(String[] args) {
        // Create a JFrame with a FlowLayout manager
        JFrame frame = new JFrame("FlowLayout Example");
        frame.setLayout(new FlowLayout());

        // Create a JPanel with a FlowLayout manager
        JPanel panel = new JPanel(new FlowLayout());

        // Create and add some components to the panel
        panel.add(new JButton("Button 1"));
        panel.add(new JButton("Button 2"));
        panel.add(new JButton("Button 3"));

        // Add the panel to the frame
        frame.add(panel);

        // Set the size, location and visibility of the frame
        frame.setSize(300, 200);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

In the above example, JFrame and JPanel are used to create a container and a sub-container. FlowLayout is used as the layout manager for the JPanel. JButton is added to the JPanel using the add() method, which will be arranged from left to right, top to bottom in the JPanel. Finally, the JPanel is added to the JFrame using the add() method.