Java Reference
In-Depth Information
The assignment gets the values of the first two
int
fields and stores their sum in
sum
. Here, you can see that the following expression has as its value the integer
that is in
int
field
i
(for
i
in the range
0..6
—the first field is numbered
0
):
getIntField(i)
In the same way, it is easy to store a value in a field. The second statement,
setIntField(2,sum);
stores in
int
field
2
the value that is in variable
sum
. In general, the statement
setIntField(i,e);
stores the value of expression
e
in
int
field
i
, where expression
i
is in
0..6
.
Methods also exist for accessing and changing the
double
and String fields:
getdoubleField(i)
getStringField(i)
setDoubleField(i,e);
setStringField(i,e);
Executing the GUI
Execution of the following call creates and shows the GUI window.
Thereafter, you can drag the window where you want, resize it, and use the fields
and ready button.
MyJLiveWindow.main(
null
);
An example
Consider the following method:
/** =
the number of seconds
n
, but given in the form
hours:minutes:seconds
.
Precondition:
n>=0 */
public static
String convert(
int
n) {
int
hours= n / 3600;
int
remainder= n % 3600;
return
hours + ":" + (remainder / 60) +
":" + (remainder % 60);
}
To test this method, place it in class
MyJLiveWindow
, change it so that it has one
int
field and one
String
field, and write method
buttonPressed
as follows:
/** D
isplay value convert(first int field) in the first string field
*/
public
Object buttonPressed() {
setStringField(0, convert(getIntField(0)));
return null
;
}
Search WWH ::
Custom Search