Detect Android: weight in JavaFX

I am developing a small software for JavaFX training

In Android, inside LinearLayout, I can expand the view to fill the whole available space by setting the variable Android: weight = "somevalue". Unfortunately, I can't do this. This is JavaFX

I tried using max / min / pref height, but it didn't work

Any help would be appreciated

resolvent:

use V@R_ 943_ 2419@ / H@R_ 943_ 2419 @, you can set the vgrow / hgrow static attribute of child nodes (or some of them) to priority.always, which makes these nodes grow when the parent node grows (assuming that they can be resized)

private static Region createRegion(String color) {
    Region region = new Region();
    region.setStyle("-fx-background-color: "+color);
    return region;
}

@Override
public void start(Stage primaryStage) {
    V@R_943_2419@ v@R_943_2419@ = new V@R_943_2419@(
            createRegion("red"),
            createRegion("blue"),
            createRegion("green")
    );
    for (Node n : v@R_943_2419@.getChildren()) {
        V@R_943_2419@.setVgrow(n, Priority.ALWAYS);
    }

    Scene scene = new Scene(v@R_943_2419@);

    primaryStage.setScene(scene);
    primaryStage.show();
}

To control the relative weight of children, you can use gridpane and set the percentwidth / percentheight property of columnconstraints / rowconstraints

@Override
public void start(Stage primaryStage) throws IOException {
    GridPane root = new GridPane();

    root.getColumnConstraints().addAll(DoubleStream.of(30, 2, 68)
            .mapToObj(width -> {
                ColumnConstraints constraints = new ColumnConstraints();
                constraints.setPercentWidth(width);
                constraints.setFillWidth(true);
                return constraints;
            }).toArray(ColumnConstraints[]::new));

    RowConstraints rowConstraints = new RowConstraints();
    rowConstraints.setVgrow(Priority.ALWAYS);

    root.getRowConstraints().add(rowConstraints);

    root.addRow(0, Stream.of("red", "green", "blue").map(s -> createRegion(s)).toArray(Node[]::new));

    Scene scene = new Scene(root);

    primaryStage.setScene(scene);

    primaryStage.show();
}

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>