samer@1
|
1 import java.awt.*;
|
samer@1
|
2 import java.awt.event.*;
|
samer@1
|
3 import javax.swing.*;
|
samer@1
|
4
|
samer@1
|
5 public class BoxLayoutDemo {
|
samer@1
|
6 protected static int NUM_COMPONENTS = 3;
|
samer@1
|
7 protected static float[] xAlignment = {Component.LEFT_ALIGNMENT,
|
samer@1
|
8 Component.CENTER_ALIGNMENT,
|
samer@1
|
9 Component.RIGHT_ALIGNMENT};
|
samer@1
|
10 protected static float[] hue = {0.0f, 0.33f, 0.67f};
|
samer@1
|
11 protected static boolean restrictSize = true;
|
samer@1
|
12 protected static boolean sizeIsRandom = false;
|
samer@1
|
13 protected static BLDComponent[] bldComponent =
|
samer@1
|
14 new BLDComponent[NUM_COMPONENTS];
|
samer@1
|
15
|
samer@1
|
16 public static void main(String[] args) {
|
samer@1
|
17 final JPanel panel = new JPanel();
|
samer@1
|
18 panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
|
samer@1
|
19
|
samer@1
|
20 //Create the rectangles.
|
samer@1
|
21 int shortSideSize = 15;
|
samer@1
|
22 for (int i = 0; i < NUM_COMPONENTS; i++) {
|
samer@1
|
23 if (sizeIsRandom) {
|
samer@1
|
24 shortSideSize = (int)(30.0 * Math.random()) + 30;
|
samer@1
|
25 } else {
|
samer@1
|
26 shortSideSize += 10;
|
samer@1
|
27 }
|
samer@1
|
28 bldComponent[i] = new BLDComponent(xAlignment[i], hue[i],
|
samer@1
|
29 shortSideSize,
|
samer@1
|
30 restrictSize,
|
samer@1
|
31 sizeIsRandom,
|
samer@1
|
32 String.valueOf(i));
|
samer@1
|
33 panel.add(bldComponent[i]);
|
samer@1
|
34 }
|
samer@1
|
35
|
samer@1
|
36 //Create the instructions.
|
samer@1
|
37 JLabel label = new JLabel("Click a rectangle to "
|
samer@1
|
38 + "change its X alignment.");
|
samer@1
|
39 JCheckBox cb = new JCheckBox("Restrict maximum rectangle size.");
|
samer@1
|
40 cb.setSelected(restrictSize);
|
samer@1
|
41 cb.addItemListener(new ItemListener() {
|
samer@1
|
42 public void itemStateChanged(ItemEvent e) {
|
samer@1
|
43 if (e.getStateChange() == ItemEvent.SELECTED) {
|
samer@1
|
44 restrictSize = true;
|
samer@1
|
45 } else {
|
samer@1
|
46 restrictSize = false;
|
samer@1
|
47 }
|
samer@1
|
48 notifyBLDComponents();
|
samer@1
|
49 }
|
samer@1
|
50 });
|
samer@1
|
51
|
samer@1
|
52 JFrame f = new JFrame("BoxLayoutDemo");
|
samer@1
|
53 Container contentPane = f.getContentPane();
|
samer@1
|
54 contentPane.add(panel, BorderLayout.CENTER);
|
samer@1
|
55 panel.setBorder(BorderFactory.createLineBorder(Color.red));
|
samer@1
|
56
|
samer@1
|
57 Box box = Box.createVerticalBox();
|
samer@1
|
58 box.add(label);
|
samer@1
|
59 box.add(cb);
|
samer@1
|
60
|
samer@1
|
61 contentPane.add(box, BorderLayout.SOUTH);
|
samer@1
|
62 f.addWindowListener(new WindowAdapter() {
|
samer@1
|
63 public void windowClosing(WindowEvent e) {
|
samer@1
|
64 System.exit(0);
|
samer@1
|
65 }
|
samer@1
|
66 });
|
samer@1
|
67 f.pack();
|
samer@1
|
68 f.setVisible(true);
|
samer@1
|
69 }
|
samer@1
|
70
|
samer@1
|
71 static public void notifyBLDComponents() {
|
samer@1
|
72 for (int i = 0; i < NUM_COMPONENTS; i++) {
|
samer@1
|
73 bldComponent[i].setSizeRestriction(restrictSize);
|
samer@1
|
74 }
|
samer@1
|
75 bldComponent[0].revalidate();
|
samer@1
|
76 }
|
samer@1
|
77 }
|