import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class layoutTest extends JFrame 
{
// define all components outside of methods, so e.getSource()
// can identify them in the ActionPerformed method
JTextField writeBox;

JComboBox myCombo;

JButton writeButton;
JButton exitButton;

JButton AButton;
JButton BButton;
JButton CButton;

public layoutTest()
   {
   setSize(300,300);
   setDefaultCloseOperation(EXIT_ON_CLOSE);

   Container c = getContentPane();
   c.setLayout( new BorderLayout(20,20 ) ); 
   String names[ ] = {"stay", "leave", "ignore"};

  // create an icon out of local file "Exit.gif"
   ImageIcon exitSign = new ImageIcon("Exit.gif");
   exitButton = new JButton( exitSign );
   
   writeBox = new JTextField( ".................................................." );
   writeButton = new JButton( "Write" );
   myCombo = new JComboBox( names );
   AButton = new JButton( "A" );
   BButton = new JButton( "B" );
   CButton = new JButton( "C" );
 
   // add everything to the container 
   c.add( writeBox, BorderLayout.NORTH );   
   c.add( AButton, BorderLayout.EAST );
   c.add( BButton, BorderLayout.CENTER );
   c.add( CButton, BorderLayout.WEST );
   c.add( myCombo );
   c.add( writeButton );
   c.add( exitButton, BorderLayout.SOUTH );
   
   // create handlers
   eventClass handler = new eventClass( );
   ComboHandler myComboHandler = new ComboHandler( );   
   
   // add components to appropriate handlers   
   writeButton.addActionListener( handler );
   writeBox.addActionListener( handler );
   exitButton.addActionListener( handler );
   AButton.addActionListener( handler );
   BButton.addActionListener( handler );
   CButton.addActionListener( handler );
    
   myCombo.addItemListener(myComboHandler);
   
   setVisible(true);
   }
	 
public static void main (String [] args)
   {
   layoutTest app = new layoutTest();
   } // end main

// actions   
private class eventClass implements ActionListener 
   {
   public void actionPerformed ( ActionEvent e ) 
      {
      // use e.getActionCommand( ); to print button label
      System.out.println("Action: " + e.getActionCommand( ));
	  
	  // use e.getSOurce to identify button
      if (e.getSource() == exitButton)
         {
		 System.out.println("Exiting...");
		 System.exit(0); 
		 } 
		 
	  if (e.getSource() == writeButton)
	     {
		 System.out.println( "Text in box: " + writeBox.getText() );
		 } 	 	 
		 
      } // end actionPerformed method
   } // end private class

// items
private class ComboHandler implements ItemListener
   {
   public void itemStateChanged( ItemEvent itemObj )
      {
      System.out.println("Item Press in Combo Box: " + itemObj.getItem( ) );
      }
   } // end inner class
   
} // end class

