I posted a very similar code 2 days ago. I now updated a few lines and will not
ID: 667833 • Letter: I
Question
I posted a very similar code 2 days ago. I now updated a few lines and will not work. Want to learn from my mistakes please help: The code is as follows:
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class test1 extends JFrame{
private JLabel celsius;
private JLabel farenheit;
private JLabel kelvin;
private JTextField celsiusText;
private JTextField fahrenheitText;
private JTextField kelvinText;
private CelsHandler celsiusHandler;
private FahrHandler fahrenheitHandler;
private kelHandler kelvinHandler;
private static final double FTOC= 5.0/9.0;
private static final double CTOF = 9.0/5.0;
public test1(){
super ("Temperature Conversion");
setLayout (new FlowLayout ());
celsius = new JLabel ("Temp in Celsius", SwingConstants.RIGHT);
farenheit = new JLabel ("Temp in Fahrenheit",SwingConstants.RIGHT);
kelvin = new JLabel("Temp in Kelvin", SwingConstants.RIGHT);
celsiusText = new JTextField(10);
fahrenheitText = new JTextField(10);
kelvinText = new JTextField(10);
add(celsius);
add (celsiusText);
add (farenheit);
add(fahrenheitText);
add (kelvin);
add (kelvinText);
celsiusHandler = new CelsHandler();
fahrenheitHandler = new fahrHandler();
kelvinHandler = new kelHandler();
celsiusText.addActionListener(celsiusHandler);
fahrenheitText.addActionListener(fahrenheitHandler);
kelvinText.addActionListener(kelvinHandler);
setSize(1000,100);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CelsHandler implements ActionListener {
public void actionPerformed(ActionEvent e){
double celsius, fahrenheit, kelvin;
celsius = Double.parseDouble(celsiusText.getText());
fahrenheit= celsius *CTOF +32;
kelvin = celsius+273.15;
fahrenheitText.setText(String.format(".2f", fahrenheit));
kelvinText.setText(String.format(".2f", kelvin));
}
private class kelHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
double kelvin, fahrenheit, celsius;
kelvin = Double.parseDouble(kelvinText.getText());
fahrenheit = (fahrenheit +459.67)*FTOC;
celsius = kelvin -273.15;
fahrenheitText.setText(String.format(".2f", fahrenheit));
celsiusText.setText(String.format(".2f",celsius));
}
}
private class fahrhandler implements ActionListener{
public void actionPerformed(ActionEvent e){
double celsius, faherheit,kelvin;
faherheit = Double.parseDouble(fahrenheitText.getText());
celsius = (faherheit- 32)*FTOC;
kelvin = celsius +273.15;
celsiusText.setText(String.format(".2f", celsius));
kelvinText.setText(String.format(".2f", kelvin));
}
}
public void main(String[] args)
{
test1 tempConverion = new test1();
tempConverion.setLocationRelativeTo(null);
}
}
}
end
Explanation / Answer
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class test1 extends JFrame
{
private JLabel celsius;
private JLabel farenheit;
private JLabel kelvin;
private JTextField celsiusText;
private JTextField fahrenheitText;
private JTextField kelvinText;
private CelsHandler celsiusHandler;
private FahrHandler fahrenheitHandler;
private kelHandler kelvinHandler;
private static final double FTOC= 5.0/9.0;
private static final double CTOF = 9.0/5.0;
public test1()
{
super ("Temperature Conversion");
setLayout (new FlowLayout ());
celsius = new JLabel ("Temp in Celsius", SwingConstants.RIGHT);
farenheit = new JLabel ("Temp in Fahrenheit",SwingConstants.RIGHT);
kelvin = new JLabel("Temp in Kelvin", SwingConstants.RIGHT);
celsiusText = new JTextField(10);
fahrenheitText = new JTextField(10);
kelvinText = new JTextField(10);
add(celsius);
add (celsiusText);
add (farenheit);
add(fahrenheitText);
add (kelvin);
add (kelvinText);
celsiusHandler = new CelsHandler();
fahrenheitHandler = new fahrHandler();
kelvinHandler = new kelHandler();
celsiusText.addActionListener(celsiusHandler);
fahrenheitText.addActionListener(fahrenheitHandler);
kelvinText.addActionListener(kelvinHandler);
setSize(1000,100);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
private class CelsHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double celsius, fahrenheit, kelvin;
celsius = Double.parseDouble(celsiusText.getText());
fahrenheit= celsius *CTOF +32;
kelvin = celsius+273.15;
fahrenheitText.setText(String.format(".2f", fahrenheit));
kelvinText.setText(String.format(".2f", kelvin));
}
}
private class kelHandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double kelvin, fahrenheit, celsius;
kelvin = Double.parseDouble(kelvinText.getText());
fahrenheit = (fahrenheit +459.67)*FTOC;
celsius = kelvin -273.15;
fahrenheitText.setText(String.format(".2f", fahrenheit));
celsiusText.setText(String.format(".2f",celsius));
}
}
private class fahrhandler implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
double celsius, faherheit,kelvin;
faherheit = Double.parseDouble(fahrenheitText.getText());
celsius = (faherheit- 32)*FTOC;
kelvin = celsius +273.15;
celsiusText.setText(String.format(".2f", celsius));
kelvinText.setText(String.format(".2f", kelvin));
}
}
public void main(String[] args)
{
test1 tempConverion = new test1();
tempConverion.setLocationRelativeTo(null);
}
}
Above is the updated code, your code consists of brase brakets mis-match.