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

Java Program to write an Applet with Event Handling – Q49


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):


Advertisements
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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: