java - mouseMoved 未调用,而 mousePressed 已调用

标签 java mouselistener mousemove

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

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;
} 

最佳答案

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

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

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

关于java - mouseMoved 未调用,而 mousePressed 已调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33706103/

相关文章:

java - Swift 中 HashSet Java 的等价物是什么?

java - 当调用 JPanel 中的一个鼠标监听器时,它们都会被调用吗?

java - MouseMotion 监听器不工作

javascript - 为什么原型(prototype)中的 mousedown 之后无法添加 mousemove 事件?

java - 对 Java Servlet 和 HTML 感到困惑

java - Spring AbstractAnnotationConfigDispatcherServletInitializer 未找到 WebApplicationContext : no ContextLoaderListener registered

c - X11 鼠标移动事件

javascript - 通过将鼠标悬停在 D3.js 中的节点上来显示多个节点的工具提示

java - 按行对二维数组进行排序

java - 检测点击的卡片