父窗格应调整大小到旋转子窗格的外边界

问题描述 投票:0回答:1

我想将窗格调整为旋转的子窗格。

码:

public class Test extends Application {
    public void start(final Stage stage) throws Exception {
        Pane p1 = new Pane();
        p1.setBackground(new Background(new BackgroundFill(Color.CYAN, null, null)));
        p1.setPrefSize(100, 100);
        p1.setMinSize(100, 100);
        p1.setMaxSize(100, 100);
        p1.setRotate(45);

        Pane p2 = new Pane(p1);
        p2.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
        p2.setLayoutX(150);
        p2.setLayoutY(150);

        Group root = new Group(p2);
        Scene scene = new Scene(root);

        stage.setTitle("Pane Test");
        stage.setScene(scene);
        stage.setWidth(400);
        stage.setHeight(400);
        stage.setResizable(false);
        stage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

我想让父窗格完全覆盖孩子所遮挡的区域。

java layout javafx pane
1个回答
0
投票

我通过将Pane p1放入一个组并将该组放入边框而不是使用位置中心的Pane p2来实现它。也许不是最好的解决方案,因为我正在使用额外的节点,但它可以工作。

public class Test extends Application {

    public void start(final Stage stage) throws Exception {
        Pane p1 = new Pane();
        p1.setBackground(new Background(new BackgroundFill(Color.CYAN, null, null)));
        p1.setPrefSize(100, 100);
        p1.setMinSize(100, 100);
        p1.setMaxSize(100, 100);
        p1.setRotate(45);

        Group g = new Group(p1);

        BorderPane.setAlignment(g, Pos.CENTER);

        BorderPane p2 = new BorderPane(g);

        p2.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));
        p2.setLayoutX(150);
        p2.setLayoutY(150);

        Group root = new Group(p2);
        Scene scene = new Scene(root);

        stage.setTitle("Pane Test");
        stage.setScene(scene);
        stage.setWidth(400);
        stage.setHeight(400);
        stage.setResizable(false);
        stage.show();


        p1.setRotate(20);
    }

    public static void main(String[] args) {
        launch(args);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.