Type Here to Get Search Results !

Temperature Converter Program | java code

         Write a program of temperature converter in java


Hello, This temperature converter java code/program is created on java swing

In this java program multiple features of temperature converter is used

                1.Converts Celcius to Farenheit and Celcius to Kelvin

                2.Converts Farenheit to Celcius and Farenheit to Kelvin

                3.Converts Kelvin to Celcius and Kelvin to Farenheit 

                4.Added some new features.

In this temperature converter java program we create the session of every part of code like where to declare class, how to initiate class, where we declare object.

In this java program we use java swing components like jbutton, jlabel, panel etc.

In this temperature converter java code we use add action listener method of java/ advance java

To create this temperature converter java program we used multiple temperature formulas in java-

  1. find celcius temperature in java 
  2. find fahrenheit temperature in java
  3. find kelvin temperature in java

Temperature Converter In Java :-





import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
 *
 * @author TOTAL PROGRAMMING
 */
public class Temperature2 implements ActionListener
{
    //----------- declaration of objects --------------
    JFrame frame ;
    JPanel panel ;
    JButton Submit , clear;
    JLabel celcius , fahrenheit , kelvin ;
    JTextField celcius_tf , fahrenheit_tf , kelvin_tf ;
    Temperature2()
    {
        //----------- initialization of objects --------------
        frame = new JFrame();
        panel = new JPanel();
        Submit = new JButton("Submit");
        clear = new JButton("Clear");
        celcius = new JLabel("Celcius");
        fahrenheit = new JLabel("Fahrenheit");
        kelvin = new JLabel("Kelvin");
        celcius_tf = new JTextField();
        fahrenheit_tf = new JTextField();
        kelvin_tf = new JTextField();
        
        panel.setLayout(null);  //set layout to panel
        
        //----------- set background color and bounds to textfield --------------
        celcius_tf.setBackground(new Color(178, 190, 181));
        fahrenheit_tf.setBackground(new Color(178, 190, 181));
        kelvin_tf.setBackground(new Color(178, 190, 181));
        celcius_tf.setBounds(60,30,50,30);
        fahrenheit_tf.setBounds(120,30,50,30);
        kelvin_tf.setBounds(180,30,50,30);
        
        //----------- add textfields to panel ------------
        panel.add(celcius_tf);
        panel.add(fahrenheit_tf);
        panel.add(kelvin_tf);
        
        //----------- set bounds to label --------------
        celcius.setBounds(60,63,50,30);
        fahrenheit.setBounds(115,63,70,30);
        kelvin.setBounds(185,63,50,30);
        
        //----------- add label to panel ------------
        panel.add(celcius);
        panel.add(fahrenheit);
        panel.add(kelvin);
        
        //----------- set background color , bounds and register actionlistener to button --------------
        Submit.setBounds(155,100,80,30);
        clear.setBounds(50,100,80,30);
        Submit.setBackground(new Color(127, 255, 212));
        clear.setBackground(new Color(127, 255, 212));
        Submit.addActionListener(this);
        clear.addActionListener(this);
        
        //----------- add button to panel ------------
        panel.add(Submit);
        panel.add(clear);
        
        //----------- set background color to panel ------------
        panel.setBackground(new Color(115, 147, 179));
        
        frame.add(panel); //----------- add panel to frame ------------
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set default close opration of frame to exit on close
        frame.setResizable(false);
        frame.setSize(300,200);
        frame.setVisible(true);
        
    }
    @Override
    public void actionPerformed(ActionEvent ae)
    {
        
        if(celcius_tf.getText().isEmpty() == false)
        {
            float c1 = Float.parseFloat(celcius_tf.getText());
            double f1 =  c1 * (9.0/5.0) +32 ;
            double k1 =  c1 + 273 ;
            fahrenheit_tf.setText(Double.toString(f1));
            kelvin_tf.setText(Double.toString(k1));
        }
        else if(fahrenheit_tf.getText().isEmpty() == false)
        {
            float f2 = Float.parseFloat(fahrenheit_tf.getText());
            double c2 = (f2 - 32 / 9) * 5 ;
            double k2 = (f2 - 32 / 9) * 5 + 273 ;
            celcius_tf.setText(Double.toString(c2));
            kelvin_tf.setText(Double.toString(k2));
        }
        else if(kelvin_tf.getText().isEmpty() == false)
        {
            float k2 = Float.parseFloat(kelvin_tf.getText());
            double c2 = (k2 - 273 / 5 ) * 5 ;
            double f2 = (k2 - 273 / 5) * 9 + 32 ;
            celcius_tf.setText(Double.toString(c2));
            fahrenheit_tf.setText(Double.toString(f2));
        }
        if(ae.getSource() == clear)
        {
            celcius_tf.setText("");
            fahrenheit_tf.setText("");
            kelvin_tf.setText("");
        }
    }
    
    public static void main(String args[]) 
    {
        Temperature2 t = new Temperature2();
    }
}  

Output of temperature converter in java :-

Temperature Converter Program | java code


Tags

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Below Post Ad