Reputation: 1429
The below code is extraction from a Java program. The program code is very big so I am just posting the needed part. In the code, what does the if (i == 2)
do? What does it check, and how can it check for 2?
panel.setLayout(new GridLayout(5, 4, 5, 5));
String[] buttons = {
"Cls", "Bck", "", "Close", "7", "8", "9", "/", "4",
"5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"
};
for (int i = 0; i < buttons.length; i++) {
if (i == 2)
panel.add(new JLabel(buttons[i]));
else
panel.add(new JButton(buttons[i]));
}
Upvotes: 1
Views: 385
Reputation: 882296
It's creating a 4-column by 5-row grid and putting buttons into all but the third one (offset 2). The third one is left blank by putting a label in there.
So what you're getting is the following calculator layout with no button in that third spot:
+-------+-------+ +-------+
| Cls | Bck | | Close |
+-------+-------+-------+-------+
| 7 | 8 | 9 | / |
+-------+-------+-------+-------+
| 4 | 5 | 6 | * |
+-------+-------+-------+-------+
| 1 | 2 | 3 | - |
+-------+-------+-------+-------+
| 0 | . | = | + |
+-------+-------+-------+-------+
It just an easy way to put a list of buttons together in a short loop, with a bit of a kludge to ensure the layout doesn't have a button that does nothing.
For what it's worth, I think the following would be better so that you don't rely on absolute positions:
panel.setLayout(new GridLayout(5, 4, 5, 5));
String[] buttons = {
"Cls" , "Bck" , null , "Close",
"7" , "8" , "9" , "/" ,
"4", , "5" , "6" , "*" ,
"1" , "2" , "3" , "-" ,
"0" , "." , "=" , "+"
};
for (int i = 0; i < buttons.length; i++) {
if (buttons[i] == null)
panel.add(new JLabel(""));
else
panel.add(new JButton(buttons[i]));
}
That just relies on one piece of information, the one in the buttons
array. Of course, it assumes you only want blank labels but, in this case, it's okay since that's all they're being used for.
Upvotes: 3
Reputation: 383956
Given int i
, i == 2
evaluates to true
if and only if i
happens to contain the value 2. Otherwise it's false
.
Thus:
if (i == 2) {
// code executed if i has the value 2
} else {
// code executed if i does NOT have the value 2
}
Note that arrays are zero-indexed. So given this declaration:
String[] buttons = {
"Cls", "Bck", "", "Close", "7", "8", "9", "/", "4",
"5", "6", "*", "1", "2", "3", "-", "0", ".", "=", "+"
};
We have:
buttons[0].equals("Cls")
buttons[1].equals("Bck")
buttons[2].isEmpty()
The entire for
loop therefore goes through each element in String[] buttons
, and for each element except at index 2 (these elements happen to be the ones that aren't empty string), it adds a JButton
with that label to the panel. For the element at index 2 (which happens to be the empty string), it adds a JLabel
instead.
Note that the GridLayout
has 5 rows and 4 columns by the constructor used, so essentially the code creates the following layout:
_______________________________
| | | | |
| Cls | Bck |<blank>| Close |
|_______|_______|_______|_______|
| | | | |
| 7 | 8 | 9 | / |
|_______|_______|_______|_______|
| | | | |
| 4 | 5 | 6 | * |
|_______|_______|_______|_______|
| | | | |
| 1 | 2 | 3 | - |
|_______|_______|_______|_______|
| | | | |
| 0 | . | = | + |
|_______|_______|_______|_______|
println
exampleTo understand the for
loop logic itself, consider the following snippet:
String[] buttons = {
"Cls", "Bck", "", "Close",
"7", "8", "9", "/",
"4", "5", "6", "*",
"1", "2", "3", "-",
"0", ".", "=", "+"
};
for (int i = 0; i < buttons.length; i++) {
if (i == 2) {
System.out.println(buttons[i]);
} else {
System.out.println("[" + buttons[i] + "]");
}
}
The above prints (as seen on ideone.com):
[Cls]
[Bck]
[Close]
[7]
[8]
[9]
[/]
[4]
[5]
[6]
[*]
[1]
[2]
[3]
[-]
[0]
[.]
[=]
[+]
Going back to the GridLayout
, the order that the strings are printed in the above snippet is also the order the components are added into the Panel
, in left-to-right, from-top-to-bottom order (i.e. row-major order).
Upvotes: 1
Reputation: 464
Given the line:
for (int i = 0; i < buttons.length; i++) {
The for
statement creates the variable i
at value 0
. Then it runs the code between {
and }
one time for each value in the array buttons
(a list structure), increasing the variable i
for each time.
The first time through, the line if (i == 2)
will fail, because i
is still 0
, and then calling the code after the else
statement. This adds a button with the text Cls
.
The third time through the code the variable i
will be 2
(because it started at zero), and then it will run the line after if (i == 2)
- because that is what it does.
It will then add a an empty label (the third entry in the array buttons
, arrays start at 0
for the first position.) instead of a button.
I suggest having a look at the Java tutorials for more information on the language and structures of it. :)
Upvotes: 1
Reputation: 14505
Its creating Calculator's key pad. Keeping third column in first row as JLable with empty text.
Wow someone has put how it will look. That should answer it all.
Upvotes: 0
Reputation: 405995
The String[] buttons
defines the text to display on a set of buttons to lay out on the panel (it looks like for a calculator program). Note that the third entry (index 2) is an empty String ""
. The code you listed inserts an empty label instead of a button in that position.
Upvotes: 0