繁体   English   中英

在光标进入对象之前按下鼠标时,如何检测光标何时悬停在对象上?

[英]How do you detect when the cursor hovers over an object while the mouse is being pressed before the cursor enters the object?

我目前所处的情况是,我在屏幕上有一个圆圈,如果我先按住 LMB,然后将光标拖过它,则没有任何反应。 我试过查看我可以使用的不同 EventHandler,但我无法让它们工作。 这是我的代码。

public class DragTester extends Application
{
    public static Group group = new Group();
    public static Scene scene = new Scene(group, 500, 500);
    @Override
    public void start(Stage stage)
    {
        for(int i = 0; i < 3; i++){
            DragBox DB = new DragBox(i * 150 + 100);
        }
        stage.setScene(scene);
        stage.show();
    }
}
public class DragBox
{
    private Circle c = new Circle(0, 0, 25);
    public DragBox(double x){
        DragTester.group.getChildren().add(c);
        c.setLayoutX(x);
        c.setLayoutY(250);
        c.setOnDragDetected(new EventHandler<MouseEvent>(){
            @Override public void handle(MouseEvent e){
                c.setFill(Color.rgb((int)(Math.random() * 255), (int)(Math.random() * 255), (int)(Math.random())));
            }
        });
    }
}

您可以使用MOUSE_DRAG_ENTERED处理程序。 此事件仅在“完全按下-拖动-释放手势”期间触发,您需要在DRAG_DETECTED事件上启动该DRAG_DETECTED 有关更多信息,请参阅Javadoc

这是您为使用此事件而编写的示例。 在这个例子中,“完全按下-拖动-释放手势”是在场景中启动的:

import java.util.Random;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

public class DragTester extends Application {
    private Group group = new Group();
    private Scene scene = new Scene(group, 500, 500);
    
    private Random rng = new Random();

    @Override
    public void start(Stage stage) {
        for (int i = 0; i < 3; i++) {
            DragBox DB = new DragBox(i * 150 + 100);
        }
        scene.setOnDragDetected(e -> scene.startFullDrag());
        stage.setScene(scene);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }

    class DragBox {
        private Circle c = new Circle(0, 0, 25);

        public DragBox(double x) {
            group.getChildren().add(c);
            c.setLayoutX(x);
            c.setLayoutY(250);

            c.setOnMouseDragEntered(e -> 
                c.setFill(Color.rgb(rng.nextInt(256), rng.nextInt(256), rng.nextInt(256))));
        }
    }
}

暂无
暂无

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

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