I'd like to know a method to differentiate a button that generated event, in my method that will handle events.
In the example below I add the same class that handles events to 2 buttons, but I can not differentiate the buttons in my method actionPerformed()
;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class Main{
public static void main(String[] args){
JFrame f = new JFrame();
JButton b1 = new JButton("B1");
JButton b2 = new JButton("B2");
ActionListener evento = new Eventos();
b1.addActionListener(evento);
b2.addActionListener(evento);
f.getContentPane().add(b1);
f.getContentPane().add(b2);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300,400);
f.setVisible(true);
f.setLayout(new GridLayout(1,2));
f.pack();
}
}
class Eventos implements ActionListener{
public void actionPerformed(ActionEvent e) {
// diferenciar b1 do b2;
System.out.println("Clique");
}
}