如何防止FlowPane在到达场景结束时(垂直)包裹

时间:2016-12-22 13:20:10

标签: java javafx javafx-8

public void start(Stage stage) throws Exception {

    FlowPane flowPane = new FlowPane();
    flowPane.setOrientation(Orientation.VERTICAL);

    for(int i = 0; i < 101;i++) {
        Label aLabel = new Label("Label number: " + i);
        flowPane.getChildren().add(aLabel);
    }

    Scene applicationScene = new Scene(flowPane);
    stage.setHeight(400.0);
    stage.setWidth(400.0);
    stage.setScene(applicationScene);
    stage.show();
}

我正在尝试编写代码,以便所有标签最终都在同一列中,即使标签不在窗口内(我计划添加scrollPane以使标签仍然可见)。但是,我不知道为什么,因为标签在填充第一列时会自动开始填充下一列(example here)。我应该怎么做呢?

1 个答案:

答案 0 :(得分:1)

  

我不清楚为什么标签会自动开始填充   第一个填充时的下一列

这是FlowPane的功能。来自documentation

  

FlowPane将其子项布置在包裹流动窗口的流中   边界。 ...垂直流动窗格以列的形式布置节点,包裹在流动窗格的高度。

您应该使用VBox代替:

public void start(Stage stage) throws Exception {

    VBox vbox = new VBox();

    for(int i = 0; i < 101;i++) {
        Label aLabel = new Label("Label number: " + i);
        vbox.getChildren().add(aLabel);
    }

    Scene applicationScene = new Scene(vbox);
    stage.setHeight(400.0);
    stage.setWidth(400.0);
    stage.setScene(applicationScene);
    stage.show();
}

如果要显示大量数据,您可能还会考虑使用ListViewListView有一个更复杂的API(它管理选择,如果你选择可以编辑),并在需要时提供自己的滚动条,但它对于大量数据更有效(基本上它只创建UI)控制可见数据,并在用户滚动时重复使用它们。

相关问题