[英]Detecting Drag and Drop event on Java FX?
I got some Circle
objects which I want to join with a Line
when dragging from the circles. 我有一些
Circle
对象,我想在从圆圈拖动时加入Line
。
I started with this code in order to test being and end drag and drop coordinates: 我开始使用此代码来测试存在并结束拖放坐标:
Circle nodo = new Circle(15.0);
nodo.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
coordenadas[0] = event.getX();
coordenadas[1] = event.getY();
}
});
nodo.setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
coordenadas[2] = event.getX();
coordenadas[3] = event.getY();
System.out.println(coordenadas);
}
});
When I try to drag a Circle
nothing is being printed out to console when I drop the mouse. 当我尝试拖动
Circle
时,当我放下鼠标时,没有任何东西被打印到控制台。 What's the correct approach and how should I being drawing a Line
on dragging. 什么是正确的方法,我应该如何在拖动上画一条
Line
。
For detecting drag and drop, follow this approach: 要检测拖放,请遵循以下方法:
//
// ON MOUSE PRESSED
// ----------------------
nodo.onMousePressedProperty().set(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
event.setDragDetect(true);
nodo.setEffect(new DropShadow(10.0, Color.BLACK));
}
});
//
// ON MOUSE DRAGGED
// ----------------------
nodo.onMouseDraggedProperty().set(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
nodo.setEffect(new DropShadow(10.0, Color.BLUEVIOLET));
/*
final Circle indicator = new Circle(3);
indicator.setStroke(Color.BLUEVIOLET);
indicator.setCenterX(x);
indicator.setCenterY(y);
*/
}
});
Change the commented part for whatever you need. 根据需要更改注释部分。 In your case, save the initial event coordinates (X,Y) and, in the "dragged" part, use them as initial coordinates for your line.
在您的情况下,保存初始事件坐标(X,Y),并在“拖动”部分中,将它们用作线的初始坐标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.