有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javafx VBox项目的java宽度未设置?

这是我写的代码,但没有为VBox项目设置宽度。所有的面板都是一样大小的。我需要一些帮助

private void initComponents() {
    this.setPadding(new Insets(0,70,0,70));
    this.setSpacing(10);
    
    red1 = new StackPane();
    red1.setStyle("-fx-background-color:red;");
    red1.setPrefHeight(60);
    red1.setPrefWidth(260);
    this.setVgrow(red1, Priority.ALWAYS);
    this.getChildren().add(red1);
    red2 = new StackPane();
    red2.setStyle("-fx-background-color:red;");
    red2.setPrefHeight(60);
    red2.setPrefWidth(240);
    this.setVgrow(red2, Priority.ALWAYS);
    this.getChildren().add(red2);
       
}

private StackPane red1;
private StackPane red2;
}

共 (1) 个答案

  1. # 1 楼答案

    所有窗格都会尝试布局并调整其子窗格的大小(如果可能),并且所有窗格的大小都由其父窗格根据其内容进行调整。有关更多信息,请阅读javadoc

    所有窗格(VBoxStackPaneFlowPanePane等)都打包到javafx.​scene.​layout包中,这意味着这些窗格用于布置其他节点/控件

    实际尝试获取的形状被打包到javafx.​scene.​shape包中。使用它们,例如:

    red1.getChildren().add(new Rectangle(200, 100, Color.YELLOW));
    ...
    red2.getChildren().add(new Circle(30, Color.BLUE));