Home > Java > Java Program to write an Applet with Event Handling – Q49

Java Program to write an Applet with Event Handling – Q49

January 25, 2009 Leave a comment Go to comments

Q49: Java Program to write an applet which does the event handling of keys by implementing keyListener Interface.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class Ap_SimpleKey extends Applet implements KeyListener {
	
	String msg = "";
	int x = 10,y = 20;
	
	public void init(){
		addKeyListener(this);
		requestFocus();
	}
	
	public void keyPressed(KeyEvent Ke){
		showStatus("Key Down");
		}
	
	public void keyReleased(KeyEvent Ke){
		showStatus("Key Up");
		}
	
	public void keyTyped(KeyEvent ke){
		msg += ke.getKeyChar();
		repaint();
	}
	
	public void paint(Graphics g){
		g.drawString(msg, x, y);
	}
}

… from College notes (BCA/MCA assignments):


Advertisement
Categories: Java Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: