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