0% found this document useful (0 votes)
28 views6 pages

Event Handling: Key Listener

The document provides examples of Java event handling using KeyListener and MouseListener interfaces. It includes code snippets for handling key events like key pressed, released, and typed, as well as mouse events such as mouse clicked, entered, exited, pressed, and released. Each example creates a simple GUI application that updates a label based on user interactions with the keyboard or mouse.

Uploaded by

bhargav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views6 pages

Event Handling: Key Listener

The document provides examples of Java event handling using KeyListener and MouseListener interfaces. It includes code snippets for handling key events like key pressed, released, and typed, as well as mouse events such as mouse clicked, entered, exited, pressed, and released. Each example creates a simple GUI application that updates a label based on user interactions with the keyboard or mouse.

Uploaded by

bhargav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 6

EVENT HANDLING:

KEY LISTENER

1.import java.awt.*;

import java.awt.event.*;

public class KeyListenerExample extends Frame implements KeyListener{

Label l;

TextArea area;

KeyListenerExample(){

l=new Label();

l.setBounds(20,50,100,20);

area=new TextArea();

area.setBounds(20,80,300, 300);

area.addKeyListener(this);

add(l);add(area);

setSize(400,400);

setLayout(null);

setVisible(true);

public void keyPressed(KeyEvent e) {

l.setText("Key Pressed");

public void keyReleased(KeyEvent e) {

l.setText("Key Released");
}

public void keyTyped(KeyEvent e) {

l.setText("Key Typed");

public static void main(String[] args) {

new KeyListenerExample();

OUTPUT:

1.KEY TYPED EVENT:

2.KEY RELEASED EVENT:


2.MOUSE LISTENER:

import java.awt.*;

import java.awt.event.*;

public class MouseListenerExample extends Frame implements MouseListener{

Label l;

MouseListenerExample(){

addMouseListener(this);

l=new Label();

l.setBounds(20,50,100,20);

add(l);

setSize(300,300);

setLayout(null);

setVisible(true);

public void mouseClicked(MouseEvent e) {

l.setText("Mouse Clicked");

}
public void mouseEntered(MouseEvent e) {

l.setText("Mouse Entered");

public void mouseExited(MouseEvent e) {

l.setText("Mouse Exited");

public void mousePressed(MouseEvent e) {

l.setText("Mouse Pressed");

public void mouseReleased(MouseEvent e) {

l.setText("Mouse Released");

public static void main(String[] args) {

new MouseListenerExample();

OUTPUT:

1.MOUSE ENTERED EVENT:


2.MOUSE RELEASED EVENT:

3.MOUSE PRESSED EVENT:


4.MOUSE CLICKED EVENT:

You might also like