Java Reference
In-Depth Information
Constructing Applet Components
Next, code must be added to the applet to construct Labels for the prompts
and output, a TextField in which the user will enter the sales amount, and a
CheckboxGroup for the three sales code options. Figure 4-42 displays the code
used to construct the applet components.
22
23
//Create components for applet
24
Label promptLabel = new Label (
"Enter the sales amount (do not use commas or dollar signs):" ) ;
25
TextField salesField = new TextField ( 20 ) ;
26
27
Label codeLabel = new Label ( "Select the appropriate commission code:" ) ;
28
29
CheckboxGroup codeGroup = new CheckboxGroup () ;
30
Checkbox telephoneBox = new Checkbox ( "Telephone Sales" , false ,codeGroup )
31
Checkbox inStoreBox = new Checkbox ( "In-Store Sales" , false ,codeGroup ) ;
32
Checkbox outsideBox = new Checkbox ( "Outside Sales" , false ,codeGroup ) ;
33
Checkbox hiddenBox = new Checkbox ( "" , true ,codeGroup ) ;
34
35
Label outputLabel = new Label (
"Click an option button to calculate the sales commission." ) ;
36
FIGURE 4-42
Line 29 in Figure 4-42 constructs an instance of the CheckboxGroup, thus
directing the Java compiler that only one Checkbox in the group can be checked
at one time. Lines 30 through 33 then construct individual instances of the
Checkboxes with unique identifiers. The hidden Checkbox coded in line 33 will
not be added to the applet's user interface, as you will see in later steps. Instead,
the hidden Checkbox is included so that if you want to clear all the other
Checkboxes in the CheckboxGroup, the code can set the hidden Checkbox to
true, thus changing the others to false automatically. Because you want none of
the visible options to be selected when the applet starts, the hidden Checkbox is
set to true, and all other members of the CheckboxGroup are set to false.
The step on the next page enters the code to construct the applet
components.
Search WWH ::




Custom Search