|
BorderLayoutComparison.java
|
1 package com.atticlabs.zonelayout.swing.examples;
2
3 import com.jgoodies.forms.layout.FormLayout;
4 import com.jgoodies.forms.layout.CellConstraints;
5 import com.atticlabs.zonelayout.swing.ZoneLayout;
6 import com.atticlabs.zonelayout.swing.ZoneLayoutFactory;
7
8 import javax.swing.JPanel;
9 import javax.swing.JButton;
10 import javax.swing.JFrame;
11 import javax.swing.SwingUtilities;
12 import java.awt.BorderLayout;
13 import java.awt.GridBagLayout;
14 import java.awt.GridBagConstraints;
15
16 public class BorderLayoutComparison {
17 public static final Object[] values = new Object[] {
18 "Item 1", "Item 2", "Item 3", "Item 4", "Item 5"
19 } ;
20
21 public JPanel buildPanelWithFormLayout() {
22 JButton north = new JButton("north");
23 JButton south = new JButton("south");
24 JButton east = new JButton("east");
25 JButton west = new JButton("west");
26 JButton center = new JButton("center");
27
28 FormLayout layout = new FormLayout(
29 "f:d, f:d:g, f:d",
30 "f:d, f:d:g, f:d"
31 );
32
33 JPanel p = new JPanel(layout);
34 CellConstraints cc = new CellConstraints();
35 p.add(north, cc.xyw(1, 1, 3));
36 p.add(south, cc.xyw(1, 3, 3));
37 p.add(east, cc.xy(3, 2));
38 p.add(west, cc.xy(1, 2));
39 p.add(center, cc.xy(2, 2));
40
41 return p;
42 }
43
44 public JPanel buildPanelWithGridBagLayout() {
45 JButton north = new JButton("north");
46 JButton south = new JButton("south");
47 JButton east = new JButton("east");
48 JButton west = new JButton("west");
49 JButton center = new JButton("center");
50
51 JPanel p = new JPanel(new GridBagLayout());
52 GridBagConstraints gc = new GridBagConstraints();
53 gc.gridx = 0; gc.gridy = 0;
54 gc.gridwidth = 3; gc.fill = GridBagConstraints.HORIZONTAL;
55 p.add(north, gc);
56 gc.gridx = 0; gc.gridy = 1;
57 gc.gridwidth = 1; gc.fill = GridBagConstraints.VERTICAL;
58 p.add(west, gc);
59 gc.gridx = 2; gc.gridy = 1;
60 gc.gridwidth = 1; gc.fill = GridBagConstraints.VERTICAL;
61 p.add(east, gc);
62 gc.gridx = 0; gc.gridy = 2;
63 gc.gridwidth = 3; gc.fill = GridBagConstraints.HORIZONTAL;
64 p.add(south, gc);
65 gc.gridx = 1; gc.gridy = 1;
66 gc.gridwidth = 1; gc.fill = GridBagConstraints.BOTH;
67 gc.weightx = 1.0; gc.weighty = 1.0;
68 p.add(center, gc);
69
70 return p;
71 }
72
73 public JPanel buildPanelWithZoneLayout() {
74 JButton north = new JButton("north");
75 JButton south = new JButton("south");
76 JButton east = new JButton("east");
77 JButton west = new JButton("west");
78 JButton center = new JButton("center");
79
80 ZoneLayout layout = ZoneLayoutFactory.newZoneLayout();
81 JPanel p = new JPanel(layout);
82 layout.addRow("n..-~n");
83 layout.addRow("wc...e");
84 layout.addRow("|.*+.!");
85 layout.addRow("!....|");
86 layout.addRow("w...ce");
87 layout.addRow("s..-~s");
88
89 p.add(north, "n");
90 p.add(south, "s");
91 p.add(east, "e");
92 p.add(west, "w");
93 p.add(center, "c");
94
95 return p;
96 }
97
98 public JPanel buildPanelWithBorderLayout() {
99 JButton north = new JButton("north");
100 JButton south = new JButton("south");
101 JButton east = new JButton("east");
102 JButton west = new JButton("west");
103 JButton center = new JButton("center");
104
105 JPanel p = new JPanel(new BorderLayout());
106 p.add(north, BorderLayout.NORTH);
107 p.add(south, BorderLayout.SOUTH);
108 p.add(east, BorderLayout.EAST);
109 p.add(west, BorderLayout.WEST);
110 p.add(center, BorderLayout.CENTER);
111
112 return p;
113 }
114
115 public static void main(String[] args) {
116 final JFrame formLayoutFrame = new JFrame("FormLayout");
117 formLayoutFrame.getContentPane().add(
118 new BorderLayoutComparison().buildPanelWithFormLayout(), BorderLayout.CENTER);
119
120 formLayoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
121
122 SwingUtilities.invokeLater(new Runnable() {
123 public void run() {
124 formLayoutFrame.pack();
125 formLayoutFrame.setVisible(true);
126 }
127 });
128
129 final JFrame gridBagLayoutFrame = new JFrame("GridBagLayout");
130 gridBagLayoutFrame.getContentPane().add(
131 new BorderLayoutComparison().buildPanelWithGridBagLayout(), BorderLayout.CENTER);
132
133 gridBagLayoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
134
135 SwingUtilities.invokeLater(new Runnable() {
136 public void run() {
137 gridBagLayoutFrame.pack();
138 gridBagLayoutFrame.setVisible(true);
139 }
140 });
141
142 final JFrame zoneLayoutFrame = new JFrame("ZoneLayout");
143 zoneLayoutFrame.getContentPane().add(
144 new BorderLayoutComparison().buildPanelWithZoneLayout(), BorderLayout.CENTER);
145
146 zoneLayoutFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
147
148 SwingUtilities.invokeLater(new Runnable() {
149 public void run() {
150 zoneLayoutFrame.pack();
151 zoneLayoutFrame.setVisible(true);
152 }
153 });
154 }
155 }
156