// Temperature conversion program based on 5C, page 247 in Mercer Text // By Dan Hyde, January 24, 2007 // *** changes from Java Application in *** import javax.swing.*; // need for graphics import java.awt.*; // need for graphics import java.awt.event.*; // need for listeners import java.text.DecimalFormat; // need for DecimalFormat class import javax.swing.JApplet; // *** added import for JApplet *** // *** now extends JApplet *** public class TempConvert extends JApplet{ //instance variables JLabel fahrLabel; JTextField fahrTextField; JLabel celLabel; JTextField celTextField; // create a DecimalFormat object for printing double values DecimalFormat onePlace = new DecimalFormat("0.0"); // *** changed constructor to void int() *** public void init() { // *** removed initialization for JFrame *** GridLayout grid2By2 = new GridLayout(2, 2, 4, 4); // set Layout of container to be grid2By2. setLayout(grid2By2); // Initialize graphical components: JLabel and JTextField celLabel = new JLabel("Celcius:", SwingConstants.RIGHT); celTextField = new JTextField(""); fahrLabel = new JLabel("Fahrenheit:", SwingConstants.RIGHT); fahrTextField = new JTextField(""); // Add graphical components to container // in the order for the Layout // *** container now Applet and not contentPane *** add(celLabel); add(celTextField); add(fahrLabel); add(fahrTextField); // Create and register listeners on the two JTextFields MyListener1 listenerCelText = new MyListener1(); celTextField.addActionListener(listenerCelText); MyListener2 listenerFahrText = new MyListener2(); fahrTextField.addActionListener(listenerFahrText); } /// inner class private class MyListener1 implements ActionListener { public MyListener1() { } public void actionPerformed(ActionEvent e1) { double cel; String fahrString; cel = Double.parseDouble(celTextField.getText()); fahrString = "" + onePlace.format((cel * 9.0 /5.0 + 32.0)); fahrTextField.setText(fahrString); } } /// end of inner class /// inner class private class MyListener2 implements ActionListener { public MyListener2() { } public void actionPerformed(ActionEvent e1) { double fahr; String celString; fahr = Double.parseDouble(fahrTextField.getText()); celString = "" + onePlace.format((5.0 / 9.0 * (fahr - 32.0))); celTextField.setText(celString); } } /// end of inner class // **** removed main () **** }