0
方案:舞台包含场景。场景包含一个StackPane。 StackPane和Scene的高度和宽度绑定在一起。 StackPane包含一个Shape,它是Rectangle和Shape的联合。将Shape类绑定到StackPane高度
问题 - 我正在将Shape类绑定到StackPane的高度时遇到问题。如何在我的示例中绑定Shape类的特定部分或完整的Shape类?
要求 - 我有2个要求。
- 当我最大化阶段,StackPane得到增加,因为 高度和宽度绑定到场景但形状不增加。 I 需要形状的(
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);
}
}
请让我知道解决的办法。提前致谢。
我用Java8(1.8.0_11)与FX2,在该版本中,RectangleBuilder已弃用且Shape.union()不存在......您使用的是哪个版本? – 2014-12-02 21:49:59
我正在使用Java8(1.8.0_25)。是的,在这个版本RectangleBuilder是deprectaed,但我们仍然能够起诉它。如果不是RectangleBuilder,我需要直接使用Rectangle。但在这种情况下,问题也会持续存在。请让我知道,如果通过直接使用新的矩形()可以实现上述要求。 Shape.union对我来说确实存在。下面的链接有它。 https://docs.oracle.com/javafx/2/api/javafx/scene/shape/Shape.html – 2014-12-03 03:33:37