如何在JAVA FX中选择多种形状(拖放)?

时间:2016-07-20 03:30:07

标签: javafx-8

enter image description here

我无法选择矩形和文本字段等多个对象。我需要选择,拖放多个形状,如word文档...........任何人都应该回答......

public SelectionModel selectionModel;
public MouseHandler(SelectionModel selectionModel2, SelectionModel selectionModel     ) {
    // TODO Auto-generated constructor stub
    this.selectionModel = selectionModel;
}

private class DragContext {
    double x;
    double y;
}

public void makeDraggable( final Node node) {
    final DragContext dragDelta = new DragContext();

    node.setOnMousePressed(mouseEvent -> {

        // TODO: add shift & ctrl check to add/remove nodes to selection
        selectionModel.clear();

        Group root=new Group();
        selectionModel.add(node);

        dragDelta.x = node.getTranslateX() - mouseEvent.getSceneX();
        dragDelta.y = node.getTranslateY() - mouseEvent.getSceneY();     

        mouseEvent.consume();            
    });

    node.setOnMouseDragged(mouseEvent -> {       
        node.setTranslateX(mouseEvent.getSceneX() + dragDelta.x);
        node.setTranslateY(mouseEvent.getSceneY() + dragDelta.y);
    });

    node.setOnMouseReleased(mouseEvent -> {
        fixPosition(node);
    });
}

private void fixPosition( Node node) {

    double x = node.getTranslateX();
    double y = node.getTranslateY();

    node.relocate(node.getLayoutX() + x, node.getLayoutY() + y);

    node.setTranslateX(0);
    node.setTranslateY(0);
}
}

请使用代码解释您的答案

1 个答案:

答案 0 :(得分:0)

在你看到的移动节点方法中你必须循环选定的节点并同时将你的翻译应用到所有节点。

如果这没有帮助请更具体:)