简体   繁体   English

mouseEntered()和mouseMoved()之间的混淆

[英]Confusion between mouseEntered() and mouseMoved()

import java.awt.*;
import java.applet.*;
import java.awt.event.*;

public class MouseEvents extends Applet implements MouseListener, MouseMotionListener
{
    String msg = "";                        // I am not implementing those methods which
    int mouseX = 0, mouseY = 0;             // are not related to my question

    public void init()
    {
        addMouseListener(this);
        addMouseMotionListener(this);
    }

    public void mouseEntered(MouseEvent me)
    {
        mouseX = 0;
        mouseY = 10;
        msg = "Mouse Entered";
        repaint();
    }

    public void mouseMoved(MouseEvent me)
    {
        mouseX = me.getX();
        mouseY = me.getY();
        showStatus("Moving mouse at "+mouseX+", "+mouseY);
    }

    public void paint(Graphics g)
    {
        g.drawString(msg, mouseX, mouseY);
    }
}

Coordinates of my applet window: 我的小程序窗口的坐标:

Upper left corner - (0, 0) 左上角-(0,0)
Lower left corner - (0, 199) 左下角-(0,199)
Upper right corner - (349, 0) 右上角-(349,0)
Lower right corner - (349, 199) 右下角-(349,199)

What I expect: 我的期望:

  1. When mouse enter the applet window, a message "Mouse Entered" should be displayed at the coordinates (0, 10) 当鼠标进入小程序窗口时,应在坐标(0, 10) "Mouse Entered"处显示消息"Mouse Entered"

  2. When the mouse is moving, a message "Moving mouse at mouseX, mouseY" should be displayed in the status window. 当鼠标移动时,状态窗口"Moving mouse at mouseX, mouseY"显示一条消息"Moving mouse at mouseX, mouseY" Where mouseX and mouseY are the current coordinates of the mouse 其中mouseXmouseY是鼠标的当前坐标

What is actually happening: 实际情况:

The message "Mouse entered" is not being displayed at the coordinates (0, 10) , instead it is getting displayed at the initial coordinates from where the mouse enteres the applet window*** 消息"Mouse entered"没有"Mouse entered"没有显示在坐标(0, 10) 0,10 (0, 10) ,而是显示在鼠标进入applet窗口的初始坐标处***

For example, the mouse enters the applet window from between Lower left corner and Lower right corner , say (187, 199) , then the message "Mouse Entered" , instead of getting displayed at (0, 10) , is getting displayed at (187, 199) 例如,鼠标从Lower left cornerLower right corner之间进入小程序窗口,例如(187, 199) ,然后消息"Mouse Entered" (而不是显示在(0, 10)显示在(187, 199)

My question 我的问题

In spite of specifying mouseX = 0 and mouseY = 10 in mouseEntered() , why is the message "Mouse Entered" is getting displayed at the coordinates from where the mouse enters the applet window, but not at the coordinates (0, 10) ? 尽管在mouseEntered()中指定了mouseX = 0mouseY = 10 ,为什么mouseEntered()鼠标进入applet窗口的坐标处而不是在坐标(0, 10) mouseEntered()显示"Mouse Entered"消息?

public void mouseMoved(MouseEvent me)
{
    mouseX = me.getX();
    mouseY = me.getY();
    showStatus("Moving mouse at "+mouseX+", "+mouseY);
}

Your code in the mouseMoved section is being updated whenever you move the mouse. 每当您移动鼠标时,mouseMoved部分中的代码都会被更新。 So, the reason it is displaying mouseX and mouseY not as (0,10) is because you changed the value of mouseX and mouseY in your mouseMoved method. 因此,之所以不显示mouseX和mouseY为(0,10)是因为您在mouseMoved方法中更改了mouseX和mouseY的值。 Causing it to display coordinates at the last position of the mouse. 使它在鼠标的最后一个位置显示坐标。 Try creating a different variable to keep track of the position of the mouse. 尝试创建其他变量以跟踪鼠标的位置。

The viewing pane gets redrawn very often as you move the mouse over it. 当您将鼠标移到查看窗格上时,它经常会重绘。

You are overwriting the values of mouseX and mouseY here: 您将在此处覆盖mouseXmouseY的值:

public void mouseMoved(MouseEvent me)
{
    mouseX = me.getX();
    mouseY = me.getY();
    showStatus("Moving mouse at "+mouseX+", "+mouseY);
}

This causes the redrawing to occur at those coordinates when you move the mouse. 这将导致在您移动鼠标时在那些坐标处进行重绘。 If you want mouseEntered to not move, you can use a local variable, eg 如果不希望mouseEntered移动,则可以使用局部变量,例如

public void mouseMoved(MouseEvent me)
{
    int currentMouseX = me.getX();
    int currentMouseY = me.getY();
    showStatus("Moving mouse at "+currentMouseX+", "+currentMouseY);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM