samer@1: import java.awt.*; samer@1: import java.awt.event.*; samer@1: import javax.swing.*; samer@1: samer@1: public class BoxLayoutDemo { samer@1: protected static int NUM_COMPONENTS = 3; samer@1: protected static float[] xAlignment = {Component.LEFT_ALIGNMENT, samer@1: Component.CENTER_ALIGNMENT, samer@1: Component.RIGHT_ALIGNMENT}; samer@1: protected static float[] hue = {0.0f, 0.33f, 0.67f}; samer@1: protected static boolean restrictSize = true; samer@1: protected static boolean sizeIsRandom = false; samer@1: protected static BLDComponent[] bldComponent = samer@1: new BLDComponent[NUM_COMPONENTS]; samer@1: samer@1: public static void main(String[] args) { samer@1: final JPanel panel = new JPanel(); samer@1: panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); samer@1: samer@1: //Create the rectangles. samer@1: int shortSideSize = 15; samer@1: for (int i = 0; i < NUM_COMPONENTS; i++) { samer@1: if (sizeIsRandom) { samer@1: shortSideSize = (int)(30.0 * Math.random()) + 30; samer@1: } else { samer@1: shortSideSize += 10; samer@1: } samer@1: bldComponent[i] = new BLDComponent(xAlignment[i], hue[i], samer@1: shortSideSize, samer@1: restrictSize, samer@1: sizeIsRandom, samer@1: String.valueOf(i)); samer@1: panel.add(bldComponent[i]); samer@1: } samer@1: samer@1: //Create the instructions. samer@1: JLabel label = new JLabel("Click a rectangle to " samer@1: + "change its X alignment."); samer@1: JCheckBox cb = new JCheckBox("Restrict maximum rectangle size."); samer@1: cb.setSelected(restrictSize); samer@1: cb.addItemListener(new ItemListener() { samer@1: public void itemStateChanged(ItemEvent e) { samer@1: if (e.getStateChange() == ItemEvent.SELECTED) { samer@1: restrictSize = true; samer@1: } else { samer@1: restrictSize = false; samer@1: } samer@1: notifyBLDComponents(); samer@1: } samer@1: }); samer@1: samer@1: JFrame f = new JFrame("BoxLayoutDemo"); samer@1: Container contentPane = f.getContentPane(); samer@1: contentPane.add(panel, BorderLayout.CENTER); samer@1: panel.setBorder(BorderFactory.createLineBorder(Color.red)); samer@1: samer@1: Box box = Box.createVerticalBox(); samer@1: box.add(label); samer@1: box.add(cb); samer@1: samer@1: contentPane.add(box, BorderLayout.SOUTH); samer@1: f.addWindowListener(new WindowAdapter() { samer@1: public void windowClosing(WindowEvent e) { samer@1: System.exit(0); samer@1: } samer@1: }); samer@1: f.pack(); samer@1: f.setVisible(true); samer@1: } samer@1: samer@1: static public void notifyBLDComponents() { samer@1: for (int i = 0; i < NUM_COMPONENTS; i++) { samer@1: bldComponent[i].setSizeRestriction(restrictSize); samer@1: } samer@1: bldComponent[0].revalidate(); samer@1: } samer@1: }