BoxLayout puts components into a row or into a column : BoxLayout « Swing JFC « Java






BoxLayout puts components into a row or into a column

 

import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class TwoButtons {
    public static void main(String[] args) {
      JFrame f = new JFrame();
      JPanel basic = new JPanel();
      basic.setLayout(new BoxLayout(basic, BoxLayout.Y_AXIS));
      f.add(basic);

      basic.add(Box.createVerticalGlue());

      JPanel bottom = new JPanel();
      bottom.setAlignmentX(1f);
      bottom.setLayout(new BoxLayout(bottom, BoxLayout.X_AXIS));

      JButton ok = new JButton("OK");
      JButton close = new JButton("Close");

      bottom.add(ok);
      bottom.add(Box.createRigidArea(new Dimension(5, 0)));
      bottom.add(close);
      bottom.add(Box.createRigidArea(new Dimension(15, 0)));

      basic.add(bottom);
      basic.add(Box.createRigidArea(new Dimension(0, 15)));

      f.setSize(300, 250);

      f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      f.setVisible(true);
    }
}

   
  








Related examples in the same category

1.Separating Components in a Row or Column
2.Glue spreads the components as far apart as possible.
3.Strut spreads the components apart by a fixed distance
4.Laying Out Components in a Row or Column
5.A vertical box container arranges the components top-to-bottom aligned in their preferred sizes.
6.BoxLayout: set a rigid area among our components.
7.Align your components in horizontal or vertical layout