JavaFX 2:可拖动的弹出工具窗口?

时间:2012-11-17 09:47:17

标签: popup draggable javafx-2 stage

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

我尝试重写可拖动面板的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;
}

1 个答案:

答案 0 :(得分:2)

我在制作clock时使用了以下示例方法。

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

/** 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);
            }
        }
    });
}