gpt4 book ai didi

JavaFX scrollpane - 带滚轮的水平平移

转载 作者:行者123 更新时间:2023-11-29 07:38:46 26 4
gpt4 key购买 nike

我可以在 JavaFX 中简单地创建一个只水平滚动的滚动 Pane ,如下所示:

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

但是,在这种情况下,鼠标滚轮仍会尝试垂直滚动而不是水平滚动(除非我专门在水平滚动条上滚动。)

如何设置滚动 Pane ,使鼠标滚轮水平移动?

最佳答案

这是我为您编写的示例应用程序,它完全符合您的要求:

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);
}

}

关于JavaFX scrollpane - 带滚轮的水平平移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32544574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com