【问题标题】:JavaFX BorderPane right side all heightJavaFX BorderPane 右侧所有高度
【发布时间】:2016-12-12 07:10:52
【问题描述】:

是否可以让 BorderPane 的右侧具有窗口的高度和底部/顶部的高度,然后以右侧的开头结束?

【问题讨论】:

    标签: javafx borderpane


    【解决方案1】:

    AFAIK 没有办法做到这一点,但您可以使用 GridPane 实现所需的结果

    private static void setBackground(Region region, Color color) {
        region.setBackground(new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY)));
    }
    
    @Override
    public void start(Stage primaryStage) {
        GridPane gridPane = new GridPane();
        RowConstraints rConstranits1 = new RowConstraints();
        rConstranits1.setVgrow(Priority.NEVER);
        RowConstraints rConstranits2 = new RowConstraints();
        rConstranits2.setVgrow(Priority.ALWAYS);
        RowConstraints rConstranits3 = new RowConstraints();
        rConstranits3.setVgrow(Priority.NEVER);
    
        ColumnConstraints cConstraints1 = new ColumnConstraints();
        cConstraints1.setHgrow(Priority.NEVER);
        ColumnConstraints cConstraints2 = new ColumnConstraints();
        cConstraints2.setHgrow(Priority.ALWAYS);
        ColumnConstraints cConstraints3 = new ColumnConstraints();
        cConstraints3.setHgrow(Priority.NEVER);
    
        gridPane.getColumnConstraints().addAll(cConstraints1, cConstraints2, cConstraints3);
        gridPane.getRowConstraints().addAll(rConstranits1, rConstranits2, rConstranits3);
    
        Region top = new Region();
        top.setPrefSize(300, 100);
        setBackground(top, Color.RED);
    
        Region bottom = new Region();
        bottom.setPrefSize(400, 50);
        setBackground(bottom, Color.YELLOW);
    
        Region center = new Region();
        setBackground(center, Color.BLUE);
    
        Region right = new Region();
        setBackground(right, Color.LIME);
        right.setPrefSize(200, 500);
    
        Region left = new Region();
        setBackground(left, Color.BROWN);
        left.setPrefSize(200, 400);
    
        gridPane.add(right, 2, 0, 1, 3);
        cConstraints3.maxWidthProperty().bind(right.prefWidthProperty());
        cConstraints3.minWidthProperty().bind(right.prefWidthProperty());
        gridPane.add(top, 0, 0, 2, 1);
        rConstranits1.minHeightProperty().bind(top.prefHeightProperty());
        rConstranits1.maxHeightProperty().bind(top.prefHeightProperty());
        gridPane.add(bottom, 0, 2, 2, 1);
        rConstranits3.minHeightProperty().bind(bottom.prefHeightProperty());
        rConstranits3.maxHeightProperty().bind(bottom.prefHeightProperty());
        gridPane.add(center, 1, 1);
        gridPane.add(left, 0, 1);
        cConstraints1.minWidthProperty().bind(left.prefWidthProperty());
        cConstraints1.maxWidthProperty().bind(left.prefWidthProperty());
    
        Scene scene = new Scene(gridPane);
    
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    

    【讨论】:

      【解决方案2】:

      嵌套两个 BorderPanes 是另一种选择,它依赖于 BorderPane 内置的高度和宽度管理。感谢 fabian 提供示例基础和 setBackground 例程!

      private static void setBackground(Region region, Color color) {
          region.setBackground(new Background(new BackgroundFill(color, CornerRadii.EMPTY, Insets.EMPTY)));
      }
      
      @Override
      public void start(Stage primaryStage) {
      
          BorderPane outer = new BorderPane();
          BorderPane inner = new BorderPane();
      
          Region top = new Region();
          top.setPrefSize(300, 300);
          setBackground(top, Color.RED);
      
          Region bottom = new Region();
          bottom.setPrefSize(400, 200);
          setBackground(bottom, Color.YELLOW);
      
          Region right = new Region();
          setBackground(right, Color.BLUE);
          right.setPrefSize(200, 500);
      
          inner.setCenter(top);
          inner.setBottom(bottom);
      
          outer.setCenter(inner);
          outer.setRight(right);
      
          Scene s = new Scene(outer);
          primaryStage.setScene(s);
          primaryStage.show();
      
      }
      

      【讨论】:

      • 我认为这是更清洁的方法(查看其他答案中的常量负载)。真的帮到我了!非常感谢:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多