Java Reference
In-Depth Information
1 Enhancing a Frame (continued)
4. Make the following changes to the program:
a. In the main() method, set the background color to red.
b. Enter two arguments for the BorderLayout to set the spacing between
components as 20 pixels horizontally and 5 pixels vertically.
5. Save the source code in TextPad using the same file name. Compile the
program and fix any errors, if necessary. Run the program.
6. Make the following changes to the program.
a. Implement the ActionListener in the class header.
b. Change the names of the Buttons to Red, Yellow, Cyan, Magenta, and
White. Add the ActionListener to each Button.
c. Below the main() method but still within the class block, enter an
actionPerformed() method using the header, public void
actionPerformed(ActionEvent e) and an opening brace.
d. Declare and assign a variable, arg, by typing, String arg =
e.getActionCommand(); within the block.
e. Test for the click of each button by writing an if statement similar to the
following: if (arg == "Yellow") . The result of a button click should
be a change in the background color of the application.
7. Save the source code. Compile, correct any errors, and then run the applet.
Print a copy of the source code.
8. Finally, make the following changes to the program:
a. Delete the button in the center area. Replace it with a Choice component
named colors.
b. Use the add() method to populate the Choice component with the colors
from step 6b above.
c. At the end of the Buttons class header, insert a comma, and then type
ItemListener to implement the ItemListener.
d. Add an itemListener to the Choice component and then add the Choice
component to the center area of the BorderLayout.
e. Write an itemStateChanged() method to test for each item in the
Choice component using the header, public void itemStateChanged
(ItemEvent ie) , and an opening brace. Use the same code as you did
in the actionPerformed() method to test for the click of each choice
(step 6e above).
9. Save the source code. Compile, correct any errors, and then run the pro-
gram. Print a copy of the source code.
2 Creating a Telephone Keypad Application
Many computer systems are connected to modems, telephones, and fax systems.
As part of the programming team at WebPhone, you have been asked to design
the user interface for a telephone keypad that displays on the screen. Figure 6-51
displays a sample of how the user interface of the application should look after
programming is complete.
Search WWH ::




Custom Search