Java Reference
In-Depth Information
and return their conjunction.
SR4 . Write and test a function to read a double value and to return the floor
(integer part) of the value, as an int . If the value is outside the range of int ,
return the maximum int value.
GUI JLiveWindow
GUI JLiveWindow (see Fig. 5.4) provides a window on your monitor with up to
7 int fields, 7 double fields, 7 String fields, and a “ready” button. Press the
ready button, and a method buttonPressed is called. In its original version, this
method places the sum of the integers in the first two int fields in the third int
field, but it can be rewritten to do anything you want. Activity 1-5.3 of this CD
provides a better introduction to this GUI than we can give here on paper.
For example, suppose you have written a function that changes a number of
seconds to an hour-minute-seconds format (e.g. 67 could be changed to the string
"0:1:7" ). You can use GUI JLiveWindow to test this program. Change method
buttonPressed to read the first int field, call your function with that integer as
the argument, and place the result in the first String field. Then, test your func-
tion by repeatedly placing an integer in the first int field, pressing the ready but-
ton, and checking the answer that appears in the first String field.
The GUI can be used in real programs as well —wherever simple input or
output is required. It can be much easier to use than keyboard input.
The GUI consists of two classes, JLiveWindow and MyJLiveWindow .
MyJLiveWindow is the only one that you have to change to adapt the GUI to your
needs. In fact, you will have to change only methods buttonPressed and main .
from a footnote
on lesson page
1-5. of the CD
Changing the number of fields in the GUI
Open file in your favorite IDE or editor and look at
method main . You will see an expression that looks like this:
new MyJLiveWindow(4, 0, 0)
The first integer, 4 , is the number of int fields; the second, 0 , the number of
double fields; the third, 0 , the number of String fields —which is what the GUI
in Fig. 5.4 has. Change these integers to any integer in the range 1..7 . That is all
there is to defining the number of fields the GUI should have.
Method buttonPressed
Method buttonPressed is called whenever the ready button is pressed. Here
is the original body of the method:
int sum= getIntField(0) + getIntField(1);
return null ;
Search WWH ::

Custom Search