0

I want to convert temperature from Celsius to Fahrenheit and vice-versa. Conversion should happen if enter is pressed and convert button is clicked. Please let me know what more should I add to this code.

package converttemp2;

import java.awt.*;
import java.awt.event.*;

public class Converttemp2 implements ActionListener {

    Frame fr;
    Label cel,fah;
    TextField celcius,farenheit;
    Button b;

    Converttemp2() {
        fr=new Frame("Temperature Conversion");
        fr.setSize(500,300);
        celcius=new TextField();
        fr.add(celcius);
        farenheit=new TextField();
        fr.add(farenheit);
        cel=new Label("Celsius :");
        fr.add(cel);
        fah=new Label("Farenheit :");
        fr.add(fah);
        b=new Button("Convert");
        fr.add(b);
        cel.setBounds(100,40,100,50);
        fah.setBounds(100, 120, 100, 50);
        celcius.setBounds(200, 40, 100, 50);
        farenheit.setBounds(200,120,100,50);
        b.setBounds(150,200,80,50);
        b.addActionListener(this);
        farenheit.addActionListener(this);
        celcius.addActionListener(this);

        fr.setLayout(null);
        fr.setVisible(true);

        fr.addWindowListener(new WindowClose());
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource()==b) {
            String faren=farenheit.getText();
            Double fh=Double.parseDouble(faren);
            Double ct=(fh-32.0)*5.0/9.0;
            celcius.setText(ct+" ");
        }

        if (e.getSource()==farenheit) {
            String faren=farenheit.getText();
            Double fh=Double.parseDouble(faren);
            Double ct=(fh-32.0)*5.0/9.0;
            celcius.setText(ct+" ");
        } else if (e.getSource()==celcius) {
            String celc=celcius.getText();
            Double cs=Double.parseDouble(celc);
            Double ft=((cs*9.0)/5.0)+32.0;
            farenheit.setText(ft+" ");
        }
    }

    class WindowClose extends WindowAdapter{
        public void windowClosing(WindowEvent e){
            System.exit(0);
        }
    }
    public static void main(String a[])
    {
        new Converttemp2();
    }
}
Kyle Falconer
  • 8,302
  • 6
  • 48
  • 68
Nikk
  • 1
  • 1
  • Welcome to Stack Overflow! Is there a particular issue you're having with the code? Are you getting an error message? – Kyle Falconer Feb 20 '17 at 18:00
  • You need to ascertain some information, in particular, which field actually has text, the source of the event is generally unimportant, the issue you will need to resolve is when to clear the fields, so both fields can't have text in them at the same time – MadProgrammer Feb 20 '17 at 21:06
  • 1) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT components in favor of Swing. 2) Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). 3) .. – Andrew Thompson Feb 21 '17 at 02:07
  • .. 3) *"how to use button for two textfields(?)"* I'd re[;ace the button with a label saying 'type a value and press enter to convert'. Add the listener to each text field. get the source of the event. use the value of whichever field is the source in the conversion calculation. Update the other field. – Andrew Thompson Feb 21 '17 at 02:09

0 Answers0