//Reference P534 Deitel, H.M & Deitel, P.J. (2005), JAVATM HOW TO PROGRAM (SIXTH EDITION), Pearson International Edition, ISBN 0-13-129014-2. import java.awt.GridLayout; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JButton; import javax.swing.JLabel; import javax.swing.JTextField; import javax.swing.text.JTextComponent; import javax.swing.SwingConstants; // common constants used with Swing public class TempretureForm extends JFrame { private JButton ConvertToCelciusJButton; private JButton ConvertToFahrenheitJButton; private JButton exitJButton; private JLabel TitleLabel; public JLabel FahrenheitLabel; private JLabel CelciusLabel; private JLabel ConfirmationLabel; private JLabel CelciuisValueLabel; private JTextField UserInputTextField; private GridLayout gridLayout; public TempretureForm() { super ("This Application Aims to Convert from Fahrenheit to Celcius and Back"); gridLayout = new GridLayout(3,3,3,3); setLayout (gridLayout); //creating buttons and textfields UserInputTextField = new JTextField(""); add(UserInputTextField); TitleLabel = new JLabel("degrees fahrenheit"); add(TitleLabel); ConvertToCelciusJButton = new JButton("Convert to Celcius"); add(ConvertToCelciusJButton); FahrenheitLabel = new JLabel(""); add(FahrenheitLabel); CelciusLabel = new JLabel("Celcius"); add(CelciusLabel ); ConvertToFahrenheitJButton = new JButton("Convert To Fahrenheit"); add(ConvertToFahrenheitJButton); ConfirmationLabel = new JLabel(""); add(ConfirmationLabel); CelciuisValueLabel = new JLabel("From C to Fahr"); add(CelciuisValueLabel); exitJButton = new JButton("Exit"); add(exitJButton); CelciuisValueLabel.setVisible(false); FahrenheitLabel.setVisible(false); CelciuisValueLabel.setVisible(false); ConvertToFahrenheitJButton.setVisible(false); //setting handler to perform tasks based on Button pressed. ButtonHandler handler = new ButtonHandler(); ConvertToCelciusJButton.addActionListener(handler); ConvertToFahrenheitJButton.addActionListener(handler); exitJButton.addActionListener(handler); } private class ButtonHandler implements ActionListener { //Actions to be performed based on Button Pressed. public void actionPerformed (ActionEvent event) { //Getting User Input if (event.getActionCommand() == "Convert to Celcius") { String temptemp = UserInputTextField.getText(); double temp = Double.parseDouble(temptemp); String type = "C"; TempretureConversion myTempretureConversion = new TempretureConversion(); double tempreture = myTempretureConversion.TheTempretureConversion(temp, type); FahrenheitLabel.setVisible(true); ConvertToFahrenheitJButton.setVisible(true); FahrenheitLabel.setText(Double.toString(tempreture)); } // Short Address Button is clicked. else if (event.getActionCommand() == "Convert To Fahrenheit") { String temptemp = FahrenheitLabel.getText(); double temp = Double.parseDouble(temptemp); String type = "F"; TempretureConversion myTempretureConversion = new TempretureConversion(); double tempreture = myTempretureConversion.TheTempretureConversion(temp, type); //JOptionPane.showMessageDialog(null, tempreture + " in degrees Fahrenheit:"); CelciuisValueLabel.setVisible(true); ConfirmationLabel.setText(Double.toString(tempreture)); } //Exit Button is Clicked else if (event.getActionCommand() == "Exit") { //Closes window dispose (); } } } } //end of Class