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

时间:2014-06-21 18:50:08

标签: java layout javafx pane

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

代码:

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);
    }
}

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

1 个答案:

答案 0 :(得分:0)

我通过将Pane p1放入一个组并将该组放入borderpane而不是使用Position Center的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);
    }

}