我想用线连接2个形状,同时按下鼠标。正如文档所说,我不能使用按下-拖动-释放模型来做到这一点,所以应该使用拖放方法 - 一切看起来都很好,但是如何禁用默认图标,这表示允许拖放内容放在目标上? 在 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;
}
}