Java Reference
In-Depth Information
2 What's My Color? (continued)
4. Write the code for the itemStateChanged() method, which uses the get-
State() method to test which Checkbox the user clicked. Use nested if
statements.
5. Compile and test the program. Print a copy of the source code for your
instructor.
6. For extra credit, convert the five Checkboxes to a control array. Then, instead
of multiple ifs, write a for loop that loops through the array using the index
number in the getState() method.
3 Using a Component Array
and Counter-Controlled Loop
You are tutoring programming students and want an application
to demonstrate arrays and looping structures. You decide to create
a panel containing an array of 16 TextField components that
change color to correspond with the start, stop, and step values
entered by the user and to create a looping structure that allows
the student to specify how many times the loop statements will be
executed. Perform the following tasks to create the Checkerboard
Array application shown in Figure 5-50a. When the user enters the
start, stop, and step fields and then clicks the Go button, the results
display as shown in Figure 5-50b.
1. Start TextPad. Create a block comment with your name, date,
program name, and purpose.
2. Import the java.awt.* and awt.event.* packages.
3. Create a class named Checkerboard that extends the Frame
and implements the ActionListener. Save the program on
the Data Disk as a Java source code file with the file name
Checkboard.java.
4. Declare variables for the following components included in
the Frame:
a. an array of 16 TextField components
b. a Panel to hold the array
c. three individual TextField components
d. three int variables to receive the start, stop, and step input
data
e. three Labels to display Start, Stop, and Step
f. a Go button and a Clear button
g. a Panel to hold the three fields and the Labels
h. a Panel to hold the two buttons
5. Create a constructor method named Checkerboard(), which
includes code that:
a. constructs each of the components declared above and sets
the start, stop, and step variables to zero
FIGURE 5-50a
FIGURE 5-50b
Search WWH ::




Custom Search