Results 1 to 2 of 2

Thread: Java program

  1. #1
    Join Date
    Nov 2002
    Posts
    5

    Angry Unanswered: Java program

    I am having trouble makin this program work!! can someone plz help me!!

    //This program converts Celsius to Fahrenheit and vice versa

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class Temperature extends JApplet implements ActionListener{



    JLabel cLabel, fLabel, resultCLabel, resultFLabel;
    JTextField input1, input2, resultField1, resultField2;
    JButton convert;

    public void init()
    {



    Container container = getContentPane();
    container.setLayout(new FlowLayout());

    cLabel = new JLabel( "Celsius" );
    container.add( cLabel );
    input1 = new JTextField( 10 );
    input1.setEditable( true );
    container.add( input1 );

    fLabel = new JLabel( "Fahrenheit" );
    container.add( fLabel );
    input2 = new JTextField( 10 );
    input2.setEditable( true );
    container.add( input2 );

    convert = new JButton("Convert");
    convert.addActionListener( this );
    container.add( convert );

    resultCLabel = new JLabel( "Fahrenheit" );
    container.add( resultCLabel );
    resultField1 = new JTextField( 15 );
    resultField1.setEditable( false );
    container.add( resultField1 );

    resultFLabel = new JLabel( "Celsius" );
    container.add( resultFLabel );
    resultField2 = new JTextField( 15 );
    resultField2.setEditable( false );
    container.add( resultField2 );

    }

    public void actionPerformed( ActionEvent actionEvent )
    {
    double F, C;

    //if ( actionEvent.getSource() == input1 ){

    F = Double.parseDouble(input1.getText() );
    celsius(F);
    resultField1.setText(Double.toString(F));
    //}

    //else {
    C = Double.parseDouble( input2.getText() );
    fahrenheit(C);
    resultField2.setText(Double.toString(C));
    //}

    }
    public double celsius(double F)
    {

    return 5.0/9.0 * ( F - 32 );

    }

    public double fahrenheit(double C)
    {

    return 9.0/5.0 * C + 32;
    }

    }

  2. #2
    Join Date
    Nov 2002
    Posts
    21

    Re: Java program

    Originally posted by Confused1
    I am having trouble makin this program work!! can someone plz help me!!

    //This program converts Celsius to Fahrenheit and vice versa

    import java.awt.*;
    import java.awt.event.*;

    import javax.swing.*;

    public class Temperature extends JApplet implements ActionListener{



    JLabel cLabel, fLabel, resultCLabel, resultFLabel;
    JTextField input1, input2, resultField1, resultField2;
    JButton convert;

    public void init()
    {



    Container container = getContentPane();
    container.setLayout(new FlowLayout());

    cLabel = new JLabel( "Celsius" );
    container.add( cLabel );
    input1 = new JTextField( 10 );
    input1.setEditable( true );
    container.add( input1 );

    fLabel = new JLabel( "Fahrenheit" );
    container.add( fLabel );
    input2 = new JTextField( 10 );
    input2.setEditable( true );
    container.add( input2 );

    convert = new JButton("Convert");
    convert.addActionListener( this );
    container.add( convert );

    resultCLabel = new JLabel( "Fahrenheit" );
    container.add( resultCLabel );
    resultField1 = new JTextField( 15 );
    resultField1.setEditable( false );
    container.add( resultField1 );

    resultFLabel = new JLabel( "Celsius" );
    container.add( resultFLabel );
    resultField2 = new JTextField( 15 );
    resultField2.setEditable( false );
    container.add( resultField2 );

    }

    public void actionPerformed( ActionEvent actionEvent )
    {
    double F, C;

    //if ( actionEvent.getSource() == input1 ){

    F = Double.parseDouble(input1.getText() );
    celsius(F);
    resultField1.setText(Double.toString(F));
    //}

    //else {
    C = Double.parseDouble( input2.getText() );
    fahrenheit(C);
    resultField2.setText(Double.toString(C));
    //}

    }
    public double celsius(double F)
    {

    return 5.0/9.0 * ( F - 32 );

    }

    public double fahrenheit(double C)
    {

    return 9.0/5.0 * C + 32;
    }

    }


    Can u pls specify where your program is hanging??


    S

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •