import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test extends JFrame implements ActionListener
{
private JButton btn1, btn2, btn3, btn4;
private JTextField txt1;
public Test()
{
setLayout(new FlowLayout());
btn1 = new JButton("Botón 1");
btn2 = new JButton("Botón 2");
btn3 = new JButton("Botón 3");
btn4 = new JButton("Botón 4");
txt1 = new JTextField(15);
add(btn1);
add(btn2);
add(btn3);
add(btn4);
add(txt1);
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
btn4.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(250, 150);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource().equals(btn1))
txt1.setText("Pulsaste el botón 1");
else if (e.getSource().equals(btn2))
txt1.setText("Pulsaste el botón 2");
else if (e.getSource().equals(btn3))
txt1.setText("Pulsaste el botón 3");
else if (e.getSource().equals(btn4))
txt1.setText("Pulsaste el botón 4");
}
public static void main(String[] args)
{
new Test();
}
}