我有一个需要在ScrollPane中放大的应用程序,但是按照我目前的方法,我仍然面临着两个挑战。为了复制问题,我编写了一个小应用程序ZoomApp,您将找到下面的代码。它的有限功能允许在某些任意形状上放大和缩小(使用Ctrl +鼠标滚轮)。当缩放的内容超出窗口范围时,滚动条应该显示。
挑战1。 当滚动条因 innerGroup 大小缩放而出现时,ScrollEvent不再到达我的ZoomHandler。相反,我们开始向下滚动窗口,直到它到达底部,当再次缩放按预期工作时。我想也许
scrollPane.setPannable(false);
会有所作为,但不会。如何避免这种不受欢迎的行为?
挑战2。 我将如何将 innerGroup 集中在scrollPane中,而不是在 innerGroup 的左上角绘制一个像素,并将所需的delta添加到正方形?
作为旁注,根据JavaDoc for ScrollPane:“如果应用程序希望滚动基于节点的可视边界(对于缩放内容等),则需要将滚动节点包装在一个小组“。这就是我在ScrollPane中有 innerGroup 和 outerGroup 的原因。
这个JavaFX新手非常赞赏任何指导我解决方案的建议。
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.ScrollPane;
import javafx.scene.input.ScrollEvent;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/**
* Demo of a challenge I have with zooming inside a {@code ScrollPane}.
* <br>
* I am running JavaFx 2.2 on a Mac. {@code java -version} yields:
* <pre>
* java version "1.7.0_09"
* Java(TM) SE Runtime Environment (build 1.7.0_09-b05)
* Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode)
* </pre>
* 6 rectangles are drawn, and can be zoomed in and out using either
* <pre>
* Ctrl + Mouse Wheel
* or Ctrl + 2 fingers on the pad.
* </pre>
* It reproduces a problem I experience inside an application I am writing.
* If you magnify to {@link #MAX_SCALE}, an interesting problem occurs when you try to zoom back to {@link #MIN_SCALE}. In the beginning
* you will see that the {@code scrollPane} scrolls and consumes the {@code ScrollEvent} until we have scrolled to the bottom of the window.
* Once the bottom of the window is reached, it behaves as expected (or at least as I was expecting).
*
* @author Skjalg Bjørndal
* @since 2012.11.05
*/
public class ZoomApp extends Application {
private static final int WINDOW_WIDTH = 800;
private static final int WINDOW_HEIGHT = 600;
private static final double MAX_SCALE = 2.5d;
private static final double MIN_SCALE = .5d;
private class ZoomHandler implements EventHandler<ScrollEvent> {
private Node nodeToZoom;
private ZoomHandler(Node nodeToZoom) {
this.nodeToZoom = nodeToZoom;
}
@Override
public void handle(ScrollEvent scrollEvent) {
if (scrollEvent.isControlDown()) {
final double scale = calculateScale(scrollEvent);
nodeToZoom.setScaleX(scale);
nodeToZoom.setScaleY(scale);
scrollEvent.consume();
}
}
private double calculateScale(ScrollEvent scrollEvent) {
double scale = nodeToZoom.getScaleX() + scrollEvent.getDeltaY() / 100;
if (scale <= MIN_SCALE) {
scale = MIN_SCALE;
} else if (scale >= MAX_SCALE) {
scale = MAX_SCALE;
}
return scale;
}
}
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
final Group innerGroup = createSixRectangles();
final Group outerGroup = new Group(innerGroup);
final ScrollPane scrollPane = new ScrollPane();
scrollPane.setContent(outerGroup);
scrollPane.setOnScroll(new ZoomHandler(innerGroup));
StackPane stackPane = new StackPane();
stackPane.getChildren().add(scrollPane);
Scene scene = SceneBuilder.create()
.width(WINDOW_WIDTH)
.height(WINDOW_HEIGHT)
.root(stackPane)
.build();
stage.setScene(scene);
stage.show();
}
private Group createSixRectangles() {
return new Group(
createRectangle(0, 0), createRectangle(110, 0), createRectangle(220, 0),
createRectangle(0, 110), createRectangle(110, 110), createRectangle(220, 110),
createRectangle(0, 220), createRectangle(110, 220), createRectangle(220, 220)
);
}
private Rectangle createRectangle(int x, int y) {
Rectangle rectangle = new Rectangle(x, y, 100, 100);
rectangle.setStroke(Color.ORANGERED);
rectangle.setFill(Color.ORANGE);
rectangle.setStrokeWidth(3d);
return rectangle;
}
}
答案 0 :(得分:12)
好的,所以我终于找到了解决问题的方法。
仅仅代替行
scrollPane.setOnScroll(new ZoomHandler(innerGroup));
与
scrollPane.addEventFilter(ScrollEvent.ANY, new ZoomHandler(innerGroup));
它现在按预期工作。不需要神秘的矩形或其他黑客。
接下来的问题是为什么?根据{{3}},
在事件捕获阶段执行事件过滤器。
,而
在事件冒泡阶段执行事件处理程序。
我认为这就是产生差异的原因。
答案 1 :(得分:2)
以下解决方法似乎可以提供更好的结果:
添加一个覆盖整个屏幕的不透明矩形,这样您就不会错过滚动事件。如果你没有碰到形状,显然你可以错过滚动事件。
Rectangle opaque = new Rectangle(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
opaque.setOpacity( 0 );
outerGroup.getChildren().add( opaque );
outerGroup.setOnScroll(new ZoomHandler(innerGroup));
答案 2 :(得分:0)
在我的情况下,我更新了以下内容
if (scrollEvent.isControlDown()) {
与if (!scrollEvent.isConsumed()) {
....以及你发布的变化......:)