0% found this document useful (0 votes)
4 views3 pages

MouseEventListener Explaination

Uploaded by

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

MouseEventListener Explaination

Uploaded by

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

Here is the code and explanation for the MouseListener example:

Code:
java
Copy code
import java.awt.*;
import java.awt.event.*;

public class MouseListenerExample extends Frame implements MouseListener {


Label l;

// Constructor to initialize the frame and components


MouseListenerExample() {
// Add MouseListener to the Frame
addMouseListener(this);

// Create a Label to display mouse events


l = new Label();
l.setBounds(20, 50, 100, 20); // Set label's position and size
add(l);

// Set Frame properties


setSize(300, 300); // Frame size: 300x300
setLayout(null); // No layout manager; using absolute positioning
setVisible(true); // Make the frame visible
}

// Called when the mouse is clicked


public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}

// Called when the mouse enters the frame


public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}

// Called when the mouse exits the frame


public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}

// Called when the mouse button is pressed


public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}

// Called when the mouse button is released


public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}

// Main method to run the application


public static void main(String[] args) {
new MouseListenerExample();
}
}
Explanation:
Imports:
import java.awt.*: Provides classes for GUI components (Frame, Label).
import java.awt.event.*: Contains interfaces and classes for event handling
(MouseListener, MouseEvent).
Class Declaration:
The class MouseListenerExample extends Frame to create a GUI window and implements
the MouseListener interface to handle mouse events.
Constructor:
MouseListener Setup:

addMouseListener(this) attaches the MouseListener to the frame (this refers to the


current instance of MouseListenerExample).
Label Setup:

A Label is created to display messages like "Mouse Clicked" or "Mouse Entered."


setBounds(20, 50, 100, 20) positions the label at (20, 50) with a size of 100x20.
Frame Setup:

setSize(300, 300) creates a 300x300 window.


setLayout(null) disables the layout manager for absolute positioning.
setVisible(true) makes the frame visible.
MouseListener Methods:
mouseClicked(MouseEvent e):

Called when the mouse is clicked (pressed and released).


Updates the label with "Mouse Clicked."
mouseEntered(MouseEvent e):

Called when the mouse pointer enters the frame.


Updates the label with "Mouse Entered."
mouseExited(MouseEvent e):

Called when the mouse pointer exits the frame.


Updates the label with "Mouse Exited."
mousePressed(MouseEvent e):

Called when the mouse button is pressed.


Updates the label with "Mouse Pressed."
mouseReleased(MouseEvent e):

Called when the mouse button is released.


Updates the label with "Mouse Released."
Output Behavior:
Mouse Clicked: When you click within the frame, the label shows "Mouse Clicked."
Mouse Entered: When you move the mouse pointer into the frame, the label shows
"Mouse Entered."
Mouse Exited: When you move the mouse pointer out of the frame, the label shows
"Mouse Exited."
Mouse Pressed: When you press a mouse button, the label shows "Mouse Pressed."
Mouse Released: When you release the mouse button, the label shows "Mouse
Released."
Key Points:
Simplicity: Uses Frame and Label for a lightweight GUI example.
Event Handling: Implements MouseListener to handle mouse events.
Label Updates: Dynamically updates the label based on the mouse action.
You can compile and run this program to see the corresponding messages in the label
as you perform mouse actions.

Explanation of new MouseListenerExample();


The line new MouseListenerExample(); is the entry point for creating an instance of
the MouseListenerExample class. Let's break it down:

1. Object Creation:
new keyword:
Allocates memory and constructs an object of the MouseListenerExample class.
Invokes the class constructor to initialize the object.
2. Constructor Call:
When new MouseListenerExample(); is executed, the no-argument constructor of the
class is invoked:
java
Copy code
MouseListenerExample() {
addMouseListener(this); // Attaches the MouseListener to this Frame
l = new Label(); // Creates a new Label
l.setBounds(20, 50, 100, 20); // Sets the position and size of the Label
add(l); // Adds the Label to the Frame
setSize(300, 300); // Sets the Frame size to 300x300
setLayout(null); // Disables layout manager (absolute positioning)
setVisible(true); // Makes the Frame visible
}
3. What Happens Internally:
When new MouseListenerExample(); is called:

Object Instantiation:

JVM creates an object of the MouseListenerExample class in memory.


Constructor Execution:

The constructor initializes the Frame, sets its properties (like size and layout),
and makes it visible.
GUI Window Displayed:

A 300x300 GUI window (frame) appears on the screen.


The Label is added to the frame, and mouse events (click, enter, exit, press,
release) are registered.

Why Don't We Store the Object in a Variable?


The object is not stored in a variable because:

The program does not need to reference the object later. It only needs to display
the frame.
By using new directly, the constructor executes, and the object is used immediately
to display the GUI.
If you needed to interact with the frame later, you could assign it to a variable:

java
Copy code
MouseListenerExample example = new MouseListenerExample();
But in this case, storing the reference isn't necessary.

You might also like