Bonjour tout le monde,
je suis un vrai débutant avec swing j'ai commencé ce matin ^^
et je possède déja un probleme, je souhaite juste faire un programme qui affiche une fenetre avec 2 champs textes, ou l'on peut rentrer des int et le programme va calculer leur somme (faut commencer petit ^^)
et mon probleme d'apres eclipse se situe au moment ou je convertis mes strings obtenu en int pour calculer la somme
voici mon code
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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 import javax.swing.*; import java.awt.FlowLayout; public class Calculatrice { int i=0; int i2=0; int res=0; Calculatrice(int in, int in2,int res2){ in=i; in2=i2; res=res2; } public int getI() { return i; } public void setI(int i) { this.i = i; } public int getI2() { return i2; } public void setI2(int i2) { this.i2 = i2; } public int getRes() { return res; } public void setRes(int res) { this.res = res; } public static void main(String[] args) { Calculatrice calc=new Calculatrice(0,0,0); JPanel contenuFenêtre = new JPanel(); FlowLayout disposition = new FlowLayout(); contenuFenêtre.setLayout(disposition); JLabel label1 = new JLabel("Nombre 1 :"); JTextField entrée1 = new JTextField(10); JLabel label2 = new JLabel("Nombre 2 :"); JTextField entrée2 = new JTextField(10); JLabel label3 = new JLabel("Somme :"); JTextField résultat = new JTextField(10); JButton lancer = new JButton("Ajouter"); String e1=entrée1.getText(); String e2=entrée2.getText(); calc.setI(Integer.valueOf(e1).intValue()); calc.setI2(Integer.valueOf(e2).intValue()); MoteurCalcul moteurCalcul = new MoteurCalcul(calc); lancer.addActionListener(moteurCalcul); résultat.setEditable(false); String rep=Integer.toString(calc.getRes()); résultat.setText(rep); contenuFenêtre.add(label1); contenuFenêtre.add(entrée1); contenuFenêtre.add(label2); contenuFenêtre.add(entrée2); contenuFenêtre.add(label3); contenuFenêtre.add(résultat); contenuFenêtre.add(lancer); JFrame cadre = new JFrame("Ma première calculatrice"); cadre.setContentPane(contenuFenêtre); cadre.setSize(400,100); cadre.setVisible(true); } }
il y'a aussi le fichier pour calculer
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class MoteurCalcul implements ActionListener { Calculatrice parent; MoteurCalcul(Calculatrice parent) { this.parent = parent;} public void actionPerformed(ActionEvent événement) { this.parent.setRes(parent.getI()+parent.getI2()); } }
apres il est possible que tout mon code soit faux, je le redis je suis vraiment un débutant
Partager