Newer
Older

Bruno Ariganello
a validé
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package tp8.fenetres;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import tp8.Application;
/**
*
* @author brunomermet
*/
public class FenetreConversion extends AbstractFenetreInterne {
private JTextField champCelsius;
private JTextField champFarenheit;
private JButton boutonConvertir;
private Action actionConvertir;
private boolean celsiusAFocus;
public FenetreConversion(Action action) {
super(action, "Conversion celsius/Farenheit");
this.setSize(new Dimension(100, 50));
this.setLayout(new GridLayout(3, 1));
JPanel ligneCelsius = new JPanel();
ligneCelsius.setLayout(new FlowLayout(FlowLayout.TRAILING));
JLabel labCelsius = new JLabel("Celsius :");
champCelsius = new JTextField(15);
labCelsius.setLabelFor(champCelsius);
ligneCelsius.add(labCelsius);
ligneCelsius.add(champCelsius);
this.add(ligneCelsius);
celsiusAFocus = true;
champCelsius.addFocusListener(new EcouteurFocus(true));
JPanel ligneFarenheit = new JPanel();
ligneFarenheit.setLayout(new FlowLayout(FlowLayout.TRAILING));
JLabel labFarenheit = new JLabel("Farenheit :");
champFarenheit = new JTextField(15);
labFarenheit.setLabelFor(champFarenheit);
ligneFarenheit.add(labFarenheit);
ligneFarenheit.add(champFarenheit);
this.add(ligneFarenheit);
champFarenheit.addFocusListener(new EcouteurFocus(false));
JPanel ligneValider = new JPanel();
ligneValider.setLayout(new FlowLayout(FlowLayout.CENTER));
actionConvertir = new ActionConvertir();
boutonConvertir = new JButton(actionConvertir);
ligneValider.add(boutonConvertir);
this.add(ligneValider);
pack();
getRootPane().setDefaultButton(boutonConvertir);
}
private class EcouteurFocus implements FocusListener {
private boolean aStocker;
public EcouteurFocus(boolean b) {
aStocker = b;
}
@Override
public void focusGained(FocusEvent fe) {
celsiusAFocus = aStocker;
}
@Override
public void focusLost(FocusEvent fe) {
return;
}
}
private class ActionConvertir extends AbstractAction {
public ActionConvertir() {
super("Convertir");
}
@Override
public void actionPerformed(ActionEvent ae) {
double tempCelsius = 0;
double tempFarenheit = 0;
if (celsiusAFocus) {
try {
tempCelsius = Double.parseDouble(champCelsius.getText());
tempFarenheit = 9. / 5 * tempCelsius + 32;
champFarenheit.setText("" + tempFarenheit);
} catch (NumberFormatException nfe) {
champFarenheit.setText("Format incorrect");
}
} else {
try {
tempFarenheit = Double.parseDouble(champFarenheit.getText());
tempCelsius = (tempFarenheit - 32) * 5. / 9;
champCelsius.setText("" + tempCelsius);
} catch (NumberFormatException nfe) {
champCelsius.setText("Format incorrect");
}
}
}
}
}