Java Reference
In-Depth Information
User clicks in
the application
Numeric Buttons
(0 - 9)
Operator Buttons
(+, -, *, /)
Equal Sign
Button (=)
Menu option
Display Number
in TextField
Store current
number
Store current
number
Display Menu
Perform math
with previous
number, operator,
and current number
Store current
operator
File Exit
Edit Clear
About Calculator
Clear
TextField
Display answer
in TextField
Edit Copy
Edit Paste
FIGURE 6-4
Each menu option requires its own code, using appropriate if statements
to determine which command was clicked. The Exit command will call the
System.exit() method. The Clear command will clear the display in the TextField.
The Copy and Paste commands will use Java's clipboard access, which is
described in detail later in the chapter. Finally, the About Calculator command
will display a JOptionPane dialog box with an appropriate message.
The keypad buttons, on the other hand, can share some of the same code
because each button is a member of an array referenced by an index number.
Recall that a major reason to use arrays in coding is the ease of manipulating
data with fewer lines of code. The case structure is ideal for manipulating an
array of 16 buttons in the Calculator application. When clicked, numeric but-
tons, represented in the case structure as cases 0 through 9, will cause the num-
ber to display in the TextField, appended to any previous number displayed
there. Operator buttons, which are represented in the case structure as cases 10
through 13, store the number and the operator in predetermined variable loca-
tions. The Equal button, which is represented in the case structure as case 14,
retrieves the data, performs the math, and displays the answer in the TextField.
The decimal point is case 15.
 
Search WWH ::




Custom Search