// File: BorderLayout2.java import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class BorderLayout2 extends Applet implements ActionListener{ private Button EastButton, WestButton, NorthButton, SouthButton; public void init() { // instantiating the button objects EastButton = new Button("East"); WestButton = new Button("West"); NorthButton = new Button("North"); SouthButton = new Button("South"); // add action lisners for all buttons EastButton.addActionListener(this); WestButton.addActionListener(this); NorthButton.addActionListener(this); SouthButton.addActionListener(this); /* setting layout to BorderLayout with horizontal and vertical spacing of 10 pixels */ setLayout(new BorderLayout(10,10)); // order is not important here as we are specifying the positions add(EastButton,BorderLayout.EAST); add(WestButton,BorderLayout.WEST); add( NorthButton,BorderLayout.NORTH); add(SouthButton,BorderLayout.SOUTH); } public void actionPerformed( ActionEvent e) { Color c=new Color((float)Math.random(),(float)Math.random(),(float)Math.random()); if (e.getSource()==EastButton) EastButton.setBackground( c); else if (e.getSource()==WestButton) WestButton.setBackground( c); else if (e.getSource()==NorthButton) NorthButton.setBackground( c); else if (e.getSource()==SouthButton) SouthButton.setBackground( c); } }