Java examples for Swing:Layout Manager
A container uses a layout manager to compute the position and size of all its components.
The following layout managers are popular.
Every container has a default layout manager.
The default layout manager for the content pane of a JFrame is BorderLayout.
The default layout manager for a JPanel is FlowLayout.
You can change the default layout manager by using setLayout() method.
To not use any layout manager, pass null to the setLayout() method.
You can use the getLayout() of a container to get its layout manager.
import java.awt.BorderLayout; import java.awt.Container; import java.awt.FlowLayout; import java.awt.LayoutManager; import javax.swing.JFrame; import javax.swing.JPanel; public class Main { public static void main(String[] argv) throws Exception { // Set FlowLayout as the layout manager for the content pane of a JFrame JFrame frame = new JFrame("Test Frame"); Container contentPane = frame.getContentPane(); contentPane.setLayout(new FlowLayout()); // Set BorderLayout as the layout manager for a JPanel JPanel panel = new JPanel(); panel.setLayout(new BorderLayout()); // Get the layout manager for a container LayoutManager layoutManager = contentPane.getLayout(); }/*w w w . j a v a 2 s . c om*/ }