停止将Node拖出窗格

时间:2018-11-14 18:35:08

标签: javafx

我有一个StackPane,可以在窗格中拖动它(窗格是堆栈窗格的父布局),但是现在我可以将其拖出窗格。当前,我无法找到一种方法来阻止StackPane从窗格中移出窗格。

当您按下StackPane时,我的事件处理程序:

EventHandler<MouseEvent> circleOnMousePressedEventHandler = 
            new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {

                currentStackPane  = ((StackPane)(t.getSource()));
                orgSceneX = t.getSceneX();
                orgSceneY = t.getSceneY();
                layoutX =  currentStackPane.getLayoutX();
                layoutY =  currentStackPane.getLayoutY();

            }
        };

我拖动StackPane时的事件处理程序:

EventHandler<MouseEvent> circleOnMouseDraggedEventHandler = 
            new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {


                double offsetX = t.getSceneX() - orgSceneX;
                double offsetY = t.getSceneY() - orgSceneY;
                currentStackPane.setTranslateX(offsetX);
                currentStackPane.setTranslateY(offsetY);
             }
        };

放开鼠标时的事件处理程序:

            new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {

                currentStackPane.setLayoutX(layoutX + ((StackPane)(t.getSource())).getTranslateX());
                currentStackPane.setLayoutY(layoutY + ((StackPane)(t.getSource())).getTranslateY());
                currentStackPane.setTranslateX(0);
                currentStackPane.setTranslateY(0);
            }
        };
        return circleOnMouseReleaseEventHandler;

}

有没有办法让堆栈窗格只能在窗格内拖动。

谢谢。

编辑:

我尝试过的事情:

我创建了此方法:

private boolean outSideParentBounds( Bounds childBounds, double newX, double newY) {

    Bounds parentBounds = centerPane.getLayoutBounds();

    //check if too left
    if( parentBounds.getMaxX() <= (newX + childBounds.getMaxX()) ) {
        return true ;
    }

    //check if too right
    if( parentBounds.getMinX() >= (newX + childBounds.getMinX()) ) {
        return true ;
    }

    //check if too down
    if( parentBounds.getMaxY() <= (newY + childBounds.getMaxY()) ) {
        return true ;
    }

    //check if too up
    if( parentBounds.getMinY() >= (newY + childBounds.getMinY()) ) {
        return true ;
    }

    return false;
}

我的拖动事件处理程序现在看起来像这样:

EventHandler<MouseEvent> circleOnMouseDraggedEventHandler = 
            new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {


                double offsetX = t.getSceneX() - orgSceneX;
                double offsetY = t.getSceneY() - orgSceneY;

                if( outSideParentBounds(currentStackPane.getLayoutBounds(), layoutX + ((StackPane)(t.getSource())).getTranslateX(), 
                        layoutY + ((StackPane)(t.getSource())).getTranslateY()) ) {  

                    return; 
                }

                currentStackPane.setTranslateX(offsetX);
                currentStackPane.setTranslateY(offsetY);

            }
        };

但是,一旦我将其拖出窗格,它似乎就卡住了窗格的边缘,并且您再也无法将其拖拽了。

1 个答案:

答案 0 :(得分:1)

我可以注意到的一个问题是您在计算outSideParentBounds时没有考虑offSet值。而且,只有在满足条件时,您才能更新转换值。相反,您必须始终在拖动处理程序中将转换值更新为所需值。

您的拖动处理程序应如下更改:

EventHandler<MouseEvent> circleOnMouseDraggedEventHandler = e -> {
            // Offset of drag
            double offsetX = e.getSceneX() - sceneX;
            double offsetY = e.getSceneY() - sceneY;

            // Taking parent bounds
            Bounds parentBounds = currentStackPane.getParent().getLayoutBounds();

            // Drag node bounds
            double currPaneLayoutX = currentStackPane.getLayoutX();
            double currPaneWidth = currentStackPane.getWidth();
            double currPaneLayoutY = currentStackPane.getLayoutY();
            double currPaneHeight = currentStackPane.getHeight();

            if ((currPaneLayoutX + offsetX > -1) && (currPaneLayoutX + offsetX < parentBounds.getWidth() - currPaneWidth)) {
                // If the dragNode bounds is within the parent bounds, then you can set the offset value.
                currentStackPane.setTranslateX(offsetX);
            } else if (currPaneLayoutX + offsetX < 0) {
                // If the sum of your offset and current layout position is negative, then you ALWAYS update your translate value to negative layout value
                // which makes the final layout position to 0 in mouse released event.
                currentStackPane.setTranslateX(-currPaneLayoutX);
            } else {
                // If your dragNode bounds are outside parent bounds,ALWAYS setting the translate value that fits your node at end.
                currentStackPane.setTranslateX(parentBounds.getWidth() - currPaneLayoutX - currPaneWidth);
            }

            if ((currPaneLayoutY + offsetY < parentBounds.getHeight() - currPaneHeight) && (currPaneLayoutY + offsetY > -1)) {
                currentStackPane.setTranslateY(offsetY);
            } else if (currPaneLayoutY + offsetY < 0) {
                currentStackPane.setTranslateY(-currPaneLayoutY);
            } else {
                currentStackPane.setTranslateY(parentBounds.getHeight() - currPaneLayoutY - currPaneHeight);
            }
        };

始终更新的原因是::拖动事件处理程序不会为您拖动的每个像素调用。假设您要将一个节点拖动500px。如果您打印offsetX / Y值,您会注意到offsetX / Y值并非始终是顺序的,或者不会为每个像素打印。它可能会跳一些值。它们可能会根据您拖动的速度而变化。因此,您可能会跳过所需的终点。

要验证这一点,一旦您熟悉上面的代码,请尝试注释代码的“其他”部分,并仅保留第一个if条件。尝试快速拖动节点,您会注意到您的节点将随机终止在所需端点的前面/后面。