/*
	The program demonstrates  handling of events
	It has 3 buttons, each buttons represent a different color
	A push of a button changes the window background color
	
*
*/

import javax.swing.*;	// Needed for the swing class
import java.awt.*;		// Needed for color class
import java.awt.event.*;	// Needed for event listener interface

public class ChangeBackground extends JFrame
{
	JLabel msg;
	JButton blackButton;
	JButton yellowButton;
	JButton greenButton;
	JButton pinkButton;
	JPanel panel;
	
	public ChangeBackground()
	{
		// Sets the title of the window
		
		setTitle("Change Background Window");
		
		// Sets the size of the window
		
		setSize(230, 400);
		
		// Sets the action when the window is close
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Creates a lable that print on the window
		
		msg = new JLabel("Click a button to change the color");
		
		// Creates the 4 button with names associate to it
		
		blackButton = new JButton("Black");
		yellowButton = new JButton("Yellow");
		greenButton = new JButton("Green");
		pinkButton = new JButton("Pink");
		
		// Register an event listener with all the buttons
		
		blackButton.addActionListener(new blackButtonListener());
		yellowButton.addActionListener(new yellowButtonListener());
		greenButton.addActionListener(new greenButtonListener());
		pinkButton.addActionListener(new pinkButtonListener());
		
		/*
			Now we will create a panel
			Then adds all the buttons to the panel & the label
			
		*
		*/
		
		panel = new JPanel();
		panel.add(msg);
		panel.add(blackButton);
		panel.add(yellowButton);
		panel.add(greenButton);
		panel.add(pinkButton);
		
		// Adds the panel to the content pane ( The JFrame window )
		
		add(panel);
		
		// Sets the window to be visible
		
		setVisible(true);
		
	}
	
	/*
		Here handles the event when the user clicks the button
		The background color will changes to black
		
	*
	*/
	
	public class blackButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			// Sets the panel's background to black
			
			panel.setBackground(Color.BLACK);
			
			// Sets the label's text to blue
			
			msg.setForeground(Color.BLUE);
			
		}
	}
	
	// The background color changes to yellow
	
	public class yellowButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			// Sets the panel's background to yellow
			
			panel.setBackground(Color.YELLOW);
			
			// Sets the label's text to red
			
			msg.setForeground(Color.RED);
			
		}
	}
	
	// The background color changes to green
	
	public class greenButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			// Sets the panel's background to green
			
			panel.setBackground(Color.GREEN);
			
			// Sets the label's text to black
			
			msg.setForeground(Color.BLACK);
			
		}
	}
	
	// The background color changes to pink
	
	public class pinkButtonListener implements ActionListener
	{
		public void actionPerformed(ActionEvent e)
		{
			// Sets the panel's background to pink
			
			panel.setBackground(Color.PINK);
			
			// Sets the label's text to red
			
			msg.setForeground(Color.RED);
			
		}
	}
	
	public static void main(String[] args)
	{
		new ChangeBackground();
	}
	
}
		
