Java Swing How to - Use default FlowLayout from JPanel








Question

We would like to know how to use default FlowLayout from JPanel.

Answer

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
//  w  w  w .j  a v  a2s .c o  m
public class Main 
{
    public static void main(String[] args)
    {
        JFrame f = new JFrame();
        
        JPanel centeredPanel = new JPanel();
        centeredPanel.add(new JButton("one"));
        centeredPanel.add(new JButton("two"));
        
        f.getContentPane().add(centeredPanel);
        f.pack();
        f.setVisible(true);
    }
}