StrutSample.java Source code

Java tutorial

Introduction

Here is the source code for StrutSample.java

Source

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridLayout;

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

public class StrutSample {
    public static void main(String args[]) {
        Box horizontalBox;
        JPanel panel;
        JFrame frame = new JFrame("Horizontal Strut");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Container contentPane = frame.getContentPane();
        contentPane.setLayout(new GridLayout(0, 1));

        horizontalBox = Box.createHorizontalBox();
        horizontalBox.add(Box.createHorizontalStrut(10));
        horizontalBox.add(new JButton("Left"));
        horizontalBox.add(new JButton("Middle"));
        horizontalBox.add(new JButton("Right"));
        panel = new JPanel(new BorderLayout());
        panel.add(horizontalBox);
        panel.setBorder(BorderFactory.createTitledBorder("Beginning Strut"));
        contentPane.add(panel);

        horizontalBox = Box.createHorizontalBox();
        horizontalBox.add(new JButton("Left"));
        horizontalBox.add(Box.createHorizontalStrut(10));
        horizontalBox.add(new JButton("Middle"));
        horizontalBox.add(Box.createHorizontalStrut(25));
        horizontalBox.add(new JButton("Right"));
        panel = new JPanel(new BorderLayout());
        panel.add(horizontalBox);
        panel.setBorder(BorderFactory.createTitledBorder("2 Middle Struts"));
        contentPane.add(panel);

        horizontalBox = Box.createHorizontalBox();
        horizontalBox.add(Box.createHorizontalStrut(25));
        horizontalBox.add(new JButton("Left"));
        horizontalBox.add(new JButton("Middle"));
        horizontalBox.add(new JButton("Right"));
        horizontalBox.add(Box.createHorizontalStrut(10));
        panel = new JPanel(new BorderLayout());
        panel.add(horizontalBox);
        panel.setBorder(BorderFactory.createTitledBorder("Beginning/End Struts"));
        contentPane.add(panel);

        horizontalBox = Box.createHorizontalBox();
        horizontalBox.add(new JButton("Left"));
        horizontalBox.add(new JButton("Middle"));
        horizontalBox.add(new JButton("Right"));
        panel = new JPanel(new BorderLayout());
        horizontalBox.add(Box.createHorizontalStrut(10));
        panel.add(horizontalBox);
        panel.setBorder(BorderFactory.createTitledBorder("End Strut"));
        contentPane.add(panel);

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