JavaFX:缩放和滚动无法正常工作

时间:2016-07-24 16:39:03

标签: java javafx scrollbar zoom

我有以下结构:

ScrollPane
   Pane
       A set of children as ImageView

我从下面开始上课(稍后会添加链接,现在无法找到)

public class ZoomController extends ScrollPane{

    private int deltaCount = 0;
    private final double DEFAULT_ZOOM = 1.0;
    private DoubleProperty zoomMax = new SimpleDoubleProperty(10.0);
    private DoubleProperty zoomMin = new SimpleDoubleProperty(0.1);
    private DoubleProperty zoomDelta = new SimpleDoubleProperty(1.2);
    private DoubleProperty zoom = new SimpleDoubleProperty(DEFAULT_ZOOM);
    GroupController gc;
    public ZoomController(Node content) {
        super();
        zoom.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> ov, Number t, Number t1) {
                System.out.println("Zoom=" + zoom.doubleValue());

                content.scaleXProperty().bind(zoom);
                content.scaleYProperty().bind(zoom);
                content.translateXProperty();
            }
        });

        content.setOnScroll(new EventHandler<ScrollEvent>() {
            public void handle(ScrollEvent event) {
                if (event.getDeltaY() > 0) {
                    zoomIn();
                } else {
                    zoomOut();
                }
            }
        });
    }

    public void zoomIn() {
        double zoomValue = DEFAULT_ZOOM * Math.pow(zoomDelta.get(), deltaCount + 1);
        System.out.println("Zoooom " + zoomValue);
        if (zoomValue > zoomMax.get()) {
            setZoom(zoomMax.get());
            return;
        }

        deltaCount++;
        setZoom(zoomValue);

    }

    public void zoomOut() {
        double zoomValue = DEFAULT_ZOOM * Math.pow(zoomDelta.get(), deltaCount - 1);
        System.out.println("Zoooom " + zoomValue);
        if (zoomValue < zoomMin.get()) {
            setZoom(zoomMin.get());
            return;
        }

        deltaCount--;
        setZoom(zoomValue);
    }

    public void setZoom(double zoomValue) {
        zoom.set(zoomValue);
    }
}

以下线折痕对象:

zoomController = new ZoomController(pane);

当我放大时,有一种非常奇怪的行为 - 当我放大时,两侧的滚动条不会出现。如何让这些酒吧出现?

据我所知,放大会改变内部面板的大小,当它比滚动面板大时,滚动窗格必须显示侧边栏。

1 个答案:

答案 0 :(得分:1)

scale内容的大小计算中未考虑ScrollPane属性。要解决此问题,可以将内容包装在Group中。此外,实际设置内容可以帮助。顺便说一下,你不应该在该绑定源的监听器内建立绑定。创建对该属性的绑定

content.scaleXProperty().bind(zoom);
content.scaleYProperty().bind(zoom);

或在侦听器中设置值:

zoom.addListener(new ChangeListener<Number>() {
    @Override
    public void changed(ObservableValue<? extends Number> ov, Number t, Number newZoom) {
        System.out.println("Zoom=" + newZoom.doubleValue());
        content.setScaleX(newZoom.doubleValue());
        content.setScaleY(newZoom.doubleValue());
    }
});

public class ZoomController extends ScrollPane {

    private int deltaCount = 0;
    private final double DEFAULT_ZOOM = 1.0;
    private DoubleProperty zoomMax = new SimpleDoubleProperty(10.0);
    private DoubleProperty zoomMin = new SimpleDoubleProperty(0.1);
    private DoubleProperty zoomDelta = new SimpleDoubleProperty(1.2);
    private DoubleProperty zoom = new SimpleDoubleProperty(DEFAULT_ZOOM);

    public ZoomController(Node content) {
        super(new Group(content));

        zoom.addListener(new ChangeListener<Number>() {
            @Override
            public void changed(ObservableValue<? extends Number> ov, Number t, Number newZoom) {
                System.out.println("Zoom=" + newZoom.doubleValue());
                content.setScaleX(newZoom.doubleValue());
                content.setScaleY(newZoom.doubleValue());
            }
        });

        content.setOnScroll(new EventHandler<ScrollEvent>() {
            public void handle(ScrollEvent event) {
                if (event.getDeltaY() > 0) {
                    zoomIn();
                } else {
                    zoomOut();
                }
            }
        });
    }