Java Reference
In-Depth Information
LISTING 4.13
continued
{
//--------------------------------------------------------------
// Performs the conversion when the enter key is pressed in
// the text field.
//--------------------------------------------------------------
public void actionPerformed (ActionEvent event)
{
int fahrenheitTemp, celsiusTemp;
String text = fahrenheit.getText();
fahrenheitTemp = Integer.parseInt (text);
celsiusTemp = (fahrenheitTemp-32) * 5/9;
resultLabel.setText (Integer.toString (celsiusTemp));
}
}
}
used to listen to multiple components at the same time. So the listener could be
added to both the text field and the button, giving the user the option. Pressing
either the button or the Enter key will cause the conversion to be performed.
These variations are left as programming projects.
SELF-REVIEW QUESTIONS (see answers in Appendix N)
SR 4.34 Describe what happens within the Fahrenheit program when a user
types a number into the text box of the interface and presses the Enter
(or Return) key.
SR 4.35 Change the FahrenheitPanel class so that when the user enters a
number in the text field a statement appears in the form “X degrees
Fahrenheit = Y degrees Celsius” below the text field. For example,
if the user enters 223, the statement “223 degrees Fahrenheit = 106
degrees Celsius” appears instead of “Temperature in Celsius: 106”.
 
Search WWH ::




Custom Search