Java Reference
In-Depth Information
Figure 12
An Application with a Text Field
When that button is clicked, its
action
Performed
method reads the user input
from the text fields, using the
getText
method of the
JTextField
class. The
getText
method returns a
String
object. In our sample program, we turn the
string into a number, using the
Double.parseDouble
method:
480
481
class AddInterestListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
double rate =
Double.parseDouble(rateField.getText());
. . .
}
}
The following application is a useful prototype for a graphical user-interface front end
for arbitrary calculations. You can easily modify it for your own needs. Place other
input components into the frame. Change the contents of the
actionPerformed
method to carry out other calculations. Display the result in a label.
ch10/textfield/InvestmentViewer3.java
1
import
javax.swing.JFrame;
2
3 /**
4
This program displays the growth of an investment.
5 */
6
public class
InvestmentViewer3
7 {
8
public static void
main(String[] args)
9 {