简体   繁体   English

JavaFX滚动窗格-使用滚轮进行水平平移

[英]JavaFX scrollpane - horizontal panning with scroll wheel

I can trivially create a scroll pane in JavaFX that only scrolls horizontally like so: 我可以在JavaFX中简单地创建一个滚动窗格,该滚动窗格只能像这样水平滚动:

ScrollPane scroll = new ScrollPane(lcp);
scroll.setPannable(true);
scroll.setFitToHeight(true);
scroll.setVbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

However, the mouse scroll wheel still tries to scroll vertically rather than horizontally in this case (unless I specifically scroll over the horizontal scroll bar.) 但是,在这种情况下,鼠标滚轮仍然尝试垂直滚动而不是水平滚动(除非我专门滚动水平滚动条。)

How can I set up the scroll pane so that the mouse wheel pans horizontally? 如何设置滚动窗格,使鼠标滚轮水平平移?

Here is the example application that I wrote for you and does exactly what you want: 这是我为您编写的示例应用程序,它确实可以满足您的要求:

public class Test extends Application {

    ScrollPane scrollPane;
    int pos = 0;
    final int minPos = 0;
    final int maxPos = 100;

    @Override
    public void start(Stage primaryStage) {

        Label label = new Label("TEXT!!!!!!!TEXT!!!!!!!TEXT!!!!!!!TEXT!!!!!!!TEXT!!!!!!!TEXT");
        label.setPrefSize(500, 100);
        label.setOnScroll(new EventHandler<ScrollEvent>() {
            @Override
            public void handle(ScrollEvent event) {

                if (event.getDeltaY() > 0)
                    scrollPane.setHvalue(pos == minPos ? minPos : pos--);
                else
                    scrollPane.setHvalue(pos == maxPos ? maxPos : pos++);

            }
        });

        scrollPane = new ScrollPane();
        scrollPane.setHmin(minPos);
        scrollPane.setHmax(maxPos);
        scrollPane.setVbarPolicy(ScrollBarPolicy.NEVER);
        scrollPane.setHbarPolicy(ScrollBarPolicy.ALWAYS);
        scrollPane.setPannable(true);
        scrollPane.setFitToHeight(true);
        scrollPane.setContent(label);

        BorderPane root = new BorderPane();
        root.setPrefSize(200, 100);
        root.setCenter(scrollPane);

        primaryStage.setScene(new Scene(root));
        primaryStage.show();

    }

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

}

I have also been looking for a solution and found this one from Ugurcan Yildirim but didn't like the fact that the natural scroll bar length and speed is modified also. 我也一直在寻找解决方案,并从Ugurcan Yildirim找到了一个解决方案,但不喜欢自然滚动条的长度和速度也被修改的事实。 This one worked for me: 这个为我工作:

scrollPane.setOnScroll(event -> {
    if(event.getDeltaX() == 0 && event.getDeltaY() != 0) {
        scrollPane.setHvalue(scrollPane.getHvalue() - event.getDeltaY() / this.allComments.getWidth());
    }
});

event.getDeltaX() == 0 just to be sure that the user is only using the mouse wheel and nothing is adding up event.getDeltaX() == 0只是为了确保用户只使用了鼠标滚轮,并且没有东西累加

this.allComments is the content of the scrollPane (a HBox in my case). this.allCommentsscrollPane的内容(在我的情况下为HBox)。 By dividing the delta y value by it's content width the scroll speed is natural according to the amount of content to scroll. 通过将Δy值除以其内容宽度,滚动速度自然取决于要滚动的内容量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM