Write a temperature conversion program. The GUI and event handling setup should
ID: 3626537 • Letter: W
Question
Write a temperature conversion program. The GUI and event handling setup should be done in the constructor of the class that implements the GUI. Your GUI class should contain a JFrame member variable. Do not use any of the GUI editing capabilities of Eclipse for this assignment. The temperature conversion application should have a JLabel and JTextField where the user inputs a value. There should be a set of 6 JButtons on the display representing the following temperature conversions:F-to-C F: Fahrenheit
C-to-F C: Celcius
K-to-C K: Kelvin
C-to-K
K-to-F
F-to-K
Event handling should be setup so that clicking on any one of the 6 buttons generates an event which your program handles. You must use an inner class to set up your event handling. You can display the result in an output text field or in a JLabel. Your program should accurately convert from Fahrenheit, Celcius, Kelvin to Fahrenheit, Celcius, Kelvin. NOTE: Only the selected conversion is displayed in the output area!!! When the conversion selection changes, the output area should change to show only the new result. The output should display 3 digits after the decimal point.
Explanation / Answer
please rate - thanks
hope this helps
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.JTextComponent;
import java.text.DecimalFormat;
public class ConvertTemp extends JFrame
implements ActionListener
{
private JButton button, button2,button3,button4,button5,button6;
private JPanel panel;
private JFrame frame;
private JTextField Text;
private JLabel label;
private JTextField infield = new JTextField(10);
private JTextField outfield = new JTextField(10);
private JLabel inlabel =new JLabel("value to convert:");
private JLabel outlabel =new JLabel("converted:");
DecimalFormat two=new DecimalFormat("0.000");
public ConvertTemp()
{
Container pane = getContentPane();
pane.setLayout(new GridLayout(8,2));
button = new JButton("F to C");
pane.add(button); //create the button in the window
button.addActionListener((ActionListener) this); //create the action when the buttion is press
button4 = new JButton("C to K");
pane.add(button4); //create the button in the window
button4.addActionListener((ActionListener) this); //create the action when the buttion is press
//create the Celsius converted to Fahrenheit button
button2 = new JButton("C to F");
pane.add(button2); //create the button in the window
button2.addActionListener((ActionListener) this); //create the action when the buttion is press
button3 = new JButton("K to C");
pane.add(button3); //create the button in the window
button.addActionListener((ActionListener) this); //create the action when the buttion is press
//create the Celsius converted to Fahrenheit button
button5 = new JButton("C to F");
pane.add(button5); //create the button in the window
button5.addActionListener((ActionListener) this); //create the action when the buttion is press
//create the Celsius converted to Fahrenheit button
button6 = new JButton("C to F");
pane.add(button6); //create the button in the window
button6.addActionListener((ActionListener) this); //create the action when the buttion is press
pane.add(inlabel);
pane.add(infield);
pane.add(outlabel);
pane.add(outfield);
outfield.setEditable(false);
setSize(400, 600); //size of the shape(width and height
setVisible(true); //enable to view the shape
setTitle("Temperature Converter"); //the title of the box
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //close window when user exit
panel = new JPanel(new GridLayout());
}
public void actionPerformed(ActionEvent i)
{ int temp;
double newtemp=0;
String inputString;
inputString=infield.getText();
temp=Integer.parseInt(inputString);
if(i.getSource()==button)
{newtemp=(temp - 32) *(5 / 9.);
}
else if(i.getSource()==button2)
{newtemp= (temp * 1.8) + 32;
}
else if(i.getSource()==button3)
{newtemp=temp-273;
}
else if(i.getSource()==button4)
{newtemp=temp+273.15;
}
else if(i.getSource()==button5)
{newtemp=((temp - 273) * 1.8 ) + 32; }
else
{newtemp= (5/9 * (temp - 32) + 273 );
}
outfield.setText(" "+ two.format(newtemp));
}
public static void main(String[] args)
{
ConvertTemp frame = new ConvertTemp();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.