TP Exceptions

This commit is contained in:
Simoes Lukas
2025-03-12 17:10:44 +01:00
parent 2a0aa37baa
commit cf33623a5d
45 changed files with 448 additions and 23 deletions

Binary file not shown.

View File

@@ -0,0 +1,9 @@
public class Conversion {
public static double celsiusAFahrenheit(double n) {
return n*9/5+32;
}
public static double FahrenheitACelsius(double n) {
return (n-32)*5/9;
}
}

Binary file not shown.

View File

@@ -0,0 +1,38 @@
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class Degres extends JComponent {
@Override
public void paintComponent(Graphics pinceau) {
Graphics secondPinceau = pinceau.create();
if (this.isOpaque()) {
secondPinceau.setColor(this.getBackground());
secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
}
secondPinceau.setColor(Color.ORANGE);
secondPinceau.fillRect(0, 0, this.getWidth(), this.getHeight());
JTextField celsius = new JTextField();
JTextField fahrenheit = new JTextField();
celsius.setBounds(0, 65, 160, 20);
fahrenheit.setBounds(0, 90, 160, 20);
JLabel texteCelsius = new JLabel("°C");
JLabel texteFahrenheit = new JLabel("°F");
texteCelsius.setBounds(165, 65, 20, 20);
texteFahrenheit.setBounds(165, 90, 20, 20);
celsius.getDocument().addDocumentListener(new GestionJTextField(celsius, fahrenheit, true));
fahrenheit.getDocument().addDocumentListener(new GestionJTextField(fahrenheit, celsius, false));
this.add(celsius);
this.add(fahrenheit);
this.add(texteCelsius);
this.add(texteFahrenheit);
}
}

Binary file not shown.

View File

@@ -0,0 +1,13 @@
import java.awt.*;
import javax.swing.*;
public class Fenetre extends JFrame {
public Fenetre() {
this.setSize(200, 200);
this.setLocation(100, 100);
this.setLayout(new GridLayout(1, 1));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Degres composant = new Degres();
this.add(composant);
}
}

Binary file not shown.

View File

@@ -0,0 +1,46 @@
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class GestionJTextField implements DocumentListener {
private JTextField aConvertir;
private JTextField convertirVers;
private boolean cToF;
public GestionJTextField(JTextField aConvertir, JTextField convertirVers, boolean cToF) {
this.aConvertir = aConvertir;
this.convertirVers = convertirVers;
this.cToF = cToF;
}
public void changedUpdate(DocumentEvent e) {
}
public void insertUpdate(DocumentEvent e) {
try {
if (this.cToF) {
this.convertirVers.setText(Conversion.celsiusAFahrenheit(Double.parseDouble(this.aConvertir.getText())) + "");
}
else {
this.convertirVers.setText(Conversion.FahrenheitACelsius(Double.parseDouble(this.aConvertir.getText())) + "");
}
} catch (NumberFormatException e2) {
try {
this.convertirVers.setText("???");
} catch (IllegalStateException ee) {
}
} catch (IllegalStateException e3) {
} finally {
this.convertirVers.repaint();
}
}
public void removeUpdate(DocumentEvent e) {
}
}

Binary file not shown.

View File

@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
Fenetre fenetre = new Fenetre();
fenetre.setVisible(true);
}
}