Studyon Minte9.com
Java SE




Box Layout



The BoxLayout class puts components in a single row or column.
It lets you align components.

package testexample; import javax.swing.*; import java.awt.*; public class Test extends JFrame { public static void main(String[] args) { Test frame = new Test(); frame.setBounds(200, 200, 300, 300); frame.setDefaultCloseOperation(EXIT_ON_CLOSE); frame.setVisible(true); } public Test() { JPanel top = new JPanel(); top.setPreferredSize(new Dimension(300, 46)); top.setBorder(BorderFactory.createTitledBorder("top")); JPanel left = new JPanel(); left.setPreferredSize(new Dimension(50, 190)); left.setBorder(BorderFactory.createTitledBorder("left")); JPanel center = new JPanel(); center.setPreferredSize(new Dimension(230, 190)); center.setBorder(BorderFactory.createTitledBorder("center")); add(top, BorderLayout.PAGE_START); add(left, BorderLayout.LINE_START); add(center, BorderLayout.CENTER); // --- Look Here --- // top.setLayout(new BoxLayout(top, BoxLayout.LINE_AXIS)); JPanel t1 = new JPanel(); t1.setMaximumSize(new Dimension(50, 5)); t1.setBackground(Color.yellow); t1.setAlignmentY(BOTTOM_ALIGNMENT); // --- align bottom --- // top.add(t1); JPanel t2 = new JPanel(); t2.setMaximumSize(new Dimension(70, 10)); t2.setBackground(Color.red); t2.setAlignmentY(BOTTOM_ALIGNMENT); top.add(t2); JPanel t3 = new JPanel(); t3.setMaximumSize(new Dimension(90, 20)); t3.setBackground(Color.blue); t3.setAlignmentY(BOTTOM_ALIGNMENT); top.add(t3); // --- Look Here --- // center.setLayout(new BoxLayout(center, BoxLayout.PAGE_AXIS)); JPanel c1 = new JPanel(); c1.setMaximumSize(new Dimension(100, 50)); c1.setBackground(Color.yellow); center.add(c1); JPanel c2 = new JPanel(); c2.setMaximumSize(new Dimension(150, 50)); c2.setBackground(Color.red); center.add(c2); JPanel c3 = new JPanel(); c3.setMaximumSize(new Dimension(200, 50)); c3.setBackground(Color.blue); center.add(c3); } }
 


http://docs.oracle.com/javase/tutorial/uiswing/layout/box.html