FlowLayout allows components to flow over multiple lines. - Java Swing

Java examples for Swing:FlowLayout

Description

FlowLayout allows components to flow over multiple lines.

Demo Code

import java.awt.FlowLayout;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JFrame;
import javax.swing.JButton;

class FlowLayoutFrame extends JFrame {
  private final JButton leftJButton; // button to set alignment left
  private final JButton centerJButton; // button to set alignment center
  private final JButton rightJButton; // button to set alignment right
  private final FlowLayout layout; // layout object
  private final Container container; // container to set layout

  // set up GUI and register button listeners
  public FlowLayoutFrame() {
    layout = new FlowLayout();
    container = getContentPane(); // get container to layout
    setLayout(layout);//from www  .  j a  v  a2 s.  c  o m

    // set up leftJButton and register listener
    leftJButton = new JButton("Left");
    add(leftJButton); // add Left button to frame
    leftJButton.addActionListener(e -> {
      layout.setAlignment(FlowLayout.LEFT);
      // realign attached components
        layout.layoutContainer(container);
      });

    // set up centerJButton and register listener
    centerJButton = new JButton("Center");
    add(centerJButton); // add Center button to frame
    centerJButton.addActionListener(e -> {
      layout.setAlignment(FlowLayout.CENTER);
      // realign attached components
        layout.layoutContainer(container);
      });

    // set up rightJButton and register listener
    rightJButton = new JButton("Right"); // create Right button
    add(rightJButton); // add Right button to frame
    rightJButton.addActionListener(e -> {
      layout.setAlignment(FlowLayout.RIGHT);
      // realign attached components
        layout.layoutContainer(container);
      });
  }
}

public class Main {
  public static void main(String[] args) {
    FlowLayoutFrame flowLayoutFrame = new FlowLayoutFrame();
    flowLayoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    flowLayoutFrame.setSize(300, 75);
    flowLayoutFrame.setVisible(true);
  }
}

Related Tutorials