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