/* * ================================================================ * TempConversion.java : Graphical version of Celsius to Fahrenheit * conversion program. * * Written By : David Chancogne September 1997 * ================================================================ */ import java.lang.Math; import java.util.*; import java.io.*; import java.awt.*; import java.awt.event.*; class TempConversion extends Frame { TextField CelText; TextField FahrenText; Button CelButton; Button FahrenButton; Button QuitButton; // The main method just creates an instance of the class. public static void main( String args[] ) { System.out.println("Celsius to Fahrenheit Converter Program"); TempConversion p = new TempConversion(); } // The constructor creates the window. public TempConversion() { setTitle("Temperatures Converter"); CelButton = new Button("Celsius"); FahrenButton = new Button("Fahrenheit"); QuitButton = new Button("Quit"); CelText = new TextField(7); FahrenText = new TextField(7); // Create 3x2 grid of buttons and text fields. setLayout(new GridLayout(3,2)); add(CelButton); add(CelText); add(FahrenButton); add(FahrenText); add(QuitButton); // Add listeners for the various components. QuitButton.addMouseListener( new QuitListener()); CelText.addKeyListener( new CelListener(CelText, FahrenText) ); FahrenText.addKeyListener( new FahrenListener(CelText, FahrenText) ); // Set the window size and display it. setSize(450,150); show(); } // The temperature conversion methods. public static float CelsiusToFahrenheit( float fCel ) { return (float)(9.0/5.0 * fCel + 32.0); } public static float FahrenheitToCelsius( float fFahrn ) { return (float)(5.0/9.0 * (fFahrn - 32.0) ); } } /* * ========================================================= * QuitListener -- this class extends the MouseAdapter class * and is attached to the "Quit" button. * ========================================================= */ class QuitListener extends MouseAdapter { public void mouseReleased(MouseEvent e) { System.out.println("Thank you !"); System.exit(0); } } /* * ================================================================= * CelListener and FahrenListener -- these classes extend KeyAdapter * and are listening for input from the TextFields. * ================================================================= */ class CelListener extends KeyAdapter { TextField CelText; TextField FahrenText; public CelListener( TextField c, TextField f ) { CelText = c; FahrenText = f; } public void keyReleased(KeyEvent e) { // Keyboard event is release of the 'return' key if ( e.getKeyCode() == KeyEvent.VK_ENTER ) { float fTemp = 0 ; // The methods are 'static' so we can call // them without an instance try { fTemp = TempConversion.CelsiusToFahrenheit( Float.valueOf(CelText.getText()).floatValue() ); } catch (Exception ex) { System.out.println("Malformated number !"); } FahrenText.setText(Float.toString( fTemp )); } } } class FahrenListener extends KeyAdapter { TextField CelText; TextField FahrenText; public FahrenListener( TextField c, TextField f ) { CelText = c; FahrenText = f; } public void keyReleased(KeyEvent e) { // Keyboard event is release of the 'return' key if ( e.getKeyCode() == KeyEvent.VK_ENTER ) { float fTemp = 0; // The method are 'static' so we can call // them without an instance try { fTemp = TempConversion.FahrenheitToCelsius( Float.valueOf(FahrenText.getText()).floatValue() ); } catch (Exception ex) { System.out.println("Malformated number !"); } CelText.setText(Float.toString( fTemp )); } } }