import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.applet.*; public class Temperature2 extends JApplet implements ActionListener { private JTextField fahrenheitField, celsiusField; private JLabel messageLabel; private JButton fahrButton, celsButton, resetButton; private double Celsius; private double Fahrenheit; public void init() { Container contentPane = getContentPane(); JLabel fahrenheitLabel, celsiusLabel, instructionsLabel; JPanel fahrenheitPanel, celsiusPanel, resetPanel; fahrenheitPanel = new JPanel(); celsiusPanel = new JPanel(); resetPanel= new JPanel(); instructionsLabel = new JLabel(" Temperature Converter Chart"); fahrButton = new JButton("convert to fahrenheit"); fahrButton.addActionListener(this); fahrenheitPanel.add(fahrButton); celsButton = new JButton("convert to celsius"); celsButton.addActionListener(this); celsiusPanel.add(celsButton); resetButton = new JButton("Reset"); resetButton.addActionListener(this); resetPanel.add(resetButton); fahrenheitLabel = new JLabel("Temperature in F:"); fahrenheitField = new JTextField(9); fahrenheitPanel.add(fahrenheitLabel); fahrenheitPanel.add(fahrenheitField); celsiusLabel = new JLabel("Temperature in C:"); celsiusField = new JTextField(9); celsiusPanel.add(celsiusLabel); celsiusPanel.add(celsiusField); contentPane.setLayout(new GridLayout(6, 1)); contentPane.add(instructionsLabel); contentPane.add(fahrenheitPanel); contentPane.add(celsiusPanel); contentPane.add(fahrButton); contentPane.add(celsButton); contentPane.add(resetButton); } public void actionPerformed(ActionEvent e) { if (e.getActionCommand().equals("convert to celsius")) { Celsius = 0; Fahrenheit = 0; Celsius = ((5.0 / 9.0) * (Fahrenheit - 30)); // double Celsius = Double.parseDouble(celsiusField.getText()); celsiusField.setText(Double.toString(Celsius)); System.out.println("Temperature in Celcius is:" + Celsius); } else if (e.getActionCommand().equals("Reset")) { Celsius = 0; Fahrenheit = 0; celsiusField.setText("0.0"); fahrenheitField.setText("0.0"); } else if (e.getActionCommand().equals("convert to fahrenheit")) { Celsius = 0; Fahrenheit = 0; Fahrenheit = (Celsius * (9.0 / 5.0) + 32); /** double Celsius = Double.parseDouble(fahrenheitField.getText()); */ fahrenheitField.setText(Double.toString(Fahrenheit)); System.out.println("Temperature in Fahrenheit is:" + Fahrenheit); } else celsiusField.setText("Error in convertor code."); } }