Java Reference
In-Depth Information
To Enter Code to Search for the About Command
1. Enter lines 170 through 175 as shown in Figure 6-26.
The TextPad window displays the remainder of the if statement to determine
which menu command the user clicked (Figure 6-27). If the user clicked the
About Calculator command, the program will display information about the
program in a message box (Figure 6-27).
tests if
the About
command
was clicked
code to
display About
Calculator
message box
FIGURE 6-27
Recall that the arguments for the showMessageDialog() include placement of
the message box (null defaults to centered), a message, a title bar caption, and an
intrinsic constant representing one of the message icons.
Searching the Numeric Buttons
Figure 6-28 displays the code to search the array of buttons to determine
which button the user clicked and to add functionality to the numeric buttons
and the decimal point in the actionPerformed() method.
176
// test for button clicks
177
foundKey = false ;
178
179
// search for the clicked key
180
for ( int i=0; i<keys.length && !foundKey; i++ )
181
{
182
if ( e.getSource () == keys [ i ])
183
{
184
foundKey = true ;
185
switch ( i )
186
{
187
// number and decimal point buttons
188
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8:
case 9: case 15:
189
if ( clearText )
190
{
191
lcd.setText ( "" ) ;
192
clearText = false ;
193
}
194
lcd.setText ( lcd.getText () + keys [ i ] .getLabel ()) ;
195
break ;
196
FIGURE 6-28
 
Search WWH ::




Custom Search