Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Create an applet and four buttons and arrange them using BorderLayout

  1. /*
  2. Layout Manager
  3. ~~~~~~~~~~~~~~
  4. BorderLayout
  5. ==========
  6. BorderLayout()
  7. BorderLayout(int horz, int vert)
  8. =========================================
  9. BorderLayout.EAST
  10. BorderLayout.WEST
  11. BorderLayout.NORTH
  12. BorderLayout.SOUTH
  13. BorderLayout.CENTER
  14. ======================================
  15. */
  16. import java.awt.*;
  17. import java.awt.event.*;
  18. import java.applet.*;
  19. /*
  20. <applet CODE = "BLDemo" width = 600 height = 400>
  21. </applet>
  22. */
  23. public class BLDemo extends Applet implements ActionListener
  24. {
  25. String msg="";
  26. Button b1, b2, b3, b4;
  27. public void init()
  28. {
  29.  setLayout(new BorderLayout());
  30.  b1 = new Button("EAST");
  31.  b2 = new Button("WEST");
  32.  b3 = new Button("NORTH");
  33.  b4 = new Button("SOUTH");
  34.  
  35.  add(b1, BorderLayout.EAST);
  36.  add(b2, BorderLayout.WEST);
  37.  add(b3, BorderLayout.NORTH);
  38.  add(b4, BorderLayout.SOUTH);
  39.  
  40.  b1.addActionListener(this);
  41.  b2.addActionListener(this);
  42.  b3.addActionListener(this);
  43.  b4.addActionListener(this);
  44. }
  45. public void actionPerformed(ActionEvent ae)
  46. {
  47.     msg = "";
  48.  if(ae.getSource() == b1)
  49.  {
  50.   msg = "EAST";
  51.  }
  52.    
  53.  if(ae.getSource() == b2)
  54.  {
  55.   msg = "WEST";
  56.  }
  57.  
  58.  if(ae.getSource() == b3)
  59.  {
  60.   msg = "NORTH";
  61.  }
  62.  if(ae.getSource() == b4)
  63.  {
  64.   msg = "SOUTH";
  65.  }
  66.  
  67.     repaint();
  68. }
  69. public void paint(Graphics g)
  70. {
  71.  g.drawString("You have clicked : " + msg, 50,100);
  72. }
  73. };
No votes yet
Share this

Post new comment

11 + 5 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.