import javax.swing.*; import java.awt.GridLayout; // *** Make sure you have all the import statements that you need. /** * Lab #5 * CS 2334, Section SECTION NUMBER GOES HERE * DATE GOES HERE *

* This implements a program that will convert a temperature in Fahrenheit * to Celsius, Kelvin, and Fahrenheit and displays the result. *

* @author Group GROUP NUMBER GOES HERE * @version 1.0 */ public class Converter { /** The data model for the GUI. */ Temperature dataModel; /** Main window of the program. */ JFrame theFrame; /** "Convert" button */ JButton convertButton; /** "Close" button */ JButton closeButton; /** Textfield for use input. */ JTextField inputField; /** Label used to output the Temperature in Celsius. */ JLabel celsiusOutput; /** Label used to output the Temperature in Fahrenheit. */ JLabel fahrenheitOutput; /** Label used to output the Temperature in Kelvin. */ JLabel kelvinOutput; /** * This constructor creates the GUI, associates all * event handlers with components, and displays the GUI. *

* @param data The date model for the GUI. */ public Converter( Temperature data ) { // Save a reference to the data model. dataModel = data; // Create the buttons for the program. convertButton = new JButton( "Convert" ); closeButton = new JButton( "Close" ); // Create a label and textfield for use input. JLabel inputLabel = new JLabel( "Temperature (F): " ); inputField = new JTextField( 10 ); // Create labels to hold the output. /* *** When the user clicks on convert you should set the text *** of each of the labels below named Output. *** Look at the setText method of JLabel. */ celsiusOutput = new JLabel( "" ); JLabel celsiusLabel = new JLabel( "Fahrenheit: " ); fahrenheitOutput = new JLabel( "" ); JLabel fahrenheitLabel = new JLabel( "Celsius: " ); kelvinOutput = new JLabel( "" ); JLabel kelvinLabel = new JLabel( "Kelvin: " ); /************************************************************************ ************************************************************************ *** The program must handle three different events. *** 1. User clicks on the convert button. --> Convert the value in inputField to a float and pass it to the data model, setting the temperature of the data model. Then, call the accessor methods of Temperature to read it in the three different metrics provided (Celsius, Fahrenheit, and Kelvin) and update the appropriate labels (see above) to display the converted temperature. *** 2. User presses enter in the inputField textfield. --> Perform the same action as #1. *** 3. User clicks on the close button. --> Terminate the program with a call to the exit() method of the system class. ************************************************************************* ************************************************************************/ /*** *** Add an action listener for the components that *** actions can occur on. ***/ // Create the JFrame for the window and set it's default close operation. theFrame = new JFrame( "Temperature Conversion" ); theFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE ); // Change the layout manager of the content pane. theFrame.getContentPane().setLayout( new GridLayout( 0, 2 ) ); // Add all components to the content pane. theFrame.getContentPane().add( inputLabel ); theFrame.getContentPane().add( inputField ); theFrame.getContentPane().add( convertButton ); theFrame.getContentPane().add( closeButton ); theFrame.getContentPane().add( celsiusLabel ); theFrame.getContentPane().add( celsiusOutput ); theFrame.getContentPane().add( fahrenheitLabel ); theFrame.getContentPane().add( fahrenheitOutput ); theFrame.getContentPane().add( kelvinLabel ); theFrame.getContentPane().add( kelvinOutput ); // Pack the window and make it visible. theFrame.pack(); theFrame.setVisible( true ); } /** * This is the main method for this program. * This method simply creates an instance of Converter and Temperature. * Temperature is used as the data model and Converter provides * a user interface to the data model. The call to the constructor * of GUI will create and display the GUI for the program. *

* @param args Contains the command line arguments. */ public static void main( String args[] ) { Temperature temp = new Temperature(); Converter convert = new Converter( temp ); } }