Mek*_*iah 6 java javafx scrollpane flowpane javafx-8
我在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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
看看下面的代码,然后告诉我这是否是您想要实现的目标。我仍然不确定导致问题的原因,我必须查看 ScrollPane 的文档才能找到答案。我的怀疑在于setFitToWidth&setFitToHeight方法。尽管我仍然相信这不是一个错误。
import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.HBox;
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");
// Apparently this cause the issue here.
// scroll.setFitToHeight(true);
// scroll.setFitToWidth(true);
// Instead just make the flow pane take the dimensions of the ScrollPane
// the -5 is to not show the Bars when both of panes have the same dimensions
flow.prefWidthProperty().bind(Bindings.add(-5, scroll.widthProperty()));
flow.prefHeightProperty().bind(Bindings.add(-5, scroll.heightProperty()));
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++) {
HBox filler = new HBox();
filler.setStyle("-fx-border-color: black");
filler.setPrefSize(100, 100);
root.getChildren().add(filler);
}
}
}
Run Code Online (Sandbox Code Playgroud)
查看 ScrollPane 的文档,具体来说,setFitToHeight您会发现:
属性描述:如果为 true 并且包含的节点是可调整大小的,则该节点将保持调整大小以匹配 ScrollPane 视口的高度。如果包含的节点不是可调整大小的,则忽略该值。
并且因为 ScrollPane 内的节点将不断调整大小以匹配 ScrollPane 视口的宽度和高度,这就是垂直滚动条永远不会出现的原因。
| 归档时间: |
|
| 查看次数: |
1649 次 |
| 最近记录: |