Java Swing Tutorial - Java BoxLayout .getLayoutAlignmentY (Container target)








Syntax

BoxLayout.getLayoutAlignmentY(Container target) has the following syntax.

public float getLayoutAlignmentY(Container target)

Example

In the following code shows how to use BoxLayout.getLayoutAlignmentY(Container target) method.

import java.awt.BorderLayout;
/* ww w . j a  va 2  s.  c  o m*/
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main {

  public static void main(String[] a) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String labels[] = { "--", "----", "--------", "------------" };

    JPanel container = new JPanel();
    BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
    System.out.println(layout.getLayoutAlignmentY(container));
    container.setLayout(layout);

    for (int i = 0; i < labels.length; i++) {
      JButton button = new JButton(labels[i]);
      container.add(button);
    }

    frame.add(container, BorderLayout.CENTER);

    frame.setSize(300, 200);
    frame.setVisible(true);
  }
}