Write a Java GUI application to do temperature conversions between Celcius, Fahr
ID: 3656871 • Letter: W
Question
Write a Java GUI application to do temperature conversions between Celcius, Fahranheit, and Kelvin. The GUI display should look something like the following: <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /?>
Your program must meet the following requirements:
1. Do not use any of the GUI editing capabilities of Eclipse for this assignment. Do all the GUI layout work based on what you have learned in class in the last 2 weeks.
2. The GUI and event handling setup should be done in the constructor of your GUI class or in private methods called from the constructor.
3. The display must have a label and JTextField where the user inputs a value which must appear in the upper part of the frame as shown above.
4. There should be a set of 3 radio buttons which indicate the input scale of the value to be converted. The 3 input scale buttons must be vertically aligned on the left side of the display as shown above.
5. There should be a set of 3 radio buttons which indicate the output scale to be converted to. The 3 output scale buttons must be vertically aligned and appear on the right side of the display as shown above.
6. Event handling must be setup so that selection of any input or output button causes an event which triggers the event handling code to determine which of 9 possible conversions is needed.
7. Event handling must also respond to the user hitting the enter key in the input textfield! For this event, the event handling code must also determine which conversion is needed based on which buttons are selected.
8. Display the conversion result in an output text field or in a JLabel which appears in the bottom part of the display as shown above. Output the degree symbol and output scale information as shown above. You can output a degree symbol by putting the character value 176 into a formatted string.
9. If a radio button is clicked when there is nothing in the input textfield, the event handler must display the string
Explanation / Answer
// Temperature conversion program import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Convert extends JFrame { private JLabel label1, label2; private JTextField temperatureF; // constructer sets up GUI public Convert() { super( "Temperature converter" ); label1 = new JLabel( "Enter Fahrenheit temperature:" ); temperatureF = new JTextField( 10 ); // register anonymous action listener temperatureF.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { int temp = Integer.parseInt( temperatureF.getText() ); int celcius = ( int ) ( 5.0f / 9.0f * ( temp - 32 ) ); temperatureF.setText( String.valueOf( celcius ) ); } } ); label2 = new JLabel( "Temperature in Celcius is:" ); Container container = getContentPane(); container.setLayout( new BorderLayout() ); container.add( label1, BorderLayout.NORTH ); container.add( temperatureF, BorderLayout.CENTER ); container.add( label2, BorderLayout.SOUTH ); setSize( 225, 80 ); setVisible( true ); } // end Convert constructor public static void main ( String args[] ) { Convert app = new Convert(); app.setDefaultCloseOperation( EXIT_ON_CLOSE ); } } // end class Convert