Java Reference
In-Depth Information
To Code the init() Method
1. With the insertion point on line 30, enter the code as shown in
Figure 3-41 on page 184.
The init() method is entered in the coding window (Figure 3-42).
setForeground()
method sets
foreground color
of applet window
calcButton.addActionListener()
method registers ActionListener
add()
method adds
objects to
interface
init() method
code to add
components to
interface
FIGURE 3-42
getImage()
method used
to load image
Because the components were constructed before the init() method, the
compiled bytecode contains the implementation of those components even
before the applet window is initialized. Constructing the components before
coding the init() method will speed processing during execution, as the init()
method quickly can access and add the constructed components to the applet
on the screen. Also, if you try to add a component at the class level, the compiler
sometimes interprets it as a new method without a return data type, and will
not compile the code.
As you enter code to add components to a user interface, remember that
the components will display in the applet window in the order they are entered
in the init() method. To the user, the components thus will display as the applet
title (companyLabel), followed by the height label and text box (heightLabel and
heightField), the weight label and text box (weightLabel and weightField), the
Calculate button (calcButton), and the results message (outputLabel).
Focus and Applet Text Boxes
When applets use components to display text boxes, the first text
box has the focus , which means the insertion point displays in
that text box. Users may press the TAB key to move to subsequent
text boxes. Java programmers should pay close attention to the
order in which they add TextFields to applets, as that sets the
default tab stop sequence.
 
Search WWH ::




Custom Search