import java.awt.*;         // basic awt classes
import java.awt.event.*;   // event classes
import javax.swing.*;      // swing classes

/**
 * CSE 331 12au-16sp. Very simple demo of Swing event handling.  
 * Create a window with a single button that prints a message when it's clicked.
 * Version 1 with named inner class to handle button events.
 * 
 * @author Hal Perkins
 */ 
public class ButtonDemo2 {

  /** Program that displays a message each time a button is pressed. */
  public static void main(String[] args) {
    // Create a new button with label "Hit me" and string "OUCH!" to be
    // returned as part of each action event.
    JButton button = new JButton("Hit me");
    button.setActionCommand("OUCH!");
    button.addActionListener(new ActionListener() {
          private int nEvents = 0;
          @Override public void actionPerformed(ActionEvent e) {
            nEvents++;
            System.out.println(e.getActionCommand() + " " + nEvents);
          }
        });

    // Display this button in a new window.
    showButton(button);
  }

  /** Creates a frame that displays the given button. (IGNORE FOR NOW) */
  private static void showButton(JButton button) {
    JFrame frame = new JFrame("Button Demo");
    frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    JPanel panel = new JPanel();
    panel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
    panel.add(button);
    
    frame.add(panel);
    frame.pack();
    frame.setVisible(true);
  }
}