import javax.swing.*;
import java.awt.*; 
import java.awt.event.*;

/*Simple demonstration of placing several panels 	within the content pane.  */
 	
 	
public class PanelDemoTwo extends JFrame implements ActionListener
{
    public static final int WIDTH = 300;
    public static final int HEIGHT = 200;
    private JPanel firstPanel,secondPanel;

    public static void main(String[] args)
    {
        PanelDemoTwo guiWithPanel = new PanelDemoTwo();
        guiWithPanel.setVisible(true);
    }

    public PanelDemoTwo()
    {
        setSize(WIDTH, HEIGHT);
        addWindowListener(new WindowDestroyer());
        setTitle("Panel Demonstration");
        

        Container contentPane = getContentPane ();                
        contentPane.setBackground(Color.blue); 
        contentPane.setLayout (new GridLayout () );
        
        firstPanel = new JPanel ();
        firstPanel.setBackground (Color.white);
        
        
        secondPanel = new JPanel ();
        secondPanel.setBackground ( Color.gray);
      
           
        
        

        JButton stopButton = new JButton("Red");
        stopButton.setBackground(Color.red); 
        stopButton.addActionListener(this);
        firstPanel.add(stopButton);

        JButton goButton = new JButton("Green");
        goButton.setBackground(Color.green);
        goButton.addActionListener(this); 
        secondPanel.add(goButton); 
        
        
        
        contentPane.add(firstPanel);
        contentPane.add(secondPanel);
        
        
    }
 
       
    public void actionPerformed(ActionEvent e) 
    {
    	
       if (e.getActionCommand().equals("Red"))
        {
        	 if (firstPanel.getBackground() .equals (Color.white ))
              firstPanel.setBackground(Color.red);
           else
           	  firstPanel.setBackground (Color.white );
        }
        else if (e.getActionCommand().equals("Green"))
        {
        	 if ( secondPanel.getBackground ().equals (Color.gray) )
              secondPanel.setBackground(Color.green);
           else
        	    secondPanel.setBackground ( Color.gray);      
        }
        else
           System.out.println ( "Error in button interface.");

     }
    
}
