我想用线连接2个形状,同时按下鼠标。 正如doc所说,我不能使用按下拖动释放模型这样做,所以应该使用拖放方法 - 一切看起来都不错,但是如何禁用默认图标,这表示拖放内容可以放在目标上?在mouseMove上画线,我不想要这些默认图标。
我包含显示它的简单程序 - 只需运行我尝试将一个圆拖到另一个。
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package onreleased;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.input.*;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
/**
*
* @author mmk
*/
public class DaDIcons extends Application {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Hello World!");
final Label label = new Label("started");
label.setLayoutY(100);
Pane pane = new Pane();
Circle red = createCircle(40, 40, 30, Color.RED, label);
Circle blue = createCircle(160, 40, 30, Color.BLUE, label);
pane.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
System.out.println("foo");
}
});
pane.getChildren().addAll(red, blue, label);
primaryStage.setScene(new Scene(pane, 300, 250));
primaryStage.show();
}
private Circle createCircle(double x, double y, double r, final Color color, final Label statusLabel) {
final Circle circle = new Circle(x, y, r);
circle.setFill(color);
circle.setOnDragOver(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent event) {
statusLabel.setText(color.toString() + " over");
System.out.println("foo");
if (event.getGestureSource() != circle) {
event.acceptTransferModes(TransferMode.ANY);
}
}
});
circle.setOnDragDropped(new EventHandler<DragEvent>() {
@Override
public void handle(DragEvent arg0) {
statusLabel.setText(color.toString() + " dropped");
}
});
circle.setOnDragDetected(new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent arg0) {
circle.startDragAndDrop(TransferMode.ANY);
Dragboard db = circle.startDragAndDrop(TransferMode.ANY);
/*
* Put a string on a dragboard
*/
ClipboardContent content = new ClipboardContent();
content.putString("foo");
db.setContent(content);
statusLabel.setText(color.toString() + " xxxxxxxxxxxxxxxxxxxxxxx");
}
});
circle.setOnDragEntered(null);
return circle;
}
}
答案 0 :(得分:0)
您可以在拖放操作中管理Cursor
在circle.setOnDragDetected()
操作集circle.setCursor(Cursor.NONE);
和,
在circle.setOnDragDropped()
操作上将其反转circle.setCursor(Cursor.DEFAULT);
无需显示光标图标,您可以在d-n-drop时在鼠标坐标处显示图像。