JavaFX:将转换应用于节点布局边界
我已经创建了SVGPath和Label,并将它们放在StackPane上:JavaFX:将转换应用于节点布局边界,java,javafx,Java,Javafx,我已经创建了SVGPath和Label,并将它们放在StackPane上: package sample; import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.StackPane; import javafx.scene.paint.Col
package sample;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.FillRule;
import javafx.scene.shape.SVGPath;
import javafx.stage.Stage;
public class Main extends Application {
private static final String BUBBLE_SVG_PATH = "m 32.339338,-904.55632 c -355.323298,0 -643.374998,210.31657 " +
"-643.374998,469.78125 0,259.46468 288.0517,469.812505 643.374998,469.812505 123.404292,0 " +
"238.667342,-25.3559002 336.593752,-69.3438 69.80799,78.7043 181.84985,84.1354 378.90625,5.3126 " +
"-149.2328,-8.9191 -166.3627,-41.22 -200.6562,-124.031305 80.6876,-78.49713 128.5,-176.04496 " +
"128.5,-281.75 0,-259.46468 -288.0205,-469.78125 -643.343802,-469.78125 z";
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("Hello World");
SVGPath svgPath = new SVGPath();
svgPath.setContent(BUBBLE_SVG_PATH);
svgPath.setFill(Color.WHITE);
svgPath.setStroke(Color.BLACK);
svgPath.setScaleX(0.5);
svgPath.setScaleY(0.5);
Label label = new Label("Hello world");
System.out.println(svgPath.getBoundsInLocal());
System.out.println(svgPath.getBoundsInParent());
System.out.println(svgPath.getLayoutBounds());
StackPane stackPane = new StackPane();
stackPane.getChildren().add(svgPath);
stackPane.getChildren().add(label);
Group root = new Group();
root.getChildren().add(stackPane);
Scene scene = new Scene(root);
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我的问题是SVGPath的layoutBounds不能反映应用的转换。因此,stackPane将以svgPath的原始大小拉伸,并拉伸窗口。我理解这种行为是根据文档(layoutBounds不应考虑转换),但如何避免这种情况?A在计算布局边界方面有以下行为(我的重点):
一个群体将承担其子女的集体界限,而不是
可直接调整大小
应用于组的任何变换、效果或状态都将应用于
那一组的所有孩子。这样的变换和效果将不会被忽略
包含在此组的布局边界中,但是如果变换和
效果直接设置在该组的子对象上,这些子对象将
包含在此组的布局边界中
因此,将SVG路径包装在一个组中将获得所需的效果:
StackPane stackPane = new StackPane();
// stackPane.getChildren().add(svgPath);
stackPane.getChildren().add(new Group(svgPath));
stackPane.getChildren().add(label);