1  // File:  BorderLayout4.java
  2  
  3  import java.applet.Applet;
  4  import java.awt.*;
  5  import java.awt.event.*;
  6  
  7  
  8  public class BorderLayout4 extends Applet implements ActionListener{
  9    private Button EastButton, 
 10                   NorthButton, 
 11                   CenterButton;
 12  
 13    public void init() {
 14      // instantiating the button objects
 15      EastButton = new Button("East");
 16      NorthButton = new Button("North");
 17      CenterButton = new Button("Center");
 18  
 19      // add action lisners for all buttons
 20      EastButton.addActionListener(this);
 21      NorthButton.addActionListener(this); 
 22      CenterButton.addActionListener(this);
 23  
 24  
 25      /* setting layout to BorderLayout 
 26         with horizontal and vertical spacing of 10 pixels */
 27      setLayout(new BorderLayout(50,100));
 28      
 29      // order is not important here as we are specifying the positions
 30      
 31      add(EastButton,BorderLayout.EAST);
 32      add( NorthButton,BorderLayout.NORTH);
 33      add( CenterButton,BorderLayout.CENTER);
 34    }
 35    public void actionPerformed( ActionEvent e) 
 36    {
 37      Color c=new Color((float)Math.random(),(float)Math.random(),(float)Math.random());
 38      
 39      if (e.getSource()==EastButton)
 40        EastButton.setBackground( c);
 41      else if (e.getSource()==NorthButton)
 42        NorthButton.setBackground( c);
 43      else if (e.getSource()==CenterButton)
 44        CenterButton.setBackground( c);
 45    }
 46    
 47  }
 48