Java Reference
In-Depth Information
The following step enters code to declare the variables and construct the
darkRed color.
To Enter Code to Declare Variables and Construct a Color
1. Enter lines 17 through 22 as shown in Figure 4-40.
TextPad displays the declared variables, including the constructed Color
object, in the coding window. All methods in the class will be able to use the
declared variables (Figure 4-41).
constructs new
darkRed Color
object
new code
FIGURE 4-41
Making Decisions in Applets
As with the Commission program created in the previous sections, a Java applet
also may need to execute a specific section of code based on the actions of the
user. For example, if the user enters an invalid number, the applet should display
an error message and then give the user a chance to enter another number. Alter-
natively, if the user makes a choice from a list of options, a Java applet should
switch or branch to a certain set of code statements based on that choice.
Through the use of check boxes, Java applets can allow the user to make
choices that are evaluated by the applet. For instance, a user can click a check
box to select it. The applet uses the ItemListener to listen for that click and then
performs a unique set of instructions associated with that component.
Java applets also support the traditional if…else statements as well as the
switch statement, to make a decision or determine which code to execute based
on a user or program action.
 
Search WWH ::




Custom Search