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


/**
  set up and display a frame with three buttons
      now using lambda expressions to make specifying the
      buttons' action listeners more concise;

  Adapted from "Core Java" text, 11th edition, Chapter 10

  @author Cay Horstmann
  @author adapted by Sharon Tuttle
  @version 2021-11-01
*/

public class Button2Test
{
    /**
        creates a simple 3-button frame

        @param args not used here
     */

    public static void main(String[] args)
    {
	EventQueue.invokeLater(
	    () ->
	       {
		   Button2Frame frame = new Button2Frame();
		   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		   frame.setVisible(true);
	       } );
    }
}

/**
     A frame with a button panel
 */

class Button2Frame extends JFrame
{
    // data fields

    private JPanel buttonPanel;
    
    private static final int DEFAULT_WIDTH = 300;
    private static final int DEFAULT_HEIGHT = 200;
   
    /**
        construct a button-panel frame instance
    */

    public Button2Frame()
    {
	setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
	setTitle("Button2 Test");

        // create some buttons

	buttonPanel = new JPanel();
	
        makeButton("yellow", Color.YELLOW);
	makeButton("blue", Color.BLUE);
	makeButton("red", Color.RED);
        makeButton("green", Color.GREEN);
	makeButton("magenta", Color.MAGENTA);
	
	this.add(buttonPanel);
    }

    /**
        expects a button's desired label test and a desired background color,
           and sets up a button with that label text that can change
           the background color of its container when it is clicked
    */

    public void makeButton(String buttonLabelText, Color backgrdColor)
    {
	JButton button = new JButton(buttonLabelText);
	buttonPanel.add(button);

	button.addActionListener(
	    event ->
	       buttonPanel.setBackground(backgrdColor) );
    }
}