Javafx 将形状类绑定到StackPane高度

Javafx 将形状类绑定到StackPane高度,javafx,javafx-2,javafx-8,Javafx,Javafx 2,Javafx 8,场景:舞台包含一个场景。该场景包含一个堆栈窗格。StackPane和场景的高度和宽度绑定在一起。StackPane包含一个矩形和形状的并集形状 问题-将形状类绑定到StackPane的高度时,我面临问题。在我的示例中,如何绑定Shape类的特定部分或完整的Shape类 要求-我有两个要求 当我最大化阶段时,StackPane会从 高度和宽度将绑定到场景,但形状不会增加。我 需要同时增加形状(smallShape和bigRectangle) 仅就高度而言 当我最大化阶段时,StackPane应该增

场景:舞台包含一个场景。该场景包含一个堆栈窗格。StackPane和场景的高度和宽度绑定在一起。StackPane包含一个矩形和形状的并集形状

问题-将形状类绑定到StackPane的高度时,我面临问题。在我的示例中,如何绑定Shape类的特定部分或完整的Shape类

要求-我有两个要求

  • 当我最大化阶段时,StackPane会从 高度和宽度将绑定到场景,但形状不会增加。我 需要同时增加形状(
    smallShape
    bigRectangle
    ) 仅就高度而言
  • 当我最大化阶段时,StackPane应该增加,并且只有较大的矩形高度应该增加,而其他小矩形(即
    bigRectangle
    高度应该只增加)
  • 下面是我的代码片段

    package application;
    
    import javafx.application.Application;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.shape.RectangleBuilder;
    import javafx.scene.shape.Shape;
    import javafx.stage.Stage;
    
    public class BindingShape extends Application {
    
        @Override
        public void start(Stage primaryStage) throws Exception {
    
            StackPane stackPane = new StackPane();
            stackPane.setPrefHeight(200);
            stackPane.setPrefWidth(200);
            stackPane.setStyle("-fx-background-color: BEIGE");
            Shape smallShape = RectangleBuilder.create()
                    .x(0)
                    .y(3)
                    .arcWidth(6)
                    .arcHeight(6)
                    .width(50) // allow overlap for union of tab and content rectangle
                    .height(50)
                    .build();
    
            Rectangle bigRectangle = RectangleBuilder.create()
                    .x(25)
                    .y(0)
                    .arcWidth(10)
                    .arcHeight(10)
                    .width(100)
                    .height(100)
                    .build();
            Shape unionShape = Shape.union(smallShape, bigRectangle);
            unionShape.setFill(Color.rgb(0, 0, 0, .50));
            unionShape.setStroke(Color.BLUE);
    
            Group shapeGroup = new Group();
            shapeGroup.getChildren().add(unionShape);
            stackPane.getChildren().add(shapeGroup);
    
            Group paneGroup = new Group();
            paneGroup.getChildren().add(stackPane);
    
            Scene scene = new Scene(paneGroup, 400, 400,Color.LIGHTSKYBLUE);
            stackPane.prefHeightProperty().bind(scene.heightProperty().divide(2));
            stackPane.prefWidthProperty().bind(scene.widthProperty().divide(2));
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    请告诉我解决办法。提前感谢。

    由于无法部分调整联合体的大小,如果容器已按如下方式调整大小,我将重建联合体:

    public class BindStackPaneToScene extends Application {
        Shape union;
        Shape makeShape(double w, double h) {
            Rectangle smallShape = new Rectangle(0, 3, 50, 50);
            smallShape.setArcHeight(6);
            smallShape.setArcWidth(6);
    
            Rectangle bigRectangle = new Rectangle(25, 3, w/2, h/2);
            bigRectangle.setArcHeight(10);
            bigRectangle.setArcWidth(10);
    
            Shape unionShape = Shape.union(smallShape, bigRectangle);
            unionShape.setFill(Color.rgb(0, 0, 0, .50));
            unionShape.setStroke(Color.BLUE);
    
            return unionShape;
        }
    
        @Override
        public void start(Stage primaryStage) throws Exception {
    
            StackPane stackPane = new StackPane();
            stackPane.setPrefHeight(200);
            stackPane.setPrefWidth(200);
            stackPane.setStyle("-fx-background-color: BEIGE");
    
            union = makeShape(200,200);
    
            Group shapeGroup = new Group();
            shapeGroup.getChildren().add(union);
            stackPane.getChildren().add(shapeGroup);
    
            stackPane.heightProperty().addListener((p, o, n) -> {
                if (union != null) shapeGroup.getChildren().remove(union);
                union = makeShape(stackPane.getWidth(),  n.doubleValue());
                shapeGroup.getChildren().add(union);
            });
    
            Group paneGroup = new Group();
            paneGroup.getChildren().add(stackPane);
    
            Scene scene = new Scene(paneGroup, 400, 400,Color.LIGHTSKYBLUE);
            stackPane.prefHeightProperty().bind(scene.heightProperty().divide(1));
            stackPane.prefWidthProperty().bind(scene.widthProperty().divide(1));
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        public static void main(String[] args) {
            launch(args);
        }
    
    }
    

    我将Java8(1.8.0_11)与fx2一起使用,在该版本中,RectangleBuilder不受欢迎,Shape.union()不存在。。。您使用的是什么版本?我使用的是Java8(1.8.0_25)。是的,在这个版本中,RectangleBuilder是不受欢迎的,但我们仍然可以起诉它。如果不是矩形生成器,我需要直接使用矩形。但在这种情况下,问题仍然存在。请让我知道,如果直接使用新矩形()是否可以达到上述要求。Shape.union对我来说确实存在。下面的链接有它。谢谢。它成功了,而且我能够根据我的其他需求调整代码。。