簡體   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