MouseEventListener Explaination
MouseEventListener Explaination
Code:
java
Copy code
import java.awt.*;
import java.awt.event.*;
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:
The constructor initializes the Frame, sets its properties (like size and layout),
and makes it visible.
GUI Window Displayed:
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.