当我删除它发生的对象时,JavaFX MouseEvent 继续
我花了一段时间才意识到当我关闭阻塞对话框时鼠标事件通过它们发生了什么,但我终于弄清楚了原因。我还是不知道有什么好的办法可以解决。
我有一个带有关闭按钮的自定义对话框(阻止鼠标)。当我单击关闭按钮时,我从场景中删除了对话框,但 JavaFx 仍在处理 MouseEvent,现在它发现取消按钮所在位置后面没有任何东西阻挡屏幕,因此该组件会收到 MouseEvent。当我看到他们按下取消并删除对话框时,如何使 mouseEvent 停止处理?或者,有没有办法让对话框在处理完 MouseEvent 后才被删除?
问题的示例代码:
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.input.MouseEvent;
import javafx.scene.control.Button;
var theScene:Scene;
var btn:Button;
Stage {
title: "Application title"
scene: theScene= Scene {
width: 500
height: 200
content: [
Rectangle{
width: bind theScene.width
height: bind theScene.height
onMouseClicked: function(e:MouseEvent):Void{
println("Rectangle");}
},
Button{
layoutX: 20 layoutY: 50
blocksMouse: true
text: "JustPrint"
action:function():Void{
println("JustPrint");}
},
btn = Button{
layoutX: 20 layoutY: 20
blocksMouse: true
text: "Cancel"
action:function():Void{
println("Cancel");
delete btn from theScene.content;}
},
]
}
}
当您按“JustPrint”时,您会得到:
JustPrint
当您按“取消”时,您会得到:
Cancel
Rectangle
It took me a while to realize what was going on with mouse events going through my blocking dialog boxes when I closed them, but I finally figured out why. I still don't know any good way to fix it.
I have a custom dialog box (that blocks the mouse) with a close button. When I click the close button, I remove the dialog box from the scene, but JavaFx is still processing the MouseEvent and now it finds that there is nothing blocking the screen behind where the cancel button was, so that component receives a MouseEvent. How do I make the mouseEvent stop processing when I see that they pressed cancel and remove the dialog box? Or, is there a way to make the removing of the dialog box not happen until after it is done processing the MouseEvent?
Example Code for the problem:
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.shape.Rectangle;
import javafx.scene.input.MouseEvent;
import javafx.scene.control.Button;
var theScene:Scene;
var btn:Button;
Stage {
title: "Application title"
scene: theScene= Scene {
width: 500
height: 200
content: [
Rectangle{
width: bind theScene.width
height: bind theScene.height
onMouseClicked: function(e:MouseEvent):Void{
println("Rectangle");}
},
Button{
layoutX: 20 layoutY: 50
blocksMouse: true
text: "JustPrint"
action:function():Void{
println("JustPrint");}
},
btn = Button{
layoutX: 20 layoutY: 20
blocksMouse: true
text: "Cancel"
action:function():Void{
println("Cancel");
delete btn from theScene.content;}
},
]
}
}
When you press "JustPrint" you get:
JustPrint
When you press "Cancel" you get:
Cancel
Rectangle
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您真的需要处理矩形上的
onMouseClicked
吗?如果您将其更改为onMousePressed
,您的问题就消失了。显然,该按钮需要完整的鼠标按下/鼠标释放序列才能执行操作,但删除(或隐藏)发生在第二个按钮被拦截之前。而且似乎鼠标释放事件足以触发
onMouseClicked
事件。因此,如果您的矩形对简单的
onMousePressed
感到满意,那么您有一个解决方法......Do you really need to handle
onMouseClicked
on the rectangle? If you change it toonMousePressed
, your problem is gone.Apparently, the button need the full mouse pressed/mouse released sequence to do the action but the deletion (or hiding) happens before the second one is intercepted. And it seems that a mouse release event is enough to trigger the
onMouseClicked
event.So if your rectangle is happy with a simple
onMousePressed
, you have a workaround...我认为你所看到的是一个时间问题。取消按钮在事件被完全处理之前被删除,因此事件随后被传递到矩形,因为取消按钮上的“blocksmouse”已被删除。试试这个:
I think what you are seeing is a timing issue. The cancel button is removed prior to the event being totally handled, thus the event is then passed to the Rect, because the "blocksmouse" on the Cancel button has been removed. Try this instead: