@@ -29,18 +29,18 @@ Layouts.Box.getVerticalJPanel(component1, component2, ..., componentN);
29
29
Border and Grid are a tiny bit more complex:
30
30
``` java
31
31
Layouts . Border . buildJPanel()
32
- .setNorth(northComponent)
33
- .setEast (eastComponent). setCenter(centerComponent). setWest (westComponent)
34
- .setSouth(southComponent). getContainer();
32
+ .setNorth(northComponent)
33
+ .setWest (eastComponent). setCenter(centerComponent). setEast (westComponent)
34
+ .setSouth(southComponent). getContainer();
35
35
36
36
Layouts . Grid . buildJPanel(). setRows(2 ). setColumns(3 )
37
- .addRow(component1, component2, component3)
38
- .addRow(component4, component5, component6). getContainer();
37
+ .addRow(component1, component2, component3)
38
+ .addRow(component4, component5, component6). getContainer();
39
39
```
40
40
Notice the use of the word ` build ` instead of ` get ` and the call to ` getContainer() ` at the end.
41
41
This stems from the fact that just adding components to Border and Grid panels is not enough,
42
42
we also need to specify where the component is added to.
43
- Although this is not entirely true in regards to GridLayout, it does offer better readabilty.
43
+ Although this is not entirely true in regards to ` GridLayout ` , it does offer better readabilty.
44
44
45
45
Builders
46
46
--------
@@ -50,10 +50,10 @@ All of the builder's methods return `this`, allowing us to chain together calls.
50
50
For example, the ` FlowLayoutBuilder ` exposes these methods:
51
51
``` java
52
52
Layouts . Flow . buildContainer(... )
53
- .setAlign(FlowLayout . Center )
54
- .setHorizontalGap(5 )
55
- .setVerticalGap(5 )
56
- .add(Components );
53
+ .setAlign(FlowLayout . Center )
54
+ .setHorizontalGap(5 )
55
+ .setVerticalGap(5 )
56
+ .add(Components );
57
57
```
58
58
59
59
Containers
@@ -64,10 +64,53 @@ The `buildContainer()` and `getContainer()` methods alow us to use anything that
64
64
JFrame frame = ...
65
65
Layouts . Border . buildContainer(frame. getContentPane()). setCenter(... );
66
66
67
- // use our own implemtaion of JPanel
67
+ // use our own implementation of JPanel
68
68
class XPanel extends JPanel {... }
69
69
Layouts . Box . getVerticalContainer(new XPanel (), component1, component2, ... , componentN)
70
70
71
71
// or any other component for that matter
72
72
Layouts . Grid . buildContainer(new JButton ()). addRow(component1, component2, ... , componentN)
73
73
```
74
+
75
+ Composition
76
+ -----------
77
+ Since ` Container extends Component ` , we can combine all of the above to create one big mess:
78
+ ``` java
79
+ Layouts . Border . buildContainer(frame. getContentPane()). setCenter(
80
+ Layouts . Border . buildJPanel()
81
+ .setNorth(Layouts . Flow . buildContainer(new XPanel ())
82
+ .setAlign(FlowLayout . Center )
83
+ .setHorizontalGap(5 )
84
+ .setVerticalGap(5 )
85
+ .add(Layouts . Box . getHorizontalJPanel(component1, component2, ... , componentN)). getContainer())
86
+ .setWest(eastComponent). setCenter(Layouts . Grid . buildJPanel(). setRows(2 ). setColumns(3 )
87
+ .addRow(component1, component2, component3)
88
+ .addRow(component4, component5, component6). getContainer()). setEast(westComponent)
89
+ .setSouth(Layouts . Grid . buildContainer(new JButton ())
90
+ .addRow(component1, component2, ... , componentN)). getContainer());
91
+ ```
92
+
93
+ Or something more sensible:
94
+ ``` java
95
+ Layouts . Border . buildContainer(frame. getContentPane())
96
+ .setCenter((Layouts . Border . buildJPanel()
97
+ .setNorth(Layouts . Box . getVerticalJPanel(
98
+ Layouts . Border . buildJPanel()
99
+ .setNorth(label)
100
+ .setCenter(Layouts . Flow . getJPanel(FlowLayout . LEFT , label1, value, label2, field)). getContainer(),
101
+ Layouts . Border . buildJPanel()
102
+ .setNorth(label3)
103
+ .setCenter(Layouts . Flow . getJPanel(FlowLayout . LEFT , label4, stuff, label5, more)). getContainer()))
104
+ .setCenter(Layouts . Border . buildJPanel()
105
+ .setNorth(label6)
106
+ .setCenter(scrollPane). getContainer())
107
+ .setSouth(Layouts . Box . getVerticalJPanel(
108
+ Layouts . Border . buildJPanel()
109
+ .setNorth(label7)
110
+ .setCenter(Layouts . Flow . getJPanel(FlowLayout . LEFT , label8, stuff1, label9, more1)). getContainer(),
111
+ Layouts . Flow . getJPanel(FlowLayout . RIGHT , ok, exit))). getContainer()));
112
+ ```
113
+
114
+ Which results in:
115
+
116
+ ![ Code Example] ( http://i.imgur.com/AT7Ud98.jpg " Code Example ")
0 commit comments