我已经创建了 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.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 不反射(reflect)应用的转换。因此,stackPane 会在 svgPath 的原始大小下拉伸(stretch)并拉伸(stretch)窗口。我知道这种行为是根据文档(layoutBounds 不应该考虑转换),但是如何解决这个问题?

最佳答案

Group 在计算布局边界方面具有以下行为(我的重点):



因此,将 SVG 路径包装在一个组中将为您提供所需的效果:

StackPane stackPane = new StackPane();
// stackPane.getChildren().add(svgPath);
stackPane.getChildren().add(new Group(svgPath));
stackPane.getChildren().add(label);

关于JavaFX:将转换应用于节点布局边界,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42931655/

10-09 17:22
查看更多