Computer Science Related Others Courses AvailableThe Best Codder.blogspot.com

EventHandling in Applet

EventHandling-Applet

 

EventHandling in Applet

As we perform event handling in AWT or Swing, we can perform it in applet also. Let's see the simple example of event handling in applet that prints a message by click on the button.

Example of EventHandling in applet:

  1. import java.applet.*;  
  2. import java.awt.*;  
  3. import java.awt.event.*;  
  4. public class EventApplet extends Applet implements ActionListener{  
  5. Button b;  
  6. TextField tf;  
  7.   
  8. public void init(){  
  9. tf=new TextField();  
  10. tf.setBounds(30,40,150,20);  
  11.   
  12. b=new Button("Click");  
  13. b.setBounds(80,150,60,50);  
  14.   
  15. add(b);add(tf);  
  16. b.addActionListener(this);  
  17.   
  18. setLayout(null);  
  19. }  
  20.   
  21.  public void actionPerformed(ActionEvent e){  
  22.   tf.setText("Welcome");  
  23.  }   
  24. }  
In the above example, we have created all the controls in init() method because it is invoked only once.

myapplet.html

  1. <html>  
  2. <body>  
  3. <applet code="EventApplet.class" width="300" height="300">  
  4. </applet>  
  5. </body>  
  6. </html>  
download this example.

Post a Comment

© JAVA. The Best Codder All rights reserved. Distributed by