java fx控制鼠标,如何在JavaFX中获取鼠标的位置?

本文介绍了如何在JavaFX中获取鼠标的位置,强调了通过添加鼠标事件处理器到相应的组件来获取节点、场景和屏幕坐标。同时,文章提醒避免使用AWT的MouseInfo,并提供了实现拖动无装饰JavaFX舞台的示例代码。
摘要由CSDN通过智能技术生成

I am a beginner in java(fx).

How do you get the mouse location in x and y in JavaFX? I tried using AWT's MouseInfo(also imported it), but it's not working. I also saw the code for it in Ensembles(that dragging the ball-window in "advanced stage", that's what I need to do, drag my undecorated JavaFX stage), but it also doesn't work. I am using FXML with controller, and I guess that's the main problem. Should I switch back to the single-file simple JavaFX? I know FXML is better for laying out the UI, but I can't get many of such codes to work. Or do I need some other sort of code for my controller? Please give proper codes with comments wherever possible.

If you need a bit of my code to inspect, feel free to ask.

解决方案

There are a few items in your question - I'll tackle them one at a time.

How do you get the mouse location in x and y in JavaFX?

Add a mouse event handler to the appropriate JavaFX component that you want to track the mouse location in. A JavaFX mouse event will report multiple different kinds of co-ordinates. The x and y co-ordinates are relative to the top left corner of the node whose location is being monitored. The sceneX and sceneY co-ordinates are relative to the scene's top left 0,0 co-ordinates. The screenX and screenY co-ordinates are relative to the top left 0,0 co-ordinates of the current screen.

These co-ordinates are documented in the MouseEvent documentation. There is extra information in u

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值