【问题标题】:JavaFX MouseEvent continues when I remove the object it happened on当我删除它发生的对象时,JavaFX MouseEvent 继续
【发布时间】:2010-06-08 19:58:47
【问题描述】:

当我关闭它们时,我花了一段时间才意识到鼠标事件通过我的阻塞对话框发生了什么,但我终于弄清楚了原因。我仍然不知道有什么好的方法来解决它。

我有一个带有关闭按钮的自定义对话框(阻止鼠标)。当我单击关闭按钮时,我从场景中删除了对话框,但 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

【问题讨论】:

    标签: javafx mouseevent


    【解决方案1】:

    你真的需要处理矩形上的onMouseClicked 吗?如果你把它改成onMousePressed,你的问题就没有了。

    显然,按钮需要完整的鼠标按下/鼠标释放序列才能执行操作,但删除(或隐藏)发生在第二个被拦截之前。而且似乎一个鼠标释放事件就足以触发onMouseClicked 事件。 因此,如果您的矩形对简单的onMousePressed 感到满意,那么您有一个解决方法...

    【讨论】:

    • 我在原始程序中确实需要 onMouseClicked 作为背景(或者至少 onMouseReleased 会出现同样的问题),但看起来我可以删除按钮操作并让它响应 onMouseClicked问题就解决了。谢谢你的帮助。凯尔
    【解决方案2】:

    我认为您看到的是时间问题。取消按钮在事件被完全处理之前被删除,因此事件随后被传递给 Rect,因为取消按钮上的“blocksmouse”已被删除。试试这个:

         action:function():Void{
              println("Cancel");
              FX.deferAction( function() {
                delete btn from theScene.content;
              });
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-24
      • 2010-10-20
      • 2021-11-12
      • 1970-01-01
      相关资源
      最近更新 更多