0
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class fahrenheitPanel extends JFrame
{
    {
    }
private JLabel JLCelsius, JLFahrenheit;
private JTextField JTFahrenheit,JTCelsius;
private JButton Convert;
private ConvertButtonHandler cHandler;

public fahrenheitPanel()
{//the constructor

    JLCelsius=new JLabel("fahrenheit",SwingConstants.LEFT);//creating the JLabels for celsius
    JLFahrenheit=new JLabel("Celsius",SwingConstants.LEFT);//creating the JLabels for fahrenheit

    JTFahrenheit=new JTextField("0");//crealting the textfield for fahrenheit
    JTCelsius=new JTextField ("0");//crealting the textfield for celsius



      Convert= new JButton("convert");
      cHandler=new ConvertButtonHandler();
      Convert.addActionListener(cHandler);

    Container pane=getContentPane();
    pane.setLayout(new GridLayout(1 ,4));

    pane.add(JLCelsius);
    pane.add(JTCelsius);
    pane.add(JLFahrenheit);
    pane.add(JTFahrenheit);
    pane.add(Convert);
//

    

    JTFahrenheit =new JTextField (15);
    JTFahrenheit.addActionListener(new TempListener());

    setPreferredSize (new Dimension(300,75));
    setBackground(Color.GREEN);
}

private class TempListener implements ActionListener
{
        public void actionPerformed(ActionEvent event)
{
    int fahrenheitTemp;
    int celsiusTemp;

    String text = JTFahrenheit.getText();


    fahrenheitTemp= Integer.parseInt (text);
    celsiusTemp=(fahrenheitTemp-32)*5/9;


    JTFahrenheit .setText(Integer.toString(celsiusTemp));

    if(celsiusTemp>30)
    {
        JLFahrenheit.setForeground(Color.BLACK);
        JLFahrenheit.setBackground(Color.RED);
        JLFahrenheit.setText("HOT");
    }
    else if( celsiusTemp>20 && celsiusTemp <=30)
    {
      JLFahrenheit.setForeground(Color.ORANGE);
     JLFahrenheit.setText("WARM");
    }
    else if (celsiusTemp >10 && celsiusTemp <=20)
    {
   JLFahrenheit.setBackground(Color.BLUE);
   JLFahrenheit.setText( "COOL");
    }
    else if(celsiusTemp >0 && celsiusTemp <=10)
    {
        JLFahrenheit.setBackground(Color.WHITE);
        JLFahrenheit.setText("CHILLY");
    }
    else if(celsiusTemp <=0)
    {
                JLFahrenheit.setBackground(Color.GRAY);
                JLFahrenheit.setText("COLD");
    }
}

}
public static void main (String[] args)
{

fahrenheitPanel theGUI = new fahrenheitPanel();
theGUI.setTitle("Degree's Converter");
theGUI.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
theGUI.pack();
theGUI.setVisible(true);
}
private class ConvertButtonHandler implements ActionListener
{

    public void actionPerformed(ActionEvent e)
    {
        double Celsius,fahrenheitTemp;
       
         fahrenheitTemp=Double.parseDouble(JTFahrenheit.getText());
         Celsius=Double.parseDouble(JTCelsius.getText());

        Celsius= (fahrenheitTemp - 32)*(5.0/9.0);
        fahrenheitTemp=9.0/5.0*Celsius+32;

       JLCelsius.setText(String.format("",+Celsius));
       JLFahrenheit.setText(""+fahrenheitTemp);
    }
}
}

The code is running but its unable to convert to desired results kindly advice.

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

2
Contributors
1
Reply
4
Views
7 Years
Discussion Span
Last Post by NormR1
0

YOU need to show the results and explain why its not what you want.

celsiusTemp=(fahrenheitTemp-32)*5/9;
Common problem here is integer arithmetic. Remember 19/10 = 1

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.