java - 在 Java FX 2.0 中滚动锚定节点时始终可见?

标签 java anchor javafx-2 scrollpane

我想让 ScrollPane 中存在一个锚定到顶部(固定在 Y 轴上)但能够在 X 轴上滚动的节点。 (在 Java-FX 2.0 中)

这可能吗?

最佳答案

您可以使用绑定(bind)来根据滚动条位置调整此特殊对象位置,请参阅下一个代码:

public class FancyScrollPane extends Application {

    @Override
    public void start(Stage primaryStage) {
        ScrollPane scrollPane = new ScrollPane();
        Pane content = new Pane();
        scrollPane.setContent(content);

        // adding background
        content.getChildren().add(new Rectangle(500, 500, Color.GREEN));

        Circle immovableObject = new Circle(30, Color.RED);
        content.getChildren().add(immovableObject);

        primaryStage.setScene(new Scene(scrollPane, 300, 300));
        primaryStage.show();

        // here we bind circle Y position
        immovableObject.layoutYProperty().bind(
                // to vertical scroll shift (which ranges from 0 to 1)
                scrollPane.vvalueProperty()
                    // multiplied by (scrollableAreaHeight - visibleViewportHeight)
                    .multiply(
                        content.heightProperty()
                            .subtract(
                                new ScrollPaneViewPortHeightBinding(scrollPane))));
    }

    // we need this class because Bounds object doesn't support binding 
    private static class ScrollPaneViewPortHeightBinding extends DoubleBinding {

        private final ScrollPane root;

        public ScrollPaneViewPortHeightBinding(ScrollPane root) {
            this.root = root;
            super.bind(root.viewportBoundsProperty());
        }

        @Override
        protected double computeValue() {
            return root.getViewportBounds().getHeight();
        }
    }

    public static void main(String[] args) { launch(); }
}

关于java - 在 Java FX 2.0 中滚动锚定节点时始终可见?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10238834/

相关文章:

java - 如何在android中将list.java添加到main.java中

java - 如何将数据库共享给不同的电脑以用于java桌面应用程序?

html - 单击 anchor 链接时如何停止跳转?

jquery - .hashchange 不跳转到 anchor 标记?

javafx-2 - 在 JavaFx 2.2 中截取场景或场景的一部分

java - 分配变量以显示数组索引时遇到问题

java - 读取和处理流

javascript - 需要 JavaScript 帮助才能平滑滚动

java - 从场景中提取内容

JavaFX。将多个带有图像的标签添加到 Pane