Java Reference
In-Depth Information
Using the techniques you have learned, including the steps
you performed in this chapter, write a Java application that dis-
plays a telephone keypad. Add functionality so that when a
button is clicked, the number or symbol label for each button
displays in the TextField. Perform the following steps to create
the Telephone program.
1. Start TextPad. Create a block comment with your name,
date, program name, and purpose.
2. Import Java's awt and awt.event packages.
3. Create a class named Telephone that extends the Frame
and implements the ActionListener.
4. Declare public variables as follows:
an array of buttons
a keypad
a TextField
a Label
a variable named foundKey
5. Create a Telephone() constructor method and include the following:
a. Write a constructor for the TextField with a length of 20.
b. Write a constructor to create the keypad Panel.
c. Write a constructor to set the array to be of length 12.
d. Set the Buttons' labels to match the nine buttons shown in Figure 6-51.
e. Set the TextField to be not editable.
f. Type setBackground(Color.magenta); to change the color of the
Frame's background.
g . Ty p e setLayout(new BorderLayout()); to declare the layout manager
for the Frame.
h. Type keyPad.setLayout(new GridLayout(4,3,10,10)); to declare
the layout manager for the Panel.
i. Add the buttons to the keypad in order from upper-left to lower-right.
j. Add the TextField to the North area of the Frame.
k. Add the Panel to the Center area of the Frame.
l. Add the Label to the South area of the Frame.
6. Register a WindowListener by typing the code as shown in Figure 6-20 on
page 398.
7. Create an actionPerformed event to include the following:
a. Write a loop to traverse the array. Test the member of the array at
the index number to see if it matches the argument passed to the
actionPerformed event. Your code will look similar to
if(e.getSource() == keys[i]) although your variable
names may differ.
FIGURE 6-51
(continued)
Search WWH ::




Custom Search