final JPanel panel = new JPanel();
panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
//Create the rectangles.
int shortSideSize = 15;
for (int i = 0; i < NUM_COMPONENTS; i++) {
if (sizeIsRandom) {
shortSideSize = (int)(30.0 * Math.random()) + 30;
} else {
shortSideSize += 10;
}
bldComponent[i] = new BLDComponent(xAlignment[i], hue[i],
shortSideSize,
restrictSize,
sizeIsRandom,
String.valueOf(i));
panel.add(bldComponent[i]);
}
//Create the instructions.
JLabel label = new JLabel("Click a rectangle to "
+ "change its X alignment.");
JCheckBox cb = new JCheckBox("Restrict maximum rectangle size.");
cb.setSelected(restrictSize);
cb.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
if (e.getStateChange() == ItemEvent.SELECTED) {
restrictSize = true;
} else {
restrictSize = false;
}
notifyBLDComponents();
}
});
JFrame f = new JFrame("BoxLayoutDemo");
Container contentPane = f.getContentPane();
contentPane.add(panel, BorderLayout.CENTER);
panel.setBorder(BorderFactory.createLineBorder(Color.red));
Box box = Box.createVerticalBox();
box.add(label);
box.add(cb);
contentPane.add(box, BorderLayout.SOUTH);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.pack();
f.setVisible(true);