import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Create a window with 2 buttons
 * Add (Register) an actionlistener on those buttons
 * When event is triggered JVM automatically calls actionPerformed()     
 */

public class HandleAction1 extends JFrame implements ActionListener {

    JButton b1 = new JButton("Taste Great");        //define fields
    JButton b2 = new JButton("Less Filling");

//---------------------------------------------------------------------
//  main
//---------------------------------------------------------------------

    public static void main(String[] args) 
    {
        HandleAction1 x = new HandleAction1();      //instantiate a program object
    }                           

//---------------------------------------------------------------------
//  constructor
//---------------------------------------------------------------------

    public HandleAction1() {

        super("Miller Light");              //call JFrame constructor
                                            //and populate the title bar
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel panel = new JPanel();
        setContentPane(panel);              //set contentPanel to panel

        panel.add(b1);
        panel.add(b2);

        pack();                             //since no window size was given, 
                                            //we must pack the components
        setVisible(true);

        b1.addActionListener(this);         //register an actionListener for button 
        b2.addActionListener(this);     
   }

//--------------------------------------------------------------------------------------
//  Define an actionPerformed method that will be called by JVM when action is performed
//  Method receives an event object
//--------------------------------------------------------------------------------------

    public void actionPerformed(ActionEvent evt) {

        Object source = evt.getSource();        //get a reference to the object 
                                                //that caused the event
        if (source == b1)
            setTitle("Taste Great");
        if (source == b2)
            setTitle("Less Filling");

//      repaint();
    }
}