Java Reference
In-Depth Information
2 Creating a Telephone Keypad Application (continued)
b. When a match is found, transfer the label from the button to
TextField, concatenating any previous entries. Your code will look
similar to lcdTextField.setText(lcd TextField.getText() +
keys[i].getLabel( )) ; although your variable names may differ.
8. Create a main() method with the following code:
a. Construct an instance of the Telephone.
b. Set the bounds of the Frame to 50, 130, 250, and 300.
c. Set the title of the Frame to Telephone.
d. Set the visibility of the Frame to true.
9. Enter the closing brace for the class. Print a copy to double-check the
matching of braces and parentheses before compiling.
10. Save the file as Telephone.java on the Data Disk.
11. Compile the source code. Fix any errors and recompile, if necessary.
12. Run and then test the application by clicking each number and symbol.
13. Print a copy of the source code for your instructor.
14. Several WebPhone customers have asked if the telephone keypad could
include a CLR button to clear the display. If directed by your instructor as an
extra credit assignment, convert the application to an applet and then add a
CLR button to clear a previous number or erase numbers entered in error.
3 Creating a Clickable Applet
Figure 6-52 displays an applet with a graphic. The buttons in the Panel at the
bottom of the applet direct the movement of the graphic. The Java cup graphic,
cup.gif, is on the Data Disk that is provided with this topic. If you wish, you may
substitute your own graphic. Be sure to place the graphic in the same directory
as your program.
In this applet, you will use the getDocumentBase()
method to import a graphic and the repaint() method
along with drawImage. The four arguments needed by the
drawImage() method are the name of the image, the top
pixel, the left pixel, and the self-referential, this. Perform the
following steps to create the host document and source code
file.
1. In the TextPad window, create an HTML host document
that calls the file MoveIt.class, using a width of 325 and a
height of 250.
2. Save the file as MoveIt.html on your Data Disk.
3. Open a new TextPad file and save it as a Java source code
file on the Data Disk with the file name MoveIt.java.
4. Create a block comment with your name, date, program
name, and purpose.
5. Import the java.awt.*, java.awt.event.*, and java.applet.*
packages.
FIGURE 6-52
Search WWH ::




Custom Search