1 // File: BorderLayout2.java 2 3 import java.applet.Applet; 4 import java.awt.*; 5 import java.awt.event.*; 6 7 public class BorderLayout2 extends Applet implements ActionListener{ 8 private Button EastButton, WestButton, NorthButton, SouthButton; 9 10 public void init() { 11 // instantiating the button objects 12 EastButton = new Button("East"); 13 WestButton = new Button("West"); 14 NorthButton = new Button("North"); 15 SouthButton = new Button("South"); 16 17 18 // add action lisners for all buttons 19 EastButton.addActionListener(this); 20 WestButton.addActionListener(this); 21 NorthButton.addActionListener(this); 22 SouthButton.addActionListener(this); 23 24 /* setting layout to BorderLayout 25 with horizontal and vertical spacing of 10 pixels */ 26 setLayout(new BorderLayout(10,10)); 27 28 // order is not important here as we are specifying the positions 29 30 add(EastButton,BorderLayout.EAST); 31 add(WestButton,BorderLayout.WEST); 32 add( NorthButton,BorderLayout.NORTH); 33 add(SouthButton,BorderLayout.SOUTH); 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()==WestButton) 42 WestButton.setBackground( c); 43 else if (e.getSource()==NorthButton) 44 NorthButton.setBackground( c); 45 else if (e.getSource()==SouthButton) 46 SouthButton.setBackground( c); 47 } 48 49 } 50 51