Bonsoir,

J'ai un JPanel qui contient plusieurs boutons, J'ai besoin de récupérer la valeur de chaque boutons sur le quel on a cliqué et l'afficher dans un JLabel

Voila ce que j'ai fais mais ça ne marche pas

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
86
87
88
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
 
public class Fenetre extends JFrame {
 
 
    JPanel panel = new JPanel();
 
Border b = BorderFactory.createLineBorder(Color.black);
JLabel resultat = new JLabel("resultat");
BorderLayout droit = new BorderLayout(10,10);
BorderLayout centre = new BorderLayout(10,10);
BorderLayout haut = new BorderLayout(10, 10);
JButton egal= new JButton("=");
 
JButton nombre1= new JButton("1");
JButton nombre2= new JButton("2");
JButton plus= new JButton("+");
    Fenetre() {
 
        this.setTitle("Calculatrice");
 
        this.setSize(600, 600);
        this.setDefaultCloseOperation(this.EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
 
 
        resultat.setBackground(Color.WHITE);
 
        this.getContentPane().add(resultat,haut.NORTH);
 
        panel.setLayout(new GridLayout(2, 2, 5, 5));
        panel.add(nombre1);
        panel.add(nombre2);
        panel.add(plus);
        panel.add(egal);
 
egal.addActionListener(new Affichage());
nombre1.addActionListener(new Affichage());
 
nombre2.addActionListener(new Affichage());
 
        this.getContentPane().add(panel, centre.CENTER);
 
        resultat.setBorder(new EmptyBorder(5, 5, 5, 5));
    panel.setBorder(new EmptyBorder(5, 5, 5, 5));
     this.pack();
     this.setVisible(true);
 
 
    }
 
private class Affichage implements ActionListener {
 
    protected double calcul ;
 
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        Object source = e.getSource();
 
        for (Component p : panel.getComponents())
        {
 
 
            if(source ==p)
                {
    resultat.setText(p.getName());
                }
 
 
    }
D'avance merci