Java Reference
In-Depth Information
LABELS A Label is an object that displays text in the applet window. Labels are
assigned a string of characters, or a text value, by the programmer. A common
usage is to use a constructor that assigns the string of characters to the compo-
nent, as shown in line 22 in Figure 3-39 on the previous page. Recall that a con-
structor is a method used to create an instance of a class, and that the
constructor has the same name as the class — in this case, the Label() method.
The Label instance is constructed during compilation. During execution, the
Label is added to the applet window and displays its message text.
Label Data Type vs. Label() Method
The word, Label, on the left of the assignment operator before the
identifier is a reference data type. In that capacity, it is a declara-
tion for the variable name that will hold the Label object. The
word Label, with its argument on the right side of the assignment
operator, is a constructor or method used to create an instance of
the class and add the caption. Java considers Label and Label() as
two different concepts. Most constructors use a similar concept
with the data type and method.
TEXTFIELDS A TextField is an object that displays a text box in which users
enter text. Like the Label components, a TextField component displays inside the
applet window. The code calls the constructor or method TextField(), which has
a width argument to define the character width of the text box during execution,
as shown in line 24 of Figure 3-39.
BUTTONS A Button is an object that displays a command button for users to
click. Most computer users are very familiar with command buttons and know
to click a command button to trigger an event. Typically, Buttons inherit their
characteristics, such as color and shape, from the operating system, but pro-
grammers can define the caption on the Button, as well as the actions to be
performed when it is clicked. As shown in line 27 of Figure 3-39, the code calls
the constructor or method Button(), which has a caption argument to define the
string of characters that displays on the face of the button during execution. The
constructor code for a Button is similar to a Label constructor.
Programming Conventions
Component identifiers must follow the same Java naming rules as variable
identifiers, but programmers differ in their specific naming conventions. A
naming convention is the way you use words, case, prefixes, and underscores to
name the identifiers in your program. The main reason for using a consistent set
of naming conventions within a given program is to standardize the structure
and coding style of an application, so that you and others may read and under-
stand the code more easily.
Some programmers name their applet components with a three-letter prefix
similar to the ones used in the Visual Basic programming language, such as
lblTitle or txtName. Others use few, if any, component-specific prefixes, simply
Search WWH ::




Custom Search