I need help with my temperature conversion code..It seems that I'm am having a problme in my action event section... I cannot figure out the problem.. Well the main problem is when i put in a number in the fahrenheit field or textbox and press the convert to celsius button the answer is always -16.666 and if i do the same and put a number in the celisus field or textbox the output for fahrenheit is always 32...
Heres the code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Temperature2 extends JApplet implements ActionListener
{
private JTextField fahrenheitField, celsiusField;
private JLabel messageLabel;
private JButton fahrButton, celsButton, resetButton;
private double Celsius;
private double Fahrenheit;
public void init()
{
Container contentPane = getContentPane();
JLabel fahrenheitLabel, celsiusLabel, instructionsLabel;
JPanel fahrenheitPanel, celsiusPanel, resetPanel;
fahrenheitPanel = new JPanel();
celsiusPanel = new JPanel();
resetPanel= new JPanel();
instructionsLabel = new JLabel(" Temperature Converter Chart");
fahrButton = new JButton("convert to fahrenheit");
fahrButton.addActionListener(this);
fahrenheitPanel.add(fahrButton);
celsButton = new JButton("convert to celsius");
celsButton.addActionListener(this);
celsiusPanel.add(celsButton);
resetButton = new JButton("Reset");
resetButton.addActionListener(this);
resetPanel.add(resetButton);
fahrenheitLabel = new JLabel("Temperature in F:");
fahrenheitField = new JTextField(9);
fahrenheitPanel.add(fahrenheitLabel);
fahrenheitPanel.add(fahrenheitField);
celsiusLabel = new JLabel("Temperature in C:");
celsiusField = new JTextField(9);
celsiusPanel.add(celsiusLabel);
celsiusPanel.add(celsiusField);
contentPane.setLayout(new GridLayout(6, 1));
contentPane.add(instructionsLabel);
contentPane.add(fahrenheitPanel);
contentPane.add(celsiusPanel);
contentPane.add(fahrButton);
contentPane.add(celsButton);
contentPane.add(resetButton);
}
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand().equals("convert to celsius"))
{
Celsius = 0;
Fahrenheit = 0;
Celsius = ((5.0 / 9.0) * (Fahrenheit - 30));
// double Celsius = Double.parseDouble(celsiusField.getText());
celsiusField.setText(Double.toString(Celsius));
System.out.println("Temperature in Celcius is:" + Celsius);
}
else if (e.getActionCommand().equals("Reset"))
{
Celsius = 0;
Fahrenheit = 0;
celsiusField.setText("0.0");
fahrenheitField.setText("0.0");
}
else if (e.getActionCommand().equals("convert to fahrenheit"))
{
Celsius = 0;
Fahrenheit = 0;
Fahrenheit = (Celsius * (9.0 / 5.0) + 32);
/**
double Celsius = Double.parseDouble(fahrenheitField.getText());
*/
fahrenheitField.setText(Double.toString(Fahrenheit));
System.out.println("Temperature in Fahrenheit is:" + Fahrenheit);
}
else
celsiusField.setText("Error in convertor code.");
}
}
This post has been edited by macosxnerd101: 11 December 2010 - 04:40 PM
Reason for edit:: Please use code tags

New Topic/Question
Reply




MultiQuote




|