Write an application that performs various temperature conversions. The application should be capable of performing two types of con- versions: degrees Fahrenheit to degrees Celsius and degrees Celsius to degrees Fahrenheit. Your output should look like Fig. 13.24.



The conversion should take place when the user enters a temperature in one of the JTextFields and presses Enter. Pressing Enter while the focus is on a JTextField generates an ActionEvent and calls event handler actionPerformed.

a) Copying the template to your working directory. Copy the C:Examples Tutorial13ExercisesTemperatureConversion2 directory to your C:Simply- Java directory.

b) Opening the template file. Open the TemperatureConversion.java file in your text editor.

c) Completing the actionPerformed event handler for fahrenheitJTextField. On line 57, add code to call method fahrenheitJTextFieldActionPerformed. Pass to the method the ActionEvent object event as the argument.

d) Coding the fahrenheitJTextFieldActionPerformed method. Starting after the createUserInterface method, on line 97, declare the fahrenheit- JTextFieldActionPerformed method. This method should get the value from fahrenheitJTextField, convert it to Celsius and display it in

```
1 // Exercise 13.12: TemperatureConversion.java
2 // Converts a degree amount from Celsius to Fahrenheit,
3 // or from Fahrenheit to Celsius.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7 import java.text.*;
8
9 public class TemperatureConversion extends JFrame
10 {
11 // JLabel and JTextField for Fahrenheit
12 private JLabel fahrenheitJLabel;
13 private JTextField fahrenheitJTextField;
14
15 // JLabel and JTextField for Celsius
16 private JLabel celsiusJLabel;
17 private JTextField celsiusJTextField;
18
19 // DecimalFormat for temperatures
20 DecimalFormat temperature = new DecimalFormat( "0.0" );
21
22 // no-argument constructor
23 public TemperatureConversion()
24 {
25 createUserInterface();
26 }
27
28 // create and position GUI components; register event handlers
29 private void createUserInterface()
30 {
31 // get content pane for attaching GUI components
32 Container contentPane = getContentPane();
33
34 // enable explicit positioning of GUI components
35 contentPane.setLayout( null );
36
37 // set up fahrenheitJLabel
38 fahrenheitJLabel = new JLabel();

39 fahrenheitJLabel.setBounds( 12, 24, 72, 21 );
40 fahrenheitJLabel.setText( "Fahrenheit:" );
41 contentPane.add( fahrenheitJLabel );
42
43 // set up fahrenheitJTextField
44 fahrenheitJTextField = new JTextField();
45 fahrenheitJTextField.setBounds( 78, 24, 64, 21 );
46 fahrenheitJTextField.setHorizontalAlignment(
47 JTextField.RIGHT );
48 contentPane.add( fahrenheitJTextField );
49 fahrenheitJTextField.addActionListener(
50
51 new ActionListener() // anonymous inner class
52 {
53 // event handler called when enter is pressed in
54 // fahrenheitJTextField
55 public void actionPerformed( ActionEvent event )
56 {
57 fahrenheitJTextFieldActionPerformed( event );
58 }
59
60 } // end anonymous inner class
61
62 ); // end call to addActionListener
63
64 // set up celsiusJLabel
65 celsiusJLabel = new JLabel();
66 celsiusJLabel.setBounds( 162, 24, 56, 21 );
67 celsiusJLabel.setText( "Celsius:" );
68 contentPane.add( celsiusJLabel );
69
70 // set up celsiusJTextField
71 celsiusJTextField = new JTextField();
72 celsiusJTextField.setBounds( 212, 24, 64, 21 );
73 celsiusJTextField.setHorizontalAlignment( JTextField.RIGHT );
74 contentPane.add( celsiusJTextField );
75 celsiusJTextField.addActionListener(
76
77 new ActionListener() // anonymous inner class
78 {
79 // event handler called when enter is pressed in
80 // celsiusJTextField
81 public void actionPerformed( ActionEvent event )
82 {
83 celsiusJTextFieldActionPerformed( event );
84 }
85
86 } // end anonymous inner class
87
88 ); // end call to addActionListener
89
90 // set properties of application's window
91 setTitle( "Temperature Converter" ); // set title-bar string
92 setSize( 306, 100 ); // set window size
93 setVisible( true ); // show window
94
95 } // end method createUserInterface
96
97
98
97 // converts degrees to Celsius
98 private void fahrenheitJTextFieldActionPerformed(
99 ActionEvent event )
100 {
101
102 // get user's input
103 double degreesFahrenheit = Double.parseDouble(
104 fahrenheitJTextField.getText() );
105 // convert Fahrenheit to Celsius
106 double degreesCelsius =
107 ( degreesFahrenheit - 32.0 ) * 5.0 / 9.0;
108
109 // display the converted temperature
110 celsiusJTextField.setText(
111 temperature.format( degreesCelsius ) );
112
113 // end method fahrenheitJTextFieldActionPerformed
114
115 // converts degrees to Fahrenheit
116 private void celsiusJTextFieldActionPerformed( ActionEvent event )
117
118 // get user's input
119 double degreesCelsius = Double.parseDouble(
120 celsiusJTextField.getText() );
121
122 // convert Celsius to Fahrenheit
123 double degreesFahrenheit = 9.0 / 5.0 * degreesCelsius + 32.0;
124
125 // display the converted temperature
126 fahrenheitJTextField.setText(
127 temperature.format( degreesFahrenheit ) );
128
129 // end method celsiusJTextFieldActionPerformed
130
131 // main method
132 public static void main( String[] args )
133 {
134 TemperatureConversion application =
135 new TemperatureConversion();
136 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
137
138 // end method main
139
140 } // end class TemperatureConversion
```

Computer Science & Information Technology

You might also like to view...

An applet has no constructor or main() method.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

You can select text and then apply a numbered ____ using buttons on the HTML Property inspector.

a. font family b. Unordered List c. Ordered List d. Definition List

Computer Science & Information Technology