/** * Class TempConvert * * Write a temperature conversion program that converts * from Fahrenheit to Celsius and back again. * The Fahrenheit temperature should be entered from the keyboard * (via a JTextField). * A JLabel should be used to display the converted temperature. * Use the following formula for the conversion: * Celsius = 5/9 x ( Fahrenheit -32) * * @author Ian Johnson * @version 19/04/2007 */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class TempConvert extends JFrame implements ActionListener { // Content pane and input field private JPanel jContentPane = null; private JTextField inputTemp = null; // Choice buttons private JButton butCelsius = null; private JButton butFahrenheit = null; // Label for user interaction final JLabel inputTxt = new JLabel("Input the temperature to be converted"); final JLabel outputInTxt = new JLabel("To which temperature scale do you wish to convert?"); final JLabel tempResult = new JLabel(""); double tempInput; // Constructor public TempConvert() { initialize(); } // Initialize the JFrame private void initialize() { this.setSize(305, 250); this.setTitle("Temperature Converter"); this.setLayout(new FlowLayout()); // Initialise entry screen this.add(inputTxt); inputTemp = new JTextField(6); this.add(inputTemp, null); this.add(outputInTxt); // Set for choice buttons butCelsius = new JButton(" Celsius "); butCelsius.addActionListener(this); this.add(butCelsius, null); butFahrenheit = new JButton(" Fahrenheit "); butFahrenheit.addActionListener(this); this.add(butFahrenheit, null); // Will use label to display input and results this.add(tempResult); } // end method initialize // Method clearInput -blank the input string for a retry public void clearInput() { inputTemp.setText(""); } // end method clearInput // Method toCelsius -Convert users input to celsius private void toCelsius() { // Convert & message input and result to user double inCelsius = (( tempInput -32)*5)/9; String scaleInfo = String.format(" %.1f degrees Fahrenheit is \n %.1f degrees Celsius", tempInput, inCelsius ); // Display message as label tempResult.setText(scaleInfo); } // end method toCelsius // Method toFahrenheit -Convert users input to fahrenheit private void toFahrenheit() { // Convert & message input and result to user double inFahrenheit = ((tempInput * 9)/5) + 32; String scaleInfo = String.format(" %.1f degrees Celsius is %.1f degrees Fahrenheit", tempInput, inFahrenheit ); // Display message as label tempResult.setText(scaleInfo); } // end method toFahrenheit //Event handler to catch choice public void actionPerformed(java.awt.event.ActionEvent e) { boolean dataOK; // If entered data ok // - message user, sort & display end result if (e.getSource() == butCelsius || e.getSource() == butFahrenheit) { // First validate the user has input integers dataOK = validateEntry(); // Convert and message the answer to the user if (e.getSource() == butCelsius && dataOK ) toCelsius(); if (e.getSource() == butFahrenheit && dataOK ) toFahrenheit(); // clear the input fields if (dataOK) clearInput(); } } // end Event handler // Load tempResult array verifying the user input private boolean validateEntry() { try { tempInput = new Double(inputTemp.getText()); } // Catch any exception and display error catch (Exception e) { JOptionPane.showMessageDialog(null, "Error " + e.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); return false; } return true; } // end method validateInput }