In: Computer Science
Consider the following Java program. Describe what it does in response to specific operations of the mouse, and how it does it. (You are encouraged to run the program for yourself to test its behavior. Then read through the program carefully to understand how that behavior arises.)
import java.awt.event.*;
import javax.swing.*;
public class MouseWhisperer extends JFrame implements MouseListener
{
MouseWhisperer() {
super("COME
CLOSER");
setSize(300,100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
setTitle("OUCH"); }
public void mousePressed(MouseEvent e) {
setTitle("LET GO"); }
public void mouseReleased(MouseEvent e) {
setTitle("WHEW"); }
public void mouseEntered(MouseEvent e) {
setTitle("I SEE YOU"); }
public void mouseExited(MouseEvent e) {
setTitle("COME CLOSER"); }
public static void main(String[] args) { new
MouseWhisperer(); }
}
import java.awt.event.*;
import javax.swing.*;
public class MouseWhisperer extends JFrame implements MouseListener
{
MouseWhisperer() {
super("COME
CLOSER"); // The title of the window at the start
setSize(300,
100);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
addMouseListener(this);
setVisible(true);
}
// this method gets called when mouse is
clicked (after release of button)
public void mouseClicked(MouseEvent e)
{
setTitle("OUCH");
}
// This gets called as soon as we start
clicking the mouse
public void mousePressed(MouseEvent e)
{
setTitle("LET
GO");
}
// Gets called when mouse button is
released
public void mouseReleased(MouseEvent e)
{
setTitle("WHEW");
}
// When mouse comes from outside the panel
to inside the panel
public void mouseEntered(MouseEvent e)
{
setTitle("I SEE
YOU");
}
// Gets called when mouse goes out of the
panel
public void mouseExited(MouseEvent e)
{
setTitle("COME
CLOSER");
}
/*
* Lifecycle:
* 1. MouseEntered (Comes inside
panel)
* 2. mousePressed
* 3. mouseReleased
* 4. mouseClicked
* 5. mouseExited (Leave panel)
*/
public static void main(String[] args)
{
new
MouseWhisperer();
}
}
**************************************************
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this
question if it helps you. by Doing that, You will help other
students, who are facing similar issue.