Java Box create horizontal Strut

Introduction

A strut is an invisible component of a fixed width or a fixed height.

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

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("FlowLayout Nesting");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Box hBox = Box.createHorizontalBox();
    hBox.add(new JButton("First"));
    // Add a 100px strut to a horizontal box
    hBox.add(Box.createHorizontalStrut(100));
    hBox.add(new JButton("Last"));
    // Add JPanel to the content pane
    frame.getContentPane().add(hBox);//from w  ww  .  j  ava  2  s  .c om

    frame.pack();
    frame.setVisible(true);
  }
}



PreviousNext

Related