Java FlowLayout Example
Chapter:
Swing
Last Updated:
27-09-2016 15:55:23 UTC
Program:
/* ............... START ............... */
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class JavaFlowLayoutExample {
public static void main(String[] args) {
JFrame frame = new JFrame("Layout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPane = frame.getContentPane();
contentPane.setLayout(new FlowLayout());
for (int i = 1; i <= 5; i++) {
contentPane.add(new JButton("Button " + i));
}
frame.pack();
frame.setVisible(true);
}
}
/* ............... END ............... */
Output
Notes:
-
The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows. If the container is wider than necessary for a row of components, the row is, by default, centered horizontally within the container.
Tags
FlowLayout, Java, Swing