Temperature Converter GUI

Arman Majumder 0 Tallied Votes 723 Views Share

A temperature converter GUI. Converts from Celsius to Fahrenheit and vise versa. Tests JLabels, JButtons and JTextFields.

*Two separate programs, Converter and Thermometer (Thermometer Class)*

// Arman Majumder
//Temperature Converter GUI
// Requires Thermometer Class <Thermometer.java>  Included in bottom

   import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;

    public class Converter extends JFrame
   {
      private Thermometer thermo = new Thermometer();
      private JLabel fahrLabel = new JLabel("Degrees Fahrenheit");
      private JLabel celsiusLabel = new JLabel("Degrees Celsius");
      private JTextField fahrField = new JTextField("32.0");
      private JTextField celsiusField = new JTextField("0.0");
      private JButton fahrButton = new JButton("Convert >>>");
      private JButton celsiusButton = new JButton("<<< Convert");
      
       public Converter()
      {
         JPanel dataPanel = new JPanel(new GridLayout(2, 2, 12, 6));
         dataPanel.add(fahrLabel);
         dataPanel.add(celsiusLabel);
         dataPanel.add(fahrField);
         dataPanel.add(celsiusField);
      
         JPanel buttonPanel = new JPanel();
         buttonPanel.add(fahrButton);
         buttonPanel.add(celsiusButton);
         Container container = getContentPane();
         container.add(dataPanel, BorderLayout.CENTER);
         container.add(buttonPanel, BorderLayout.SOUTH);
         fahrButton.addActionListener(new FahrenheitListener());
         celsiusButton.addActionListener(new CelsiusListener());
      }
   	
       private class FahrenheitListener implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
         {
            try
            {
               String input = fahrField.getText();
               double fahr = Double.parseDouble(input);
               thermo.setFahrenheit(fahr);
               double celsius = thermo.getCelsius();
               celsiusField.setText(String.format("%.2f", celsius));
            }
                catch(Exception ex)
               {
                  JOptionPane.showMessageDialog(null, "ERROR: Input Syntax");
               }
         }
      }
   
       private class CelsiusListener implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
         {
            String input = celsiusField.getText();
            double celsius = Double.parseDouble(input);
            thermo.setCelsius(celsius);
            double fahr = thermo.getFahrenheit();
            fahrField.setText(String.format("%.2f", fahr));
         }
      }  
      
       public static void main(String [] args)
      {
         Converter theGUI = new Converter();
         theGUI.setTitle("F to C Converter");
         theGUI.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         theGUI.pack();
         theGUI.setVisible(true);
      } 
   }

//////////////////////////////////////////////////////////////////////////////////
//Thermometer Class 
// New Program
//////////////////////////////////////////////////////////////////////////////////

// Arman Majumder
// Corresponds with Temperature Converter GUI <Converter.java>

    public class Thermometer 
   {
      private double degreesCelsius;
   
       public void setCelsius(double degrees)
      {
         degreesCelsius = degrees;
      }
       public void setFahrenheit(double degrees)
      {
         degreesCelsius = (degrees - 32.0) * 5.0 / 9.0;
      }
       public double getCelsius()
      {
         return degreesCelsius;
      }
       public double getFahrenheit()
      {
         return degreesCelsius * (9.0 / 5.0) + 32.0;
      }   
   }
Oo5 0 Newbie Poster

I tried to copy the text to my Complier program but it doesn't work

Chalandria 0 Light Poster

It's because you need to save them as 2 different .java files.

Helen Toma 0 Newbie Poster

my program is just 2 classes..

convert from Fahrenheit to Celsius and from Celsius to Fahrenheit:

Class 1:

// Author: Helen Toma

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JTextField;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;

public class ConvertTemp extends JFrame {
	// instance variables 
	JLabel labelFahr;
	JLabel labelCels;
	JTextField textFahr;
	JTextField textCels;
	JButton conFahToCel;
	JButton conCelToFah;
	
	public ConvertTemp () {
		
		super ("Temperature");
		setLayout (new FlowLayout ());
		
		labelFahr = new JLabel ("Fahrenheit: ", SwingConstants.LEFT);
		labelFahr.setToolTipText("This is a temerature scale");
		add (labelFahr);
		textFahr = new JTextField (10);
		add (textFahr);
		
		labelCels = new JLabel ("Celsius:       ", SwingConstants.LEFT);
		labelCels.setToolTipText("This is a scale and unit of measurement for temperature");
		add (labelCels);
		textCels = new JTextField (10);
		add (textCels);
		
		conFahToCel = new JButton ("Convert Fahrenheit to Celsius");
		add (conFahToCel);
		conCelToFah = new JButton ("Convert Celsius to Fahrenheit");
		add (conCelToFah);
		
		JPanel panel = new JPanel(new GridLayout(2, 2, 12, 6));
		panel.add(labelFahr);
		panel.add(labelCels);
		panel.add(textFahr);
		panel.add(textCels);
		add(panel, BorderLayout.NORTH);
		
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(conFahToCel);
		buttonPanel.add(conCelToFah);	
		add(buttonPanel, BorderLayout.SOUTH);
		
		conFahToCel.addActionListener(new FahrListener ());
		conCelToFah.addActionListener(new CelsListener ());
		
	} // end constructor 
	
	private class FahrListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == conFahToCel){
				int conFahToCel = (int) ((5.0/9.0 * (((Double.parseDouble(textFahr.getText())) -32))));
				textCels.setText(conFahToCel + " °C");
				textFahr.requestFocus();
				textFahr.selectAll();
			} // end if statement
			
		} // end actionPerformed
	} // end FahrListener
	
	private class CelsListener implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == conCelToFah){
				int conCelToFah = (int) (( 9.0/5.0 * (((Double.parseDouble(textCels.getText())) + 32))));
				textFahr.setText(conCelToFah + " °F");
				textCels.requestFocus();
				textCels.selectAll();
			} // end if statement
			
		} // end actionPerformed
	} // end CelsListener
	
} // end class ConvertTemp

----------------------------------------------------------------------------------------

Class 2:

// Author: Helen Toma

import javax.swing.JFrame;

public class ConvertTempTest {

	public static void main(String[] args) {
		
		ConvertTemp app = new ConvertTemp ();
		app.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
		app.setSize (500, 150); // set the frame size
		app.setVisible (true); // display frame

	} // end main method

} // end class ConvertTempTest
robinlrandall 0 Newbie Poster

Actually Helen Toma's C to F formula will not work. The "+32" needs to be OUTSIDE the product part.
An easy mistake from trying to copy the other formula. (I did it myself too)
RR

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.