Java Reference
In-Depth Information
In line 177, a flag variable named foundKey is set to false. Setting a flag vari-
able such as this one to false later will allow testing to prevent actions from being
performed unless a button is clicked directly (for example, a user might acciden-
tally click the display field, the title bar, or a border of the Frame). If a button
click is detected, the foundKey flag variable will be set to true.
Next, the actionPerformed() method performs a sequential search, checking
each button to determine if it was clicked by the user. Starting in line 180, the
code first looks for which key was pressed by traversing, or going through, the
array one item at a time.
Notice that the termination of the for loop occurs when the increment goes
beyond the length of the array and the foundKey is false (line 180). Line 180 uses
the length property rather than the actual number of items in the array (in this
case, 16 for the 16 buttons). This ensures that if you wish to add more buttons to
the calculator, the search will execute accurately without requiring a change to
the number in that line of code.
If the source of the click, stored in the object e, matches one of the buttons
in the keys (line 182), the code inside the if block begins. Line 184 toggles the
foundKey variable to true when a match is found. The location of the match, at
the i position in the array, becomes the variable used in the subsequent switch
statement in line 185.
The different case statements in line 188 of the switch structure deserve spe-
cial consideration. As you have learned, Java uses a switch structure to test a
variable against multiple possibilities. Typically, each case ends with a break
statement; however, you can use one break statement for multiple cases if that
one action can serve for multiple cases. For example, in the Calculator applica-
tion, the first possibility is that the user may click one of the numeric buttons or
the decimal point. Line 188 combines all of these cases, delimited with colons (:),
into one line of code. Java allows each case to perform the same action as long as
break statements do not separate the cases.
Lines 189 though 195 define the action that results when a user clicks a
number key or the decimal point key. First, an if statement in line 189 tests
clearText. Recall that the variable, clearText, was initialized to true at the begin-
ning of the program. Therefore, the first button click will execute the code inside
the if statement automatically. The code in line 191 then clears the lcd TextField
by setting the text to a null String. Line 192 sets the clearText flag to false. When
the actionPerformed() method is called with each button click, line 194 causes
the program to display the label from the clicked button in the lcd TextField,
regardless of whether it is the first click or a subsequent click. To display a num-
ber with multiple digits, such as 12345, the result of the getLabel() method is
concatenated with previous text to display in the lcd TextField, thereby display-
ing the entire number in correct order.
For example, assume that the user begins the program and enters the num-
ber 27. When the user clicks the 2 button, the actionPerformed event picks up
the click and then clears the lcd TextField. The label from keys[2] then is dis-
played in the lcd TextField. When the user then clicks a 7, the label from keys[7]
is concatenated (+) with the previous text in the field, and 27 displays in the lcd
TextField. The integers 0 through 9 and the decimal points are entered this way.
The step on the next page enters the code that executes when a user clicks a
numeric button or the decimal point button.
Search WWH ::




Custom Search