BoxLayout: set a rigid area among our components. : BoxLayout « Swing JFC « Java






BoxLayout: set a rigid area among our components.

 

import java.awt.Dimension;
import java.awt.Insets;

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

public class RigidArea {

  public static void main(String[] args) {
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setBorder(new EmptyBorder(new Insets(40, 60, 40, 60)));

    panel.add(new JButton("Button"));
    panel.add(Box.createRigidArea(new Dimension(0, 5)));
    panel.add(new JButton("Button"));
    panel.add(Box.createRigidArea(new Dimension(0, 5)));
    panel.add(new JButton("Button"));
    panel.add(Box.createRigidArea(new Dimension(0, 5)));
    panel.add(new JButton("Button"));

    JFrame f = new JFrame();
    f.add(panel);
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    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 puts components into a row or into a column
7.Align your components in horizontal or vertical layout