我在 JavaFX 8 中的 ScrollPane 根据需要显示滚动条时遇到了一些困难。我目前正在做的只是创建一个具有 x 个元素的 FlowPane,并将其设置为 ScrollPane 的内容。
当我垂直于 FlowPane 的方向收缩时,就会出现问题。当元素开始换行并越界时,滚动条不会出现。当我平行于方向收缩时,不会发生这种情况。我有一个小的 Java 程序来举例说明这个问题。
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
FlowPane flow = new FlowPane();
flow.setStyle("-fx-border-color: red");
addPanes(flow, 16);
ScrollPane scroll = new ScrollPane(flow);
scroll.setStyle("-fx-border-color: green");
scroll.setFitToHeight(true);
scroll.setFitToWidth(true);
Scene scene = new Scene(scroll, 450, 450);
primaryStage.setScene(scene);
primaryStage.show();
}
public void addPanes(FlowPane root, int panes) {
for(int i = 0; i < panes; i++) {
StackPane filler = new StackPane();
filler.setStyle("-fx-border-color: black");
filler.setPrefSize(100, 100);
root.getChildren().add(filler);
}
}
}