调用mousePressed时未调用mouseMoved

时间:2015-11-14 07:19:15

标签: java mouselistener mousemove

我的mouseMoved在调用mousePressed时根本不会被调用,但是当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;
} 

1 个答案:

答案 0 :(得分:2)

基于mousePressed处理程序中的代码,您尝试执行某种拖动矩形,但我无法确定。

mouseMoved消息将继续发生,直到您收到mousePressed个事件。收到mousePressed活动后,您将收到mouseDragged个活动,直到收到mouseReleased个活动为止。在此之后,您将再次开始接收mouseMoved个活动。

这是为了区分只需移动鼠标和使用其中一个按钮拖动。