mouseMoved 未调用,而 mousePressed 已调用

Are*_*yan 2 java mousemove mouselistener

我的在被调用mouseMoved时根本不会被调用,但在没有被调用时它会正常调用。如果我在按下鼠标按钮时移动鼠标,则不会被调用。mousePressedmousePressedmouseMoved

package src.game.main.gui_hud;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.geom.RoundRectangle2D;

import javax.swing.SwingUtilities;

import src.game.main.Game;

public class Slider {

private Color lc,hc,fc;
private int x,y;
private int w,h;
private Runnable change;
private int lineY;
private double value = 100;
private volatile boolean canMove;
public Slider(Color bgColor,Color filledColor,Color handlerColor,Runnable onValueChange,int x,int y,int w,int h,int lineY) {
    setLc(bgColor);
    setHc(handlerColor);
    setFc(filledColor);
    change = onValueChange;
    this.x = x;
    this.y = y;
    this.w = w;
    this.h = w;
    this.lineY = lineY;
}



public void render(Graphics gt) {
    Graphics2D g = (Graphics2D) gt.create(x, y, w, h);
    g.setColor(getLc());
    g.fillRoundRect(10, y/2-lineY, w-10, lineY, 10, 10);
    g.setColor(getFc());
    g.fillRoundRect(10, y/2-lineY, (int) ((value*w)/100)-10, lineY, 10, 10);
    g.setColor(getHc());
    g.fillRoundRect((int)((value*w)/100)-6, y/2-20, 5, 30, 10, 10);

}

public void tick() {
    value = Game.clamp(value, 0, 100);
    System.out.println(canMove);
}



public void mousePressed(MouseEvent e) {

    Point p = e.getPoint();

    if (new RoundRectangle2D.Double(x+ ((int)((value*w)/100)-6), y + (y/2-20), 5, 30, 10, 10).contains(p)) {

        canMove = SwingUtilities.isLeftMouseButton(e);

    }
}


public void mouseReleased(MouseEvent e) {
    Point p = e.getPoint();

    canMove = false;

}

public void mouseMoved(MouseEvent e) {
    System.out.println(e.getX());
    Point p = e.getPoint();


    if(canMove)     System.out.println("LOL");



}











public Color getHc() {
    return hc;
}

public Slider setHc(Color hc) {
    this.hc = hc;
    return this;
}

public Color getLc() {
    return lc;
}

public Slider setLc(Color lc) {
    this.lc = lc;
    return this;
}



public int getX() {
    return x;
}



public Slider setX(int x) {
    this.x = x;
    return this;
}



public int getY() {
    return y;
}



public Slider setY(int y) {
    this.y = y;
    return this;
}



public int getW() {
    return w;
}



public Slider setW(int w) {
    this.w = w;
    return this;
}



public int getH() {
    return h;
}



public Slider setH(int h) {
    this.h = h;
    return this;
}



public double getValue() {
    return value;
}

public Slider setValue(double v) {
    this.value = v;
    return this;
}



public Color getFc() {
    return fc;
}



public Slider setFc(Color fc) {
    this.fc = fc;
    return this;
} 
Run Code Online (Sandbox Code Playgroud)

Pet*_*esh 5

根据处理程序中的代码mousePressed,您正在尝试执行某种拖动矩形,尽管我不能确定。

这些mouseMoved消息将持续发生,直到您收到mousePressed事件为止。收到mousePressed事件后,您将开始接收mouseDragged事件,直到收到事件mouseReleased。之后,您将mouseMoved再次开始接收事件。

这是为了区分仅移动鼠标和按下其中一个按钮进行拖动。