1 // File: BorderLayout3.java 2 3 import java.applet.Applet; 4 import java.awt.*; 5 import java.awt.event.*; 6 7 8 public class BorderLayout3 extends Applet implements ActionListener{ 9 private Button EastButton, 10 SouthButton, 11 CenterButton; 12 13 public void init() { 14 // instantiating the button objects 15 EastButton = new Button("East"); 16 SouthButton = new Button("South"); 17 CenterButton = new Button("Center"); 18 19 // add action lisners for all buttons 20 EastButton.addActionListener(this); 21 SouthButton.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()); 28 29 // order is not important here as we are specifying the positions 30 31 add(EastButton,BorderLayout.EAST); 32 add(SouthButton,BorderLayout.SOUTH); 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()==SouthButton) 42 SouthButton.setBackground( c); 43 else if (e.getSource()==CenterButton) 44 CenterButton.setBackground( c); 45 } 46 47 } 48 49 50 51 52 53