Java Reference
In-Depth Information
Coding the init() Method
Figure 4-44 displays the code for the init() method. Lines 39 and 40 set the
background and foreground colors of the applet window, and lines 41 and 42
add the promptLabel and salesField to accept user input. Line 43 sets the focus
to the salesField and line 44 sets the foreground color of the salesField compo-
nent to black. Lines 45 through 52 use the add() method to add the components
to the applet and to add the ItemListener to each of the Checkbox components.
37
public void init ()
38
{
39
setBackground ( darkRed ) ;
40
setForeground ( Color .white ) ;
41
add ( promptLabel ) ;
42
add ( salesField ) ;
43
salesField.requestFocus () ;
44
salesField.setForeground ( Color .black ) ;
45
add ( codeLabel ) ;
46
add ( telephoneBox ) ;
47
telephoneBox.addItemListener ( this ) ;
48
add ( inStoreBox ) ;
49
inStoreBox.addItemListener ( this ) ;
50
add ( outsideBox ) ;
51
outsideBox.addItemListener ( this ) ;
52
add ( outputLabel ) ;
53
}
54
FIGURE 4-44
It is a common coding practice to set the applet background and foreground
colors first and then add additional components and set their background and
foreground colors as they are added to the applet interface. The applet compo-
nents will display in the order they are added. The code to add the ItemListener
for each Checkbox component typically is coded directly after the code used to
add each Checkbox component.
The following step codes the init() method in the applet.
To Code the init() Method
1. Enter the code from lines 39 thorugh 52 as shown in Figure 4-44.
The init() method code will execute at run time (Figure 4-45). The code
completes the init() method by adding the components to the user interface,
defining the foreground and background colors for the applet window and
components, setting the corresponding ItemListener for each Checkbox, and
setting the focus to the salesField TextField component.
Search WWH ::




Custom Search