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.