Java Tutorial/Swing/BoxLayout — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 17:44, 31 мая 2010

Align your components in horizontal or vertical layout

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Main extends JFrame {
  public Main() {
    super("Demostrating BoxLayout");
    final int SIZE = 3;
    Container c = getContentPane();
    c.setLayout(new BorderLayout(30, 30));
    Box boxes[] = new Box[4];
    boxes[0] = Box.createHorizontalBox();
    boxes[1] = Box.createVerticalBox();
    boxes[2] = Box.createHorizontalBox();
    boxes[3] = Box.createVerticalBox();
    for (int i = 0; i < SIZE; i++)
      boxes[0].add(new JButton("boxes[0]: " + i));
    for (int i = 0; i < SIZE; i++) {
      boxes[1].add(Box.createVerticalStrut(25));
      boxes[1].add(new JButton("boxes[1]: " + i));
    }
    for (int i = 0; i < SIZE; i++) {
      boxes[2].add(Box.createHorizontalGlue());
      boxes[2].add(new JButton("boxes[2]: " + i));
    }
    for (int i = 0; i < SIZE; i++) {
      boxes[3].add(Box.createRigidArea(new Dimension(12, 8)));
      boxes[3].add(new JButton("boxes[3]: " + i));
    }
    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    for (int i = 0; i < SIZE; i++) {
      panel.add(Box.createGlue());
      panel.add(new JButton("panel: " + i));
    }
    c.add(boxes[0], BorderLayout.NORTH);
    c.add(boxes[1], BorderLayout.EAST);
    c.add(boxes[2], BorderLayout.SOUTH);
    c.add(boxes[3], BorderLayout.WEST);
    c.add(panel, BorderLayout.CENTER);
    setSize(350, 300);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);
    
  }
  public static void main(String args[]) {
    Main app = new Main();
  }
}





A Simple BoxLayout Test

import java.awt.Container;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
public class BoxLayoutYAXISTest {
  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);
      pane.add(button);
    }
    f.setSize(400, 300);
    f.setVisible(true);
  }
}





A vertical box container arranges the components top-to-bottom aligned in their preferred sizes.

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class Main {
  public static void main(String[] argv) throws Exception {
    JButton component1 = new JButton();
    JButton component2 = new JButton();
    // Create vertical box container
    Box box = new Box(BoxLayout.Y_AXIS);
    // create a vertical box container
    box = Box.createVerticalBox();
    // Add components
    box.add(component1);
    box.add(component2);
  }
}





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





Creating a BoxLayout

BoxLayout has a single constructor:

public BoxLayout(Container target, int axis)



public BoxLayout(Container target, int axis)





Drawing Borders Within a BoxLayout-Managed Container

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.LayoutManager;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class BoxLayoutManagerContainerTest {
  public static void main(String[] args) {
    JFrame f = new JFrame("Vertical BoxLayout-managed container");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = new BoxPanel();
    f.setContentPane(pane);
    pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
    for (float align = 0.0f; align <= 1.0f; align += 0.25f) {
      JTextField tf = new JTextField("X Alignment = " + align, 10);
      tf.setAlignmentX(align);
      pane.add(tf);
    }
    f.setSize(400, 300);
    f.setVisible(true);
  }
}
class BoxPanel extends JPanel {
  public void paintChildren(Graphics g) {
    super.paintChildren(g);
    Dimension size = getSize();
    LayoutManager manager = getLayout();
    if ((manager != null) && (manager instanceof BoxLayout)) {
      BoxLayout layout = (BoxLayout) manager;
      boolean vertical = true;
      if (vertical) {
        int axis = (int) (layout.getLayoutAlignmentX(this) * size.width);
        g.fillRect(axis - 1, 0, 3, size.height);
      } else {
        int axis = (int) (layout.getLayoutAlignmentY(this) * size.height);
        g.fillRect(0, axis - 1, size.width, 3);
      }
    }
  }
}





Glue spreads the components as far apart as possible.

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class Main {
  public static void main(String[] argv) throws Exception {
    JButton component1 = new JButton();
    JButton component2 = new JButton();
    Box box = new Box(BoxLayout.X_AXIS);
    
    box.add(component1);
    box.add(Box.createGlue());
    box.add(component2);
  }
}





Laying Out Components in a Row or Column

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class Main {
  public static void main(String[] argv) throws Exception {
    JButton component1 = new JButton();
    JButton component2 = new JButton();
    Box box = new Box(BoxLayout.X_AXIS);
    box = Box.createHorizontalBox();
    // Add components
    box.add(component1);
    box.add(component2);
  }
}





Separating Components in a Row or Column

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class Main {
  public static void main(String[] argv) throws Exception {
    JButton component1 = new JButton();
    JButton component2 = new JButton();
    Box box = new Box(BoxLayout.X_AXIS);
    
    box.add(component1);
    box.add(Box.createGlue());
    box.add(component2);
  }
}





Struts and Glue

