11. Write a GUI program that does temperature conversions between Kelvin, Fahren
ID: 3649909 • Letter: 1
Question
11. Write a GUI program that does temperature conversions between Kelvin, Fahrenheit, and Celcius. Your GUI application must inherit from the JFrame class. The GUI and event handling setup should be done in the constructor. Do not use any of the GUI editing capabilities of Eclipse for this assignment. The temperature conversion application should have a label and JTextField where the user inputs a value which must appear in the upper part of the frame. There should be a set of 3 radio buttons which indicate the input scale of the value to be converted. There should also be a set of 3 radio buttons which indicate the output scale to be converted to. The 3 input scale buttons must appear vertically aligned (ie. use a JPanel) on the left side of the display, and the 3 output scale buttons must appear vertically aligned (ie. use another JPanel) and appear on the right side of the display. Event handling should be setup so that selection of any input or output radio button causes an event which triggers the event handling code to determine which of 9 possible conversions is needed. You can display the result in an output text field or in a JLabel which appears in the bottom part of the display. Make sure that you include exception handling in your event handler in case the user input cannot be converted to a number! 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. HINT: Use the ItemListener interface and use the is Selected method on the radio buttons to find out which buttons are turned on!Explanation / Answer
001 import java.awt.*; 002 import java.awt.event.*; 003 import javax.swing.*; 004 import java.text.DecimalFormat; 005 006 @SuppressWarnings("serial") 007 public class ConvertTemp extends JFrame 008 implements ActionListener { 009 private JButton button1, button2, button3, button4, button5, button6; 010 @SuppressWarnings("unused") 011 private JPanel panel; 012 @SuppressWarnings("unused") 013 private JFrame frame; 014 @SuppressWarnings("unused") 015 private JTextField Text; 016 @SuppressWarnings("unused") 017 private JLabel label; 018 private JTextField infield = new JTextField(10); 019 private JTextField outfield = new JTextField(10); 020 private JLabel inlabel = new JLabel("Temperature Value:"); 021 private JLabel outlabel = new JLabel("Converted Temperature Value:"); 022 023 DecimalFormat three = new DecimalFormat("0.000");//sets up the 3 Decimal places for use on the output 024 025 public static void main(String[] args) { ConvertTemp frame = new ConvertTemp(); 027 frame.createFrame(); 028 frame.createButton1(); 029 frame.createButton2(); 030 frame.createButton3(); 031 frame.createButton4(); 032 frame.createButton5(); 033 frame.createButton6(); 034 frame.addLabels(); 035 } 036 037 public void createFrame() { 038 Container window = getContentPane(); 039 window.setLayout(new FlowLayout() ); //using the FlowLayout manager 040 setSize(400, 200); //setting the size of the initial box 041 setVisible(true); //allows for manual size changing of the box 042 setTitle("Temperature Converter"); 043 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 044 panel = new JPanel(new GridLayout()); 045 add(new JLabel("Temperature Conversions" + " C: Celsius" + " F: Farhrenheit"+ " K: Kelvin")); 046 } 047 048 public void createButton1() { 049 //this is to create the Fahrenheit to Celsius button 050 button1 = new JButton("F to C"); 051 add(button1); 052 button1.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed 053 } 054 055 public void createButton2() { 056 //this is to create the Fahrenheit to Kelvin button 057 button2 = new JButton("F to K"); 058 add(button2); 059 button2.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed 060 } 061 062 public void createButton3() { 063 //this is to create the Celsius to Kelvin button 064 button3 = new JButton("C to K"); 065 add(button3); 066 button3.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed 067 } 068 069 public void createButton4() { 070 //this is to create the Celsius to Fahrenheit button 071 button4 = new JButton("C to F"); 072 add(button4); 073 button4.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed 074 } 075 076 public void createButton5() { 077 //this is to create the Kelvin to Celsius button 078 button5 = new JButton("K to C"); 079 add(button5); 080 button5.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed 081 } 082 083 public void createButton6() { 084 //this is to create the Kelvin to Fahrenheit button 085 button6 = new JButton("K to F"); 086 add(button6); 087 button6.addActionListener((ActionListener) this);//this allows an action to be performed when the button is pressed 088 } 089 090 public void addLabels() { 091 add(inlabel); 092 add(infield); 093 add(outlabel); 094 add(outfield); 095 outfield.setEditable(false);//removes ability to enter a value in the output field 096 } 097 098 public void actionPerformed(ActionEvent i) { 099 int temp; 100 double newtemp = 0; 101 String inputString; 102 inputString = infield.getText();//pulls the number that was entered in the input field 103 temp = Integer.parseInt(inputString);// makes a new variable to handle the input 104 //tells the output how to calculate the input based on the button that was pressed 105 if(i.getSource() == button1) { 106 newtemp = ((.55555) * (temp - 32));//Fahrenheit to Celsius equation 107 } 108 else if(i.getSource() == button2) { 109 newtemp = ((.55555) * (temp - 32) + 273);//Fahrenheit to Kelvin equation 110 } 111 else if(i.getSource() == button3) { 112 newtemp = (temp + 273);//Celsius to Kelvin equation 113 } 114 else if(i.getSource() == button4) { 115 newtemp = (((1.8) * temp) + 32);//Celsius to Fahrenheit equation 116 } 117 else if(i.getSource() == button5) { 118 newtemp = (temp - 273);//Kelvin to Celsius equation 119 } 120 else if(i.getSource() == button6){ 121 newtemp = (((temp - 273) * (1.8)) + 32);//Kelvin to Fahrenheit equation 122 } 123 outfield.setText(" "+ three.format(newtemp)); 124 } 125 }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.