How to set FlowLayout for JFrame in Java?


To set FlowLayout for a frame, use the Container. At first, set a JFrame −

JFrame frame = new JFrame();

Now, use Container and set the layout as FlowLayout−

Container container
container = frame.getContentPane();
container.setLayout(new FlowLayout());

The following is an example to set FlowLayout for JFrame −

Example

package my;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
public class SwingDemo {
   public static void main(String[] val) {
      JFrame frame = new JFrame();
      Container container;
      JButton btn;
      frame.setBounds(20, 20, 15, 15);
      container = frame.getContentPane();
      container.setLayout(new FlowLayout());
      btn = new JButton("Submit");
      JCheckBox checkBox1 = new JCheckBox("Graduate");
      JCheckBox checkBox2 = new JCheckBox("Post-Graduate");
      container.add(btn);
      container.add(checkBox1);
      container.add(checkBox2);
      frame.setVisible(true);
      frame.setSize(550, 400);
      frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
   }
}

Output

Updated on: 30-Jul-2019

486 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements