简体   繁体   English

使用JavaFX和Event获取鼠标位置

[英]Getting a mouse position with JavaFX and Event

I'm trying to get the position of a mouse click in my JavaFX application. 我正在尝试在JavaFX应用程序中单击鼠标的位置。 I have an EventHandler as so: 我有一个EventHandler:

EventHandler<Event> mouseHandler = new EventHandler<Event>() {
    @Override
    public void handle(Event event) {
        String eType = event.getEventType().toString();
        if(eType.equals("MOUSE_PRESSED")){
            //mouse position
        }
    }
};

which is called on mouse press, release and drag. 在鼠标按下,释放和拖动时调用。 I'm unable to get the position of the mouse from Event and when I try and change EventHandler<Event> to EventHandler<MouseEvent> I get the message Type paremeter 'java.awt.event.MouseEvent' is not within it's bounds; should extend to 'javafx.event.Event' 我无法从Event获取鼠标的位置,并且尝试将EventHandler<Event>更改为EventHandler<MouseEvent> ,得到消息Type paremeter 'java.awt.event.MouseEvent' is not within it's bounds; should extend to 'javafx.event.Event' Type paremeter 'java.awt.event.MouseEvent' is not within it's bounds; should extend to 'javafx.event.Event' . Type paremeter 'java.awt.event.MouseEvent' is not within it's bounds; should extend to 'javafx.event.Event'

How can I move this over to MouseEvents? 如何将其移至MouseEvents?

You need to use the JavaFx MouseEvent . 您需要使用JavaFx MouseEvent

Currently you are trying to use the Java.awt MouseEvent 当前,您正在尝试使用Java.awt MouseEvent

Try importing only Java libraries from Javafx and not awt so you can avoid having the wrong type. 尝试仅从Javafx而不是awt导入Java库,这样可以避免输入错误的类型。

Once you fix that, the methods getX() and getY() should give you the position. 解决此问题后,方法getX()getY()应该会为您提供位置。 (Or depending on what you want that position relative to, getScreenX() or getSceneX() might be what you want.) (或者取决于您想要的位置相对于什么, getScreenX()getSceneX()可能就是您想要的。)

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

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