ScalaFX 8将节点拖放到另一个节点

时间:2015-07-04 20:41:40

标签: scala javafx drag-and-drop scalafx

我需要将一个矩形节点拖到另一个节点中。 ScalaFX应该基于JavaFX,所以这应该类似,对吗?由于没有太多的ScalaFX文档,我试图找到一些JavaFX示例,我已经阅读了很多这些示例,但它们都没有为我工作:(

据我了解,应该有3种类型的拖放操作:

  • 单个节点上的简单拖放
  • 完全拖放(我应该使用此节点在节点之间进行交互)
  • 在应用程序之间拖放文件和数据

我最终得到了这个简单的课程:

class GameBoardRectangle(val coordinates: Coordinates) extends Rectangle
  {
    width = 32
    height = 32
    stroke = Color.BLACK

    var dragDelta: Point2D = new Point2D(0,0)

    onMousePressed = new EventHandler[input.MouseEvent] {
      override def handle(event: input.MouseEvent): Unit = {
        dragDelta = new Point2D(layoutX.toDouble - event.getSceneX,
          layoutY.toDouble - event.getSceneY)
          //event.setDragDetect(true)
          rect.setMouseTransparent(true)
      }
    }

    onMouseDragged = new EventHandler[input.MouseEvent] {
      override def handle(event: input.MouseEvent): Unit = {
        layoutX = event.getSceneX + dragDelta.x
        layoutY = event.getSceneY + dragDelta.y
       // println("OnMouseDragged")
        rect.setMouseTransparent(true)
        event.consume()
      }
    }

    val rect = this

    onDragDetected = new EventHandler[MouseEvent] {
      override def handle(event: MouseEvent): Unit = {
       // var db: Dragboard = rect.startDragAndDrop(TransferMode.LINK)
        var clibpoard = new ClipboardContent()
        //event.

        println("OnDragDetected")
        rect.startFullDrag()
      //  event.consume()
      }
    }

    onMouseDragEntered = new EventHandler[MouseDragEvent] {
      override def handle(event: MouseDragEvent): Unit = {
        println("onMouseDragEntered")
      }
    }

    onMouseDragExited = new EventHandler[MouseDragEvent] {
      override def handle(event: MouseDragEvent): Unit = {
        println("onMouseDragExited")
      }
    }

    onMouseDragOver = new EventHandler[MouseDragEvent] {
      override def handle(event: MouseDragEvent): Unit = {
        println("onMouseDragExited")

      }
    }

    onMouseDragReleased = new EventHandler[MouseDragEvent] {
      override def handle(event: MouseDragEvent): Unit = {
        println ("onMouseDragReleased")
      }
    }

    onDragDropped = new EventHandler[DragEvent] {
      override def handle(event: DragEvent): Unit = {
        println("OnDragDropped")
      }
    }

    onDragDone = new EventHandler[DragEvent] {
      override def handle(event: DragEvent): Unit = {
        println("OnDragDone")
      }
    }

    onDragOver = new EventHandler[DragEvent] {
      override def handle(event: DragEvent): Unit = {
        println("OnDragOver")
        //event.acceptTransferModes(TransferMode.LINK)
      }
    }

    onDragEntered = new EventHandler[DragEvent] {
      override def handle(event: DragEvent): Unit = {
        event.acceptTransferModes(TransferMode.LINK)
        println("OnDragEnetered")
      }
    }
  }
将一个节点拖到另一个节点之后

并从控制台输出:

OnDragDetected
onMouseDragEntered
onMouseDragExited

没有触发OnDragOver方法。

我错过了什么?我对Drag and Drop事件的数量感到很困惑。

0 个答案:

没有答案