Create a small GUI application that converts Celsius degrees

Create a small GUI application that converts Celsius degrees into Fahrenheit degrees.

The specific look might be different depending on which operating system you are using, but the app should contain a JtextField where the user can type in a temperature in Celsius, and a button with the label “Convert”.

Use this code as an example: Click Counter

The Kilo Conversion app in the text book displays the result in a dialog box. You can take this approach, or you can try to display the result in a JLabel, as in the example below.

______________________________________________________________________

Click Counter codes:

import javax.swing.*;

public class ClickCountMain {

   public static void main(String[] args) {

       JFrame frame = new ClickCountFrame();

       frame.setVisible(true);

   }

}

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

Solution

Find the solution below:

=======================================================================

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

public class Conversion {
   private JLabel label;
   private JTextField textfield;
   private JButton button;
   private JPanel panel;

   public static void main(String args[]) {
       new Conversion();

   }

   public Conversion() {
       JFrame frame = new JFrame(\"Celsius Converter\");
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(200, 100);

       textfield = new JTextField(5);
       button = new JButton(\"Convert\");
       label = new JLabel(\"Fahrenheit: \");

       panel = new JPanel();
       panel.add(textfield);
       panel.add(button);
       panel.add(label);
       frame.add(panel);
       frame.setVisible(true);

       button.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent e) {

               String text = textfield.getText();
               double Celsius = Double.parseDouble(text);
               double Fahrenheit = convert(Celsius);
               label.setText(\"Fahrenheit: \" + Fahrenheit + \" Degrees\");

           }
       });
   }

   double convert(double Celsius) {
       double Fahrenheit = (9 * Celsius / 5) + 32;
       return Fahrenheit;
   }

}

======================================================================

OUTPUT:

=======================================================================

Create a small GUI application that converts Celsius degrees into Fahrenheit degrees. The specific look might be different depending on which operating system y
Create a small GUI application that converts Celsius degrees into Fahrenheit degrees. The specific look might be different depending on which operating system y

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site