Develop a Java program that creates a simple GUI application for atemperature converter. The program should allow users to converttemperatures between Celsius and Fahrenheit.
Question
Develop a Java program that creates a simple GUI application for atemperature converter. The program should allow users to converttemperatures between Celsius and Fahrenheit.
Solution
Sure, here is a simple Java program using Swing for GUI that converts temperatures between Celsius and Fahrenheit.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class TemperatureConverter {
private JFrame frame;
private JTextField textField;
private JButton cToFButton;
private JButton fToCButton;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
TemperatureConverter window = new TemperatureConverter();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public TemperatureConverter() {
initialize();
}
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
textField = new JTextField();
textField.setBounds(34, 36, 146, 26);
frame.getContentPane().add(textField);
textField.setColumns(10);
cToFButton = new JButton("Celsius to Fahrenheit");
cToFButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int tempFahr = (int)((Double.parseDouble(textField.getText())) * 1.8 + 32);
textField.setText(tempFahr + " Fahrenheit");
}
});
cToFButton.setBounds(34, 89, 146, 29);
frame.getContentPane().add(cToFButton);
fToCButton = new JButton("Fahrenheit to Celsius");
fToCButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int tempCels = (int)((Double.parseDouble(textField.getText()) - 32) * 0.5556);
textField.setText(tempCels + " Celsius");
}
});
fToCButton.setBounds(34, 130, 146, 29);
frame.getContentPane().add(fToCButton);
}
}
This program creates a simple GUI with a text field for input and two buttons for conversion. When you enter a temperature and click a button, it converts the temperature to the other scale and displays the result in the text field.
Similar Questions
Create a Temprature class. Make two methods :1. convertFahrenheit - It will take celsius and will print it into Fahrenheit.2. convertCelsius - It will take Fahrenheit and will convert it into Celsius.
WAP in Java to input temperature in Fahrenheit and convert it in Celsius. C= (F-32)*5/9
Write code that allow the user to enter the temperature in degrees Celsius. The program should convert it and print it in Fahrenheit degrees using the following formula: Fahrenheit = (Celsius * 9/5) + 32 For example, if the user enters 0, then the program will print "Temperature in Fahrenheit: 32.0"
Fahrenheit to Celsius degreeWrite a Java program to convert temperature from Fahrenheit to Celsius degree.The Fahrenheit scale is a temperature scale based on one proposed in 1724 by physicist Daniel Gabriel Fahrenheit. It uses the degree Fahrenheit (symbol: °F) as the unit.The Celsius scale, previously known as the centigrade scale, is a temperature scale used by the International System of Units (SI). As an SI derived unit, it is used by all countries in the world, except the U.S.Note:One inch is 0.0254 meter.Print upto 1 value after decimal Sample Input:Input a degree in FahrenheitSample Output:Output will have the Celsius degree value of the Fahrenheit degree input valueClick on the image for preview.Constraints:NAExample:Input:212Output:100.00Explanation:-Click on the image for preview.Public Test Cases:# INPUT EXPECTED OUTPUT1 212100.02 300148.9
Develop a Java program to simulate a currency converter from USD toIndian Rupees using control statements
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.