Java Reference
In-Depth Information
TABLE 6-4
Some Methods of the
class
JTextField
(continued)
Method / Description
public
JTextField(String str, int columns)
//Constructor to initialize the object with the text specified
//by str and to set the size of the text field.
public void
setText(String str)
//Method to set the text of the text field to the string specified
//by str.
public
String getText()
//Method to return the text contained in the text field.
public void
setEditable(
boolean
b)
//If the value of the boolean variable b is false, the user cannot
//type in the text field.
//In this case, the text field is used as a tool to display
//the result.
public void
addActionListener(ActionListener obj)
//Method to register a listener object to a JTextField.
Consider the following statements:
private
JTextField lengthTF, widthTF, areaTF,
perimeterTF;
//Line 1
lengthTF =
new
JTextField(10);
//Line 2
widthTF =
new
JTextField(10);
//Line 3
areaTF =
new
JTextField(10);
//Line 4
perimeterTF =
new
JTextField(10);
//Line 5
The statement in Line 1 declares four reference variables,
lengthTF
,
widthTF
,
areaTF
,and
perimeterTF
,oftype
JTextField
. The statement in Line 2 instantiates the
object
lengthTF
and sets the width of this text field to 10 characters. That is, this text field
can display no more than 10 characters. The meaning of the other statements is similar.
Placing these objects involves using the
add
method of the
class
Container
as described
in the previous section. The following statements add these components to the container:
Search WWH ::
Custom Search