我需要将一个矩形节点拖到另一个节点中。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 方法。
我错过了什么?我对拖放事件的数量感到非常困惑。