Java Reference
In-Depth Information
file name
displayed on
title bar
file name
displayed in
Selector
window
new code
entered
variables
defined after
class header
have class
scope
FIGURE 3-38
Declaring variables at the beginning of the program, after the class header,
creates data that can be accessed by the entire program; that is, the variables are
not limited to use by certain methods. The scope of an identifier for a variable,
reference, or method is the portion of the program that can recognize and use
the identifier. The variables in Figure 3-38 have class scope, which means all of
the methods defined in this program will be able to utilize these variables.
Adding Interface Components to an Applet
The java.awt package contains components or object data types that you
can use to build a user interface for applets. Each component , sometimes called
a control, is a Java class with methods to construct, add, and manipulate the
object. Java AWT components, such as Checkbox, List, or MenuItem, begin with
an uppercase letter for each word in the name — referred to as title case. In
the applet version of the Body Mass Index Calculator, you will use three
components for the java.awt.package: Labels, TextFields, and Buttons.
Figure 3-39 shows the code to add the Labels, TextFields, and Button
component to the applet window interface.
21
//construct components
22
Label companyLabel = new Label ( "THE SUN FITNESS CENTER BODY MASS INDEX CALCULATOR" ) ;
23
Label heightLabel = new Label ( "Enter your height to the nearest inch: " ) ;
24
TextField heightField = new TextField ( 10 ) ;
25
Label weightLabel = new Label ( "Enter your weight to the nearest pound: " ) ;
26
TextField weightField = new TextField ( 10 ) ;
27
Button calcButton = new Button ( "Calculate" ) ;
28
Label outputLabel = new Label ( "Click the Calculate button to see your body mass index." ) ;
29
FIGURE 3-39
 
Search WWH ::




Custom Search