The development of the project will be based on j avax.swing.JPanel Temperature
ID: 3706961 • Letter: T
Question
The development of the project will be based on javax.swing.JPanel
Temperature Converter.
The program should have the following characteristics:
- It should disokay a drio down list to select if we are converting from celius to farenheit or backwords.
- It should have a two buttons (converter and reset)
- if the temperature it too high display a message indicating (Warning: temperature is too hot) otherwise indicating (Warning: temperature is too low)
The output should look something like this:
Temperature Converter Temperature Converter This program converts Celsius to Farenheit & Farenheit to Celsius Celalus toF Hazardous Activity C 50 Warning! Hot water is hot! OK Convert . BesetExplanation / Answer
package convert.temperature;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
public class ApplicationRunner {
private static JButton btnConvert;
private static JButton btnReset;
private static JTextField txtCelsius;
private static JTextField txtFarenheit;
private static JLabel lblCelsius;
private static JLabel lblFarenheit;
private static DecimalFormat df2 = new DecimalFormat(".##");
private static JComboBox drpOperation;
private static String[] options = {"Convert Celsius to Farenheit","Convert Farenheit to Celsius"};
public static void main(String[] args) {
JFrame frame = new JFrame("Temperature Convertor");
JLabel label = new JLabel("Temperature Covertor");
JLabel info = new JLabel("This program converts celsius to farenheit & farenheit to celsius");
btnConvert = new JButton("Convert");
btnReset = new JButton("Reset");
txtCelsius = new JTextField();
txtFarenheit = new JTextField();
lblCelsius = new JLabel("C");
lblFarenheit = new JLabel("F");
drpOperation = new JComboBox(options);
label.setBounds(-10, -20, 300, 60);
info.setBounds(40, 70, 500, 60);
drpOperation.setBounds(150, 120, 200, 40);
lblCelsius.setBounds(190, 180, 10, 10);
lblFarenheit.setBounds(190, 210, 10, 10);
txtCelsius.setBounds(210, 180, 50, 20);
txtFarenheit.setBounds(210, 210, 50, 20);
btnConvert.setBounds(150, 250, 95, 30);
btnReset.setBounds(260, 250, 95, 30);
label.setFont(new Font("Courier New", Font.BOLD, 20));
label.setBounds(180, 50, 300, 50);
btnConvert.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int operation = drpOperation.getSelectedIndex();
System.out.println(operation);
float value = -1;
if(operation == 0) {
//Celsius to Farenheit
value = Integer.parseInt(txtCelsius.getText());
if(value > 70)
JOptionPane.showMessageDialog(null, "Temperature is Too hot !!");
else if(value < 5)
JOptionPane.showMessageDialog(null, "Temperature is Too Low !!");
txtFarenheit.setText(celsiusToFarenheit(value));
System.out.println("text "+value);
}else {
//Farenheit to Celsius
value = Integer.parseInt(txtFarenheit.getText());
txtCelsius.setText(FarenheitToCelsius(value));
System.out.println("text "+value);
}
}
});
btnReset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
txtCelsius.setText("");
txtFarenheit.setText("");
drpOperation.setSelectedIndex(0);
}
});
frame.add(label);
frame.add(info);
frame.add(drpOperation);
frame.add(lblCelsius);
frame.add(lblFarenheit);
frame.add(txtCelsius);
frame.add(txtFarenheit);
frame.add(btnConvert);
frame.add(btnReset);
frame.setSize(500, 350);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static String celsiusToFarenheit(float celsius) {
return df2.format(celsius*(9/5) + 32);
}
public static String FarenheitToCelsius(float farenheit) {
return df2.format((farenheit - 32)*5/9);
}
}