import java.awt.BorderLayout;
import java.awt.Container;
import javax.swing.Box;
import javax.swing.ButtonGroup;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
public class TryBoxLayout {
  public static void main(String[] args) {
    JFrame aWindow = new JFrame("This is a Box Layout");
    aWindow.setBounds(30, 30, 300, 300);
    aWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    // Create left column of radio buttons
    Box left = Box.createVerticalBox();
    left.add(Box.createVerticalStrut(30));
    ButtonGroup radioGroup = new ButtonGroup();
    JRadioButton rbutton;
    radioGroup.add(rbutton = new JRadioButton("Red"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Green"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Blue"));
    left.add(rbutton);
    left.add(Box.createVerticalStrut(30));
    radioGroup.add(rbutton = new JRadioButton("Yellow"));
    left.add(rbutton);
    Box right = Box.createVerticalBox();
    right.add(new JCheckBox("A"));
    right.add(new JCheckBox("B"));
    right.add(new JCheckBox("C"));
    Box top = Box.createHorizontalBox();
    top.add(left);
    top.add(right);
    Container content = aWindow.getContentPane();
    content.setLayout(new BorderLayout());
    content.add(top, BorderLayout.CENTER);
    aWindow.pack();
    aWindow.setVisible(true);
  }
}





Strut spreads the components apart by a fixed distance

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
public class Main {
  public static void main(String[] argv) throws Exception {
    JButton component1 = new JButton();
    Box box = new Box(BoxLayout.X_AXIS);
    int width = 10;
    box.add(Box.createHorizontalStrut(width));
    box.add(component1);
  }
}





Using a BoxLayout Manager

Arranges components either in a row or in a column.

  1. Box class offers a container that uses BoxLayout as its default layout manager.
  2. BoxLayout works to honor each component"s x and y alignment properties as well as its maximum size.

The BoxLayout class has only one constructor:



public BoxLayout(java.awt.Container target, int axis)





Vertical BoxLayout-managed container

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.LayoutManager;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class VerticalBoxLayoutManagerContainerTest {
  public static void main(String[] args) {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container pane = new BoxPanel();
    f.setContentPane(pane);
    BoxLayout bl = new BoxLayout(pane, BoxLayout.Y_AXIS);
    pane.setLayout(bl);
    for (float align = 0.0f; align <= 1.0f; align += 0.25f) {
      JButton button = new JButton("X Alignment = " + align);
      button.setAlignmentX(align);
      pane.add(button);
      pane.add(Box.createRigidArea(new Dimension(0, 15)));
    }
    f.setSize(400, 300);
    f.setVisible(true);
  }
}
class BoxPanel extends JPanel {
  public void paintChildren(Graphics g) {
    super.paintChildren(g);
    Dimension size = getSize();
    LayoutManager manager = getLayout();
    if ((manager != null) && (manager instanceof BoxLayout)) {
      BoxLayout layout = (BoxLayout) manager;
      boolean vertical = true;
      if (vertical) {
        int axis = (int) (layout.getLayoutAlignmentX(this) * size.width);
        g.fillRect(axis - 1, 0, 3, size.height);
      } else {
        int axis = (int) (layout.getLayoutAlignmentY(this) * size.height);
        g.fillRect(0, axis - 1, size.width, 3);
      }
    }
  }
}





X-Axis Alignment

import java.awt.ruponent;
import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class XAxisAlignY {
  private static Container layoutComponents(String title, float alignment) {
    String labels[] = { "-", "-", "-" };
    JPanel container = new JPanel();
    container.setBorder(BorderFactory.createTitledBorder(title));
    BoxLayout layout = new BoxLayout(container, BoxLayout.X_AXIS);
    container.setLayout(layout);
    for (int i = 0, n = labels.length; i < n; i++) {
      JButton button = new JButton(labels[i]);
      button.setAlignmentY(alignment);
      container.add(button);
    }
    return container;
  }
  public static void main(String args[]) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container panel1 = layoutComponents("Top", Component.TOP_ALIGNMENT);
    Container panel2 = layoutComponents("Center", Component.CENTER_ALIGNMENT);
    Container panel3 = layoutComponents("Bottom", Component.BOTTOM_ALIGNMENT);
    frame.setLayout(new GridLayout(1, 3));
    frame.add(panel1);
    frame.add(panel2);
    frame.add(panel3);
    frame.setSize(400, 150);
    frame.setVisible(true);
  }
}





Y-Axis Alignment

import java.awt.ruponent;
import java.awt.Container;
import java.awt.FlowLayout;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class YAxisAlignX {
  private static Container layoutComponents(String title, float alignment) {
    String labels[] = { "--", "----", "--------", "------------" };
    JPanel container = new JPanel();
    container.setBorder(BorderFactory.createTitledBorder(title));
    BoxLayout layout = new BoxLayout(container, BoxLayout.Y_AXIS);
    container.setLayout(layout);
    for (int i = 0, n = labels.length; i < n; i++) {
      JButton button = new JButton(labels[i]);
      button.setAlignmentX(alignment);
      container.add(button);
    }
    return container;
  }
  public static void main(String args[]) {
    JFrame frame = new JFrame("Alignment Example");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container panel1 = layoutComponents("Left", Component.LEFT_ALIGNMENT);
    Container panel2 = layoutComponents("Center", Component.CENTER_ALIGNMENT);
    Container panel3 = layoutComponents("Right", Component.RIGHT_ALIGNMENT);
    frame.setLayout(new FlowLayout());
    frame.add(panel1);
    frame.add(panel2);
    frame.add(panel3);
    frame.pack();
    frame.setVisible(true);
  }
}