在Java FX 2.0中滚动锚定节点时始终可见? [英] Always visible when scrolled anchored Node in Java FX 2.0?

查看:93
本文介绍了在Java FX 2.0中滚动锚定节点时始终可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to make a node present in a ScrollPane anchored to the top (fixed on the Y axis) but capable of scrolling in the X axis. (In Java-FX 2.0)

这可能吗?

推荐答案

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

You can use binding to adjust this special object position based on scrollbar position, see next code:

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 FX 2.0中滚动锚定节点时始终可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