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

Create Checkbox controls, implement ItemListener and arrange them using FlowLayout

  1. /*
  2. Layout Manager
  3. ~~~~~~~~~~~~~~
  4. FlowLayout
  5. ==========
  6. FlowLayout()
  7. FlowLayout(int how)
  8. FlowLayout(int how, int horz,int vert)
  9. =========================================
  10. FlowLayout.LEFT
  11. FlowLayout.RIGHT
  12. FlowLayout.CENTER
  13. ======================================
  14. */
  15. import java.awt.*;
  16. import java.awt.event.*;
  17. import java.applet.*;
  18. /*
  19. <applet CODE = "FLDemo" width = 600 height = 400>
  20. </applet>
  21. */
  22. public class FLDemo extends Applet implements ItemListener
  23. {
  24.  String msg = "";
  25.  Checkbox Monitor, Processor, Keyboard, Mouse;
  26.  
  27.  public void init()
  28.  {
  29.   setLayout(new FlowLayout());
  30.   Monitor = new Checkbox("MONITOR");
  31.   Processor = new Checkbox("Processor P4");
  32.   Keyboard= new Checkbox("TVSE GOLD");
  33.   Mouse= new Checkbox("Logitech");
  34.  
  35.   add(Monitor);
  36.   add(Processor);
  37.   add(Mouse);
  38.   add(Keyboard);
  39.  
  40.   Monitor.addItemListener(this);
  41.   Processor.addItemListener(this);
  42.   Mouse.addItemListener(this);
  43.   Keyboard.addItemListener(this);
  44.  }
  45.  
  46.  public void itemStateChanged(ItemEvent ce)
  47.  {
  48.      msg = "";
  49.      if(Processor.getState())
  50.      {
  51.          msg += "Processor";
  52.      }
  53.      if(Monitor.getState())
  54.      {
  55.          msg += "Monitor";
  56.      }
  57.      if(Keyboard.getState())
  58.      {
  59.          msg += "KeyBoard";
  60.      }
  61.      if(Mouse.getState())
  62.      {
  63.          msg += "Mouse";
  64.      }
  65.      repaint();
  66.  }
  67.  
  68.  public void paint(Graphics g)
  69.  {
  70.   g.drawString("You have Purchased : " + msg, 50,100);
  71.  }
  72.  
  73. }
No votes yet
Share this

Post new comment

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