BUT2/DEV/DEV2.1/TP10_exceptions.java/Q5_Degres/DetectConvertion.java

40 lines
1.1 KiB
Java
Raw Normal View History

2023-10-23 13:23:36 +02:00
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DetectConvertion implements ActionListener{
private JTextField champsFarenheit;
private JTextField champsCelsius;
public DetectConvertion(JTextField champsFarenheit, JTextField champsCelsius){
this.champsFarenheit = champsFarenheit;
this.champsCelsius = champsCelsius;
}
@Override
public void actionPerformed(ActionEvent evenement){
JTextField source = (JTextField) evenement.getSource();
String valeurString = evenement.getActionCommand();
if (source == this.champsCelsius){
try{
double valeurDouble = Double.parseDouble(valeurString);
double resultat = valeurDouble*1.8+32;
this.champsFarenheit.setText(Double.toString(resultat));
}
catch(NumberFormatException erreur){
this.champsFarenheit.setText("???");
}
}
if (source == this.champsFarenheit){
try{
double valeurDouble = Double.parseDouble(valeurString);
double resultat = (valeurDouble-32)/1.8;
this.champsCelsius.setText(Double.toString(resultat));
}
catch(NumberFormatException erreur){
this.champsCelsius.setText("???");
}
}
}
}