import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GridBagLayoutDriver 
{
	public static void main( String []args)
	  {
	  	  new GridBagLayoutTest(); // implements an automatic object
	  	 
      }
}
class GridBagLayoutTest extends JFrame implements ActionListener,WindowListener  
                            // You must extend JFrame instead of Frame
							// if you want to use J components
{    
    JButton first = new JButton("First - a button with a very long caption");
    Button second = new Button("Second");
    TextField txtFirst= new TextField("field");
    JCheckBox chkFirst = new JCheckBox("check this out I am a checkbox");
    Button third = new Button("third");
    GridBagConstraints constraint = new GridBagConstraints();
    String message = "Click a button or type in the text field";
    boolean action = false;
    public GridBagLayoutTest()                //constructor  executes on implementation
    {
    	setTitle("GridBagLayout test");
        setLayout(new GridBagLayout());
        constraint.gridx = 1;
        constraint.gridy = 1;
        add(first,constraint);   //add buttons to Frame in relative (constraint) location
    	first.addActionListener(this);    //activates ActionListener
        constraint.gridy = 0;
        constraint.gridx = 1;
        first.setSelected(true);
        add(second,constraint);
    	second.addActionListener(this);    //activates ActionListener
        constraint.gridx = 2;
        constraint.ipadx = 100;  //spacing inside a component
        constraint.gridy = 2;
       // constraint.insets=new Insets(100,20,0,0);  //spacing outside a component
        add(txtFirst,constraint);
    	txtFirst.addActionListener(this); //activates ActionListener
        constraint.gridx = 3;
        constraint.gridy = 3;
        add(chkFirst,constraint);
        //chkFirst.setVisible( true);
        chkFirst.setOpaque(true);        
        constraint.gridx = 4;
        constraint.gridy = 4;
        add(third,constraint);   //add buttons to Frame in relative (constraint) location
    	third.addActionListener(this);    //activates ActionListener
        setSize(1000,700);  //Sets the size of the frame in pixels
        addWindowListener(this); //activates WindowListener
    	chkFirst.addActionListener(this); //activates ActionListener
         pack();
    	setVisible(true);
    	while(true)   // infinite loop - The operating system interrupts when
			{       	// a component is activated
				if (action)
				   {
				   	   action = false;
				   	   repaint();  //sends message to the operating system
				   }               //which in turn calls the paint method
		    }
				   	               
    }
    public void paint(Graphics pen)
    {  
        pen.setColor(Color.white);
        pen.fillRect(100,25,250,50);         //erase the previous message
        pen.setColor(Color.black);
        pen.drawString(message,100,50);      //reflects the component activated
    }
    public void windowClosing(WindowEvent w)//ALL abstract methods in WindowListener
    {										//interface must be overridden
    	System.exit(0);   //Closes program
    }
    public void windowOpened(WindowEvent w)
    {
    	
    }
    public void windowClosed(WindowEvent w)
    {
    	System.exit(0);    //Closes program
    }
    public void windowDeactivated(WindowEvent w)
    {
    	System.exit(0);    //Closes program
    }
    public void windowDeiconified(WindowEvent w)
    {
    	
    }
    public void windowIconified(WindowEvent w)
    {
    	
    }
    public void windowActivated(WindowEvent w)
    {
    	
    }
    public void actionPerformed(ActionEvent e)
    {
    	 this.setTitle("" +e.getActionCommand() );
    	 message = e.getActionCommand();
         action = true;
    }
    public void keyTyped(KeyEvent e)  //overridden methods
    {
    	this.setTitle("" +e.getKeyChar());
    	System.out.println("TYPED "+e.getKeyChar());
    } 
	public void keyPressed(KeyEvent e)
    {
    	this.setTitle("" +(char)e.getKeyCode());
    	System.out.println("PRESSED "+(char)e.getKeyCode());
    } 
	public void keyReleased(KeyEvent e)
    {
    	System.out.println("RELEASED "+e.getKeyCode());
    } 
   
}

