0% found this document useful (0 votes)
79 views

JAVA

This document discusses mouse events in Java. It explains that the MouseListener interface notifies when the mouse state changes and has five methods. It provides an example program that implements MouseListener on a Frame to detect and display messages for different mouse events like click, enter, exit, press, and release. The output shows a message for each mouse event on the GUI.

Uploaded by

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

JAVA

This document discusses mouse events in Java. It explains that the MouseListener interface notifies when the mouse state changes and has five methods. It provides an example program that implements MouseListener on a Frame to detect and display messages for different mouse events like click, enter, exit, press, and release. The output shows a message for each mouse event on the GUI.

Uploaded by

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

By: Manan Preet Singh

A2305317100
Lakshay
Kumar
A2305317099

Java Programming
PROGRAM TO HANDLE MOUSE EVENTS
Mouse Listener in Java

 The Java MouseListener is notified whenever


you change the state of mouse. It is notified
against MouseEvent. The MouseListener
interface is found in java.awt.event
package. It has five methods.
Action Event

 Components such as the Button and JButton fire off ActionEvents to


indicate some kind of component-defined action. For example, the
Button fires off an ActionEvent whenever the user presses it.
 The entire point of an event is to inform a listener that something has
happened to a component in the GUI.
 An event includes all of the information that a listener needs to
figure out what happened and to whom it happened (the what
and who of the event).
 An event must give enough information to fully describe itself. That
way, a listener can figure out what exactly happened and respond
in a meaningful way.
Example MouseListener Programme
 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

Whenever A Mouse event occurs, whether the


mouse is clicked, moved, entered or released
from the GUI(Graphical User Interface), a
message regarding the same is displayed.
Thankyou For Being
A Patient Listener

You might also like