Java Reference
In-Depth Information
DESIGN THE SOLUTION Once you have analyzed the problem, the next step
is to design the user interface and the logic to solve the problem. Utilizing Java's
Abstract Windows Toolkit to take advantage of some of the GUI-based objects, a
user-friendly window needs to be designed. The Frame should be the size and
shape of a simple handheld calculator with appropriately sized buttons, as shown
in Figure 6-1 on page 380. The title bar of the application should display an
appropriate caption and icon. Below the title bar, a TextField is positioned across
the top to display the numbers. Recall from Chapter 5 that the layout manager,
BorderLayout, places components into five regions within the container: North,
South, East, West, and Center. In the Calculator application, the TextField will be
added in the North region of a Frame with BorderLayout. Sixteen buttons will be
placed in a Panel using a GridLayout with four rows and four columns; this
Panel will be added to the Center of the BorderLayout. No components will be
placed in the East, West, or South regions of the BorderLayout.
Each calculator button should have a label with a number or symbol. In
order to simplify the declaration, construction, adding, and labeling of the but-
tons, an array of Button components will be created.
As stated in the requirements document, the Calculator application should
provide a menu system with commands including Exit, Clear, Copy, Paste, and
About Calculator. Figure 6-3 shows the structure of the menu system to be used
in the Calculator application.
FIGURE 6-3
PROGRAM DESIGN Once you have designed the interface, the next step
is to design the logic to solve the problem and create the desired results. The
Calculator application will require each click of a numeric button or the decimal
point button to display in the TextField. When the user clicks an operator button
(+, -, *, or /), the program should store the number from the TextField and then
store the operator for future use in the calculation. If the user clicks the equal
sign, the math should be performed and the result should be displayed in the
TextField. If the user clicks a menu command on the menu bar, the resulting
menu should display available commands.
The program code will include declaration and construction of the various
components, including menu commands; however, the majority of the logic will
take place in the ActionListener event, which the program uses to listen for and
respond to the user clicking a button. Figure 6-4 on the next page displays a
flowchart of what will happen when the user clicks each kind of button.
 
Search WWH ::




Custom Search