2

我想做一个可拖动的弹出控件,它独立于其父Window,并且没有系统的标题栏。

我尝试为可拖动面板重写 JavaFx 示例,以便根据节点上的鼠标事件移动未装饰的舞台,请参见下面的代码。不幸的是,这似乎无法正常工作,它不流畅,并且在多个屏幕上不规律地在屏幕上跳跃。我读到其他一些人使用了类似的方法,但是这是目前唯一的方法还是有人找到了一个不错的解决方案?谢谢,

private Node makeDraggable(final Node node) {
    final DragContext dragContext = new DragContext();
    final Node wrapGroup = node;

    wrapGroup.addEventFilter(
            MouseEvent.MOUSE_PRESSED,
            new EventHandler<MouseEvent>() {
                public void handle(final MouseEvent mouseEvent) {

                        dragContext.mouseAnchorX = mouseEvent.getX();
                        dragContext.mouseAnchorY = mouseEvent.getY();
                        dragContext.initialTranslateX =
                                stage.getX();
                        dragContext.initialTranslateY =
                                stage.getY();
                    }

            });

    wrapGroup.addEventFilter(
            MouseEvent.MOUSE_DRAGGED,
            new EventHandler<MouseEvent>() {
                public void handle(final MouseEvent mouseEvent) {

                        stage.setX(
                                dragContext.initialTranslateX
                                        + mouseEvent.getX()
                                        - dragContext.mouseAnchorX);
                        stage.setY(
                                dragContext.initialTranslateY
                                        + mouseEvent.getY()
                                        - dragContext.mouseAnchorY);
                    dragContext.initialTranslateX =
                            stage.getX();
                    dragContext.initialTranslateY =
                            stage.getY();

                }
            });

    return node;
}
4

1 回答 1

2

我在制作时钟时使用了下面的示例方法。

我只在一个屏幕上测试过,因为我没有多个屏幕可用。

/** holder structure for drag delta amounts */
private static class Delta { double x, y; }

/** makes a stage draggable using a given node */
public static void makeDraggable(final Stage stage, final Node byNode) {
    final Delta dragDelta = new Delta();
    byNode.setOnMousePressed(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
            // record a delta distance for the drag and drop operation.
            dragDelta.x = stage.getX() - mouseEvent.getScreenX();
            dragDelta.y = stage.getY() - mouseEvent.getScreenY();
            byNode.setCursor(Cursor.MOVE);
        }
    });
    byNode.setOnMouseReleased(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
            byNode.setCursor(Cursor.HAND);
        }
    });
    byNode.setOnMouseDragged(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
            stage.setX(mouseEvent.getScreenX() + dragDelta.x);
            stage.setY(mouseEvent.getScreenY() + dragDelta.y);
        }
    });
    byNode.setOnMouseEntered(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
            if (!mouseEvent.isPrimaryButtonDown()) {
                byNode.setCursor(Cursor.HAND);
            }
        }
    });
    byNode.setOnMouseExited(new EventHandler<MouseEvent>() {
        @Override public void handle(MouseEvent mouseEvent) {
            if (!mouseEvent.isPrimaryButtonDown()) {
                byNode.setCursor(Cursor.DEFAULT);
            }
        }
    });
}
于 2012-11-17T10:49:16.560 回答