A Java Program to Convert Celsius to Fahrenheit.



Hey Guy's!!!!

Welcome once again.It's Parth Patel here.In this tutorial we are going to make a Temperature converter which can convert Celsius to Fahrenheit.In this program we are extending JFrame . For that we are going to use 2 Buttons,2 TextFields,2 Labels and 4 Panels.

b1,b2----------2 Buttons
l1,l2---------- 2 Labels
t1,t2---------- 2 TextFields
p1,p2,p3,p4---------- 4 Panels

p1 is the main panel which is added into the center of border layout.And then p1's layout is set to grid layout of 3x1.Then p2,p3,p4 are added to p1 layout.


Let's start  Programming!!!!

 /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package temperatureconvertor;

import java.awt.BorderLayout;
import java.awt.GridLayout;
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 Parth
 */

class MyFrame extends JFrame implements ActionListener
{
    JButton b1,b2;
    JLabel l1,l2;
    JPanel p1,p2,p3,p4;
    JTextField t1,t2;

    MyFrame()
    {
    b1= new JButton("Convert");
    b2= new JButton("Reset");
    l1= new JLabel("Celsius");
    l2= new JLabel("Fahrenheit");
    p1= new JPanel();
    p2= new JPanel();
    p3= new JPanel();
    p4= new JPanel();
    t1= new JTextField(10);
    t2= new JTextField(10);
    add(p1,BorderLayout.CENTER);
    p1.setLayout(new GridLayout(3,1));
    p1.add(p2);
    p1.add(p3);
    p1.add(p4);
    p2.setLayout(new GridLayout(1,2));
    p3.setLayout(new GridLayout(1,2));
    p4.setLayout(new GridLayout(1,2));
    p2.add(l1);
    p2.add(t1);
    p3.add(l2);
    p3.add(t2);
    p4.add(b1);
    p4.add(b2);
    b1.addActionListener(this);
    b2.addActionListener(this);
    t2.setEditable(false);
    }

    public void actionPerformed(ActionEvent e) {
        String s = e.getActionCommand();
        if(s.equals("Convert"))
        {
            String s1=t1.getText();
            int n= Integer.parseInt(s1);
            float f;
            f=32+(5*n)/9;
            t2.setText(f+"");
        }
        else
        {
            t1.setText(null);
            t2.setText(null);

        }
    throw new UnsupportedOperationException("Not supported yet.");
    }
    }

class Demo
{
 public static void main(String arg[])
 {
 MyFrame mf =new MyFrame();
 mf.setSize(300,150);
 mf.setVisible(true);

 }
}

Comments

  1. Caesars Palace Hotel & Casino, Atlantic City - MapyRO
    Find 파주 출장마사지 Caesars 광주 출장마사지 Palace 수원 출장안마 Hotel & Casino (book online) with GPS coordinates and elevation information. 사천 출장안마 This 김포 출장안마 map is not your typical destination for

    ReplyDelete

Post a Comment