import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

/**
 * A window with one button that changes text.
 */
public class ButtonTAJFrameSol extends JFrame implements ActionListener {

  /** My text area. */
  private JTextArea ta;

  /** My buttons. */
  private Vector buttons= new Vector();

  /** The click count. */
  private int clickCount= 0;

  /** Create one button in the center. */
  public ButtonTAJFrame(int numButtons) {
    JPanel j= new JPanel();

    // Add i buttons to j and register 'this' with each as an action listener.
    for (int i= 0; i != numButtons; i++) {
      buttons.add(new JButton("Button " + i));
      j.add((JButton)  buttons.get(i));
     ((JButton) buttons.get(i)).addActionListener(this);
   }
 
    ta= new JTextArea("Click count: 0", 20, 30);

    Container c= this.getContentPane();
    c.add(j, "North");
    c.add(this.ta, "Center");

    this.pack();
  }

  /** React to a button click: ask the user for the new button name. */
  public void actionPerformed(ActionEvent e) {
    ++clickCount;

    JButton b= (JButton) e.getSource(); // this points to what b1 points to!
    
    if (b == buttons.get(0)) {
      ta.setText(ta.getText() + "\n" + "First");
    } else if (b == buttons.get(buttons.size()-1)) {
      ta.setText(ta.getText() + "\n" + "Last");
    }
    // b.setText("Click count: " + clickCount);

    
 // Set the text of the text area to whatever it had, plus a newline,
 // plus the text on the button.
    ta.setText(ta.getText() + "\n" + b.getText());

  }
}
