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

Create a button and implement ActionListener interface using applet

  1. /*
  2. Button:
  3. ~~~~~~~
  4.         Button();
  5.         Button(String);
  6.     Methods:-
  7.         String getLabel();
  8.         void setActionCommand(String);
  9.         String getActionCommand();
  10.         void setLabel(String);
  11.      public synchronized void addActionListener(ActionListener);
  12.  
  13. */
  14.  
  15. import java.awt.event.*;
  16. import java.awt.*;
  17. import java.applet.*;
  18.  
  19. /*
  20. <applet CODE = "ButtonDemo" Height = 400 Width = 400>
  21. </applet>
  22. */
  23. public class ButtonDemo extends Applet implements ActionListener
  24. {
  25. Button b1, b2, b3;
  26. String msg = "";
  27. public void init()
  28. {
  29.     b1 = new Button("One");
  30.     b2 = new Button("Two");
  31.     b3 = new Button("Three");
  32.     add(b1);
  33.     add(b2);
  34.     add(b3);
  35.     b1.addActionListener(this);
  36.     b2.addActionListener(this);
  37.     b3.addActionListener(this);
  38. }
  39.  
  40. public void actionPerformed(ActionEvent be)
  41. {
  42.     if(be.getActionCommand().equals(b1.getLabel()))
  43.     {
  44.         msg = "First";
  45.         repaint();
  46.     }
  47.  
  48.     if(be.getActionCommand().equals(b2.getLabel()))
  49.     {
  50.         msg = "Second";
  51.         repaint();
  52.     }
  53.  
  54.     if(be.getActionCommand().equals(b3.getLabel()))
  55.     {
  56.         msg = "Three";
  57.         repaint();
  58.     }
  59.  
  60. }
  61.  
  62. public void paint(Graphics g)
  63. {
  64.     g.drawString(msg, 100, 100);
  65. }
  66.  
  67. }
Your rating: None Average: 3 (2 votes)
Share this
Anonymous's picture

trfdtdr

trfdtdr

Post new comment

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