import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JButton;
public class Botão implements ActionListener {
private JButton BotãoUm, BotãoDois;
public Botão(JButton BotãoUm, JButton BotãoDois) {
this.BotãoUm = BotãoUm;
this.BotãoDois = BotãoDois;
}
@Override
public void actionPerformed(ActionEvent evento) {
if(evento.getSource() == BotãoUm){
JOptionPane.showMessageDialog(null, "Você clicou no Botão 1.");
}
if(evento.getSource() == BotãoDois){
JOptionPane.showMessageDialog(null, "Você clicou no Botão 2.");
}
}
}
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JButton;
public class Delicia extends JFrame {
private JButton botãoUm;
private JButton botãoDois;
private Botão handler;
public Delicia()
{
super("Criando botões gostosos.");
setLayout(new FlowLayout());
handler = new Botão(botãoUm, botãoDois);
botãoUm.addActionListener(handler);
add(botãoUm);
botãoDois.addActionListener(handler);
add(botãoDois);
}
}
<strong>Classe Principal:</strong>
import javax.swing.JFrame;
public class Janela {
public static void main(String[] args) {
Delicia frame = new Delicia();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500, 500);
frame.setVisible(true);
}
}
Quando eu executo no Netbeans 8.0.2 dá erro…