Example usage for javax.swing BoxLayout BoxLayout

List of usage examples for javax.swing BoxLayout BoxLayout

Introduction

In this page you can find the example usage for javax.swing BoxLayout BoxLayout.

Prototype

@ConstructorProperties({ "target", "axis" })
public BoxLayout(Container target, int axis) 

Source Link

Document

Creates a layout manager that will lay out components along the given axis.

Usage

From source file:TwoButtons.java

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);/*  w ww .  j  ava  2  s . c om*/

    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);
}

From source file:AlignY.java

public static void main(String args[]) {
    JFrame frame = new JFrame("Y Alignment");
    JPanel contentPane = (JPanel) frame.getContentPane();

    BoxLayout layout = new BoxLayout(contentPane, BoxLayout.X_AXIS);
    contentPane.setLayout(layout);// ww  w.  j av a2  s.  c o m

    JButton button = new JButton("0.0");
    button.setAlignmentY(Component.TOP_ALIGNMENT);
    contentPane.add(button);
    button = new JButton("1.0");
    button.setAlignmentY(Component.BOTTOM_ALIGNMENT);
    contentPane.add(button);
    button = new JButton("0.5");
    button.setAlignmentY(Component.CENTER_ALIGNMENT);
    contentPane.add(button);

    frame.setSize(200, 100);
    frame.show();
}

From source file:RigidArea.java

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);/*w  w w  .j a  v a2  s . co m*/
    f.pack();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame f = new JFrame("This is BOX LAYOUT");
    JPanel panel1 = new JPanel();
    panel1.setLayout(new BoxLayout(panel1, BoxLayout.Y_AXIS));
    panel1.setBackground(Color.yellow);

    JPanel panel2 = new JPanel();
    panel2.setLayout(new FlowLayout());
    panel2.setBackground(Color.ORANGE);

    for (int i = 0; i < 5; i++)
        panel1.add(new JCheckBox("CheckBox " + (i + 1)));

    f.add(panel1, BorderLayout.WEST);
    f.add(panel2, BorderLayout.CENTER);
    f.setVisible(true);//  w  w  w  . j  av  a2s.  co m
    f.setSize(300, 300);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

From source file:BoxLayoutYAXISTest.java

public static void main(String[] args) {
    JFrame f = new JFrame("Vertical BoxLayout-managed container");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = f.getContentPane();
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
    for (float align = 0.0f; align <= 1.0f; align += 0.25f) {
        JButton button = new JButton("X Alignment = " + align);
        button.setAlignmentX(align);//from w  w w.ja  va2s.c o  m
        pane.add(button);
    }
    f.setSize(400, 300);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] args) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));

    JPanel a = new JPanel();
    a.setAlignmentX(Component.CENTER_ALIGNMENT);
    a.setPreferredSize(new Dimension(100, 100));
    a.setMaximumSize(new Dimension(100, 100)); // set max = pref
    a.setBorder(BorderFactory.createTitledBorder("aa"));
    JPanel b = new JPanel();
    b.setAlignmentX(Component.CENTER_ALIGNMENT);
    b.setPreferredSize(new Dimension(50, 50));
    b.setMaximumSize(new Dimension(50, 50)); // set max = pref
    b.setBorder(BorderFactory.createTitledBorder("bb"));

    frame.getContentPane().add(a);/*from www. java2s .  c om*/
    frame.getContentPane().add(b);
    frame.pack();
    frame.setVisible(true);
}

From source file:BoxLayoutDemo.java

public static void main(String[] a) {
    JFrame frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    LayoutManager layout = new BoxLayout(panel, BoxLayout.X_AXIS);
    panel.setLayout(layout);//from  w w w.j  a v a 2 s .c om

    panel.add(new JLabel("a"));
    panel.add(new JLabel("b"));
    panel.add(new JLabel("c"));
    panel.add(new JLabel("d"));
    panel.add(new JLabel("e"));
    panel.add(new JLabel("f"));

    frame.add(panel);

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

From source file:Main.java

public static void main(String args[]) throws ParseException {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container content = f.getContentPane();
    content.setLayout(new BoxLayout(content, BoxLayout.PAGE_AXIS));

    MaskFormatter mf1 = new MaskFormatter("###-###-###");
    mf1.setPlaceholderCharacter('_');
    JFormattedTextField ftf1 = new JFormattedTextField(mf1);
    content.add(ftf1);//from  w  w w . java  2 s  . co m

    MaskFormatter mf2 = new MaskFormatter("(###) ###-####");
    JFormattedTextField ftf2 = new JFormattedTextField(mf2);
    content.add(ftf2);
    f.setSize(300, 100);
    f.setVisible(true);
}

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String labels[] = { "--", "----", "--------", "------------" };

    JPanel container = new JPanel();
    BoxLayout layout = new BoxLayout(container, BoxLayout.PAGE_AXIS);
    container.setLayout(layout);/*  w  ww.j  av  a  2 s.  c om*/

    for (int i = 0; i < labels.length; i++) {
        JButton button = new JButton(labels[i]);
        container.add(button);
    }

    frame.add(container, BorderLayout.CENTER);

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

From source file:Main.java

public static void main(String[] a) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    String labels[] = { "--", "----", "--------", "------------" };

    JPanel container = new JPanel();
    BoxLayout layout = new BoxLayout(container, BoxLayout.LINE_AXIS);
    container.setLayout(layout);/*ww w .j a va 2  s  . com*/

    for (int i = 0; i < labels.length; i++) {
        JButton button = new JButton(labels[i]);
        container.add(button);
    }

    frame.add(container, BorderLayout.CENTER);

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