BoxLayoutGlueStrutCompare.java Source code

Java tutorial

Introduction

Here is the source code for BoxLayoutGlueStrutCompare.java

Source

import java.awt.GridLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

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

public class BoxLayoutGlueStrutCompare extends JFrame {

    public BoxLayoutGlueStrutCompare() {
        JPanel p = new JPanel(new GridLayout(3, 1));

        JPanel p1 = new JPanel();
        Box box1 = new Box(BoxLayout.X_AXIS);
        box1.add(Box.createHorizontalGlue());
        box1.add(new JButton("glue-strut"));
        box1.add(Box.createHorizontalStrut(15));
        box1.add(new JButton("strut-glue"));
        box1.add(Box.createHorizontalGlue());
        p1.add(box1);
        p1.setBorder(BorderFactory.createRaisedBevelBorder());

        JPanel p2 = new JPanel();
        Box box2 = new Box(BoxLayout.X_AXIS);
        box2.add(Box.createHorizontalStrut(25));
        box2.add(new JButton("strut-glue"));
        box2.add(Box.createHorizontalGlue());
        box2.add(new JButton("glue-strut"));
        box2.add(Box.createHorizontalStrut(25));
        p2.add(box2);
        p2.setBorder(BorderFactory.createRaisedBevelBorder());

        JPanel p3 = new JPanel();
        Box box3 = new Box(BoxLayout.X_AXIS);
        box3.add(Box.createHorizontalStrut(25));
        box3.add(new JButton("strut-glue"));
        box3.add(Box.createHorizontalGlue());
        box3.add(new JButton("glue-glue"));
        box3.add(Box.createHorizontalGlue());
        p3.add(box3);
        p3.setBorder(BorderFactory.createRaisedBevelBorder());

        p.add(p1);
        p.add(p2);
        p.add(p3);
        getContentPane().add(p);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        pack();
    }

    public static void main(String[] args) {
        BoxLayoutGlueStrutCompare ex1 = new BoxLayoutGlueStrutCompare();
        ex1.setVisible(true);
    }
}