This example shows how FlowLayout works with JFrame in Swing. Below Java program set JFrame layout as flow layout using AWT FlowLayout class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
import javax.swing.JFrame; import javax.swing.JButton; import java.awt.FlowLayout; public class FlowLayoutForJFrame { public static void main(String[] args) { //Create a JFrame with title ( Set JFrame layout manager using Flow Layout ) JFrame frame = new JFrame("Set JFrame layout manager using Flow Layout"); //Set JFrame layout to Flow Layout frame.setLayout(new FlowLayout()); //Create 6 buttons that want to add into JFrame JButton button1 = new JButton("BUTTON 1"); JButton button2 = new JButton("BUTTON 2"); JButton button3 = new JButton("BUTTON 3"); JButton button4 = new JButton("BUTTON 4"); JButton button5 = new JButton("BUTTON 5"); JButton button6 = new JButton("BUTTON 6"); //Add all button into JFrame frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); //Set default close operation for JFrame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set JFrame size with //Width = 600 pixels //Height = 400 pixels frame.setSize(600, 400); //Make JFrame visible. So we can see it. frame.setVisible(true); } } |