1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
|
//**********************************************************************
// Package
//**********************************************************************
package doc.example;
//**********************************************************************
// Import list
//**********************************************************************
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.freixas.tablelayout.*;
/**
* This example compares the GridBagLayout to the TableLayout. The
* same layout is created using each layout manager. The layouts are
* displayed in their own window.
*
* @author Antonio Freixas
*/
// Copyright © 2004 Antonio Freixas
// All Rights Reserved.
public class Example1
{
//**********************************************************************
// Public Constants
//**********************************************************************
//**********************************************************************
// Private Constants
//**********************************************************************
//**********************************************************************
// Private Members
//**********************************************************************
//**********************************************************************
// main
//**********************************************************************
public static void
main(
String[] args)
{
new Example1();
}
//**********************************************************************
// Constructors
//**********************************************************************
/**
* Create the two windows and display them.
*/
public
Example1()
{
JFrame frame1 = createGridBagLayout();
JFrame frame2 = createTableLayout();
frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame2.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame1.pack();
frame2.pack();
frame1.setLocation(10, 100);
frame2.setLocation(400, 100);
frame1.setVisible(true);
frame2.setVisible(true);
}
//**********************************************************************
// Private
//**********************************************************************
/**
* Create the layout using GridBagLayout. This code comes from the API
* page for GridBagLayout.
*
* @return The JFrame containing the layout.
*/
public JFrame
createGridBagLayout()
{
JFrame frame = new JFrame("GridBagLayout");
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
JPanel panel = new JPanel(gridbag);
frame.getContentPane().add(panel);
c.fill = GridBagConstraints.BOTH;
c.weightx = 1.0;
makebutton(panel, "Button1", gridbag, c);
makebutton(panel, "Button2", gridbag, c);
makebutton(panel, "Button3", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton(panel, "Button4", gridbag, c);
c.weightx = 0.0; //reset to the default
makebutton(panel, "Button5", gridbag, c); //another row
c.gridwidth = GridBagConstraints.RELATIVE; //next-to-last in row
makebutton(panel, "Button6", gridbag, c);
c.gridwidth = GridBagConstraints.REMAINDER; //end row
makebutton(panel, "Button7", gridbag, c);
c.gridwidth = 1; //reset to the default
c.gridheight = 2;
c.weighty = 1.0;
makebutton(panel, "Button8", gridbag, c);
c.weighty = 0.0; //reset to the default
c.gridwidth = GridBagConstraints.REMAINDER; //end row
c.gridheight = 1; //reset to the default
makebutton(panel, "Button9", gridbag, c);
makebutton(panel, "Button10", gridbag, c);
return frame;
}
/**
* Helper method for createGridBagLayout().
*
* @param panel The panel to add the button to.
* @param name The button's label.
* @param gridbag The GridBagLayout to use.
* @param c The constraints to use.
*/
private void
makebutton(
JPanel panel,
String name,
GridBagLayout gridbag,
GridBagConstraints c)
{
JButton button = new JButton(name);
gridbag.setConstraints(button, c);
panel.add(button);
}
/**
* Create the layout using TableLayout.
*
* @return The JFrame containing the layout.
*/
public JFrame
createTableLayout()
{
JFrame frame = new JFrame("TableLayout");
JPanel panel = new JPanel(new TableLayout("cols=4"));
frame.getContentPane().add(panel);
panel.add(new JButton("Button1"));
panel.add(new JButton("Button2"));
panel.add(new JButton("Button3"));
panel.add(new JButton("Button4"));
panel.add(new JButton("Button5"), "cspan=4");
panel.add(new JButton("Button6"), "cspan=3");
panel.add(new JButton("Button7"));
panel.add(new JButton("Button8"), "rspan=2");
panel.add(new JButton("Button9"), "cspan=3");
panel.add(new JButton("Button10"), "cspan=3 rweight=1");
return frame;
}
}
|