FileDocCategorySizeDatePackage
BoxLayoutDemo.javaAPI DocExample2811Tue Dec 12 18:59:00 GMT 2000None

BoxLayoutDemo

public class BoxLayoutDemo extends Object

Fields Summary
protected static int
NUM_COMPONENTS
protected static float[]
xAlignment
protected static float[]
hue
protected static boolean
restrictSize
protected static boolean
sizeIsRandom
protected static BLDComponent[]
bldComponent
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


         
        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);
    
public static voidnotifyBLDComponents()

        for (int i = 0; i < NUM_COMPONENTS; i++) {
            bldComponent[i].setSizeRestriction(restrictSize);
        }
        bldComponent[0].revalidate();