-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGridBagLayoutKareem.java
More file actions
197 lines (184 loc) · 5.74 KB
/
Copy pathGridBagLayoutKareem.java
File metadata and controls
197 lines (184 loc) · 5.74 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
/* The "GridBagLayoutKareem" class provides an example of the GridBagLayout layout manager; it demonstrates
* the various fields and methods featured in this layout. GridBagLayout organizes components in a dynamic
* grid structure where each component occupies a cell-like space.
*
* @author Kareem Golaub
* @author Ronald Perlas
* @version 2.0 February 24 2014
*/
public class GridBagLayoutKareem extends JPanel implements ActionListener
{
/*
* g Reference Refrences the GridBagLayout class and makes its methods and features available.
*/
GridBagLayout g = new GridBagLayout();
/*
* topRow Reference Refrences the GridBagConstraints class and makes its methods and features available.
*/
GridBagConstraints topRow = new GridBagConstraints();
/*
* moveColumn Reference Refrences the "Move Over" JButton.
*/
JButton moveColumn = new JButton("Move Over");
/*
* growWidth Reference Refrences the "Grow Horizontal Container" JButton.
*/
JButton growWidth = new JButton("Grow Horizontal Container!");
/*
* growHeight Reference Refrences the "Grow Vertical Container" JButton.
*/
JButton growHeight = new JButton("Grow Vertical Container!");
/*
* horizontal Reference Refrences the "Unfill Horizontal" JButton.
*/
JButton horizontal = new JButton("Unfill Horizontal");
/*
* vertical Reference Refrences the "Unfill Vertical" JButton.
*/
JButton vertical = new JButton("Unfill Vertical");
/*
* move Integer Creates a new integer to store movement and initializes it to zero.
*/
int move = 0;
/*
* width Integer Creates a new integer to store width and initializes it to zero.
*/
int width = 0;
/*
* height Integer Creates a new integer to store height and initializes it to zero.
*/
int height = 0;
/*
* This constructor sets the layout of the JPanel, fills the GridBagConstraints and
* executes the buttons() method.
*/
public GridBagLayoutKareem()
{
setLayout(g);
topRow.fill = GridBagConstraints.BOTH;
buttons();
}
/*
* This method sets the dimensions of the GridBagLayout, sets the horizontal and vertical
* constraints and adds the buttons to the JPanel.
*/
public void buttons()
{
removeAll();
topRow.gridwidth = 1;
topRow.gridheight = 1;
topRow.weightx = 0.5;
topRow.gridx = 0+move;
topRow.gridy = 0;
add (moveColumn, topRow);
topRow.gridheight = 1+height;
topRow.gridx = 2;
topRow.gridy = 2-height;
add (growHeight, topRow);
topRow.gridheight = 1;
topRow.gridwidth = 1+width;
topRow.gridx = 0;
topRow.gridy = 1;
add (growWidth, topRow);
topRow.gridwidth = 1;
topRow.gridx = 1;
topRow.gridy = 2;
add(horizontal, topRow);
topRow.gridx = 3;
topRow.gridy = 2;
add(vertical, topRow);
moveColumn.addActionListener(this);
growWidth.addActionListener(this);
growHeight.addActionListener(this);
vertical.addActionListener(this);
horizontal.addActionListener(this);
setLayout(g);
validate();
repaint();
}
/*
* This method responds to ActionEvents when the buttons are clicked and executes the relevant actions.
* This method performs the appropriate action when the "Move Over", "Grow Horizontal Container",
* "Grow Vertical Container", "Fill Vertical", "Fill Horizontal" and their opposite buttons are invoked.
*
* @param horizontalTruth Boolean Stores the horizontal settings, initialized to true.
* @param verticalTruth Boolean Stores the verticals settings, initialized to true.
* @param ae ActionEvent Stores the action command.
*/
public void actionPerformed (ActionEvent ae)
{
Boolean horizontalTruth = true;
Boolean verticalTruth = true;
if (ae.getActionCommand().equals("Move Over"))
{
move = 1;
moveColumn.setText("Move Back");
}
else if (ae.getActionCommand().equals("Move Back"))
{
move = 0;
moveColumn.setText("Move Over");
}
else if (ae.getActionCommand().equals("Grow Horizontal Container!"))
{
width = 1;
growWidth.setText("Shrink Horizontal Container!");
}
else if (ae.getActionCommand().equals("Shrink Horizontal Container!"))
{
width = 0;
growWidth.setText("Grow Horizontal Container!");
}
else if (ae.getActionCommand().equals("Shrink Vertical Container!"))
{
height = 0;
growHeight.setText("Grow Vertical Container!");
}
else if (ae.getActionCommand().equals("Grow Vertical Container!"))
{
height = 1;
growHeight.setText("Shrink Vertical Container!");
}
else if (ae.getActionCommand().equals("Fill Vertical"))
{
verticalTruth = true;
if (!horizontalTruth)
topRow.fill = GridBagConstraints.VERTICAL;
else
topRow.fill = GridBagConstraints.BOTH;
vertical.setText("Unfill Vertical");
}
else if (ae.getActionCommand().equals("Unfill Vertical"))
{
verticalTruth = false;
if (!horizontalTruth)
topRow.fill = GridBagConstraints.NONE;
else
topRow.fill = GridBagConstraints.HORIZONTAL;
vertical.setText("Fill Vertical");
}
else if (ae.getActionCommand().equals("Fill Horizontal"))
{
horizontalTruth = true;
if (!verticalTruth)
topRow.fill = GridBagConstraints.HORIZONTAL;
else
topRow.fill = GridBagConstraints.BOTH;
horizontal.setText("Unfill Horizontal");
}
else
{
horizontalTruth = false;
if (!verticalTruth)
topRow.fill = GridBagConstraints.NONE;
else
topRow.fill = GridBagConstraints.VERTICAL;
horizontal.setText("Fill Horizontal");
}
buttons();
}
}