设置堆栈窗格中窗格的对齐方式

set alignment of a pane in a stack pane

我尝试创建一个在堆栈窗格中包含 2 个窗格的应用程序。 一个窗格是主窗格,居中,第二个窗格较小,停靠在舞台的左下角。

问题是我已经尝试使用 'setAlignment' 但它似乎不起作用(尽管按钮已对齐)。 小窗格始终居中。

这是什么问题,我该如何解决? 我想也许我无法对齐窗格,那么我该如何克服呢?

Pane pane = new Pane();

for (SerialPoint sp : points) {
    Circle circle = new Circle(sp.getX(), sp.getY(), 6, Color.GREEN);
    pane.getChildren().add(circle);
}

Pane smallPane = new Pane();
smallPane.setScaleX(0.25);
smallPane.setScaleY(0.25);
smallPane.setStyle("-fx-border-color: black;");

for (SerialPoint sp : points) {
    Circle circle = new Circle(sp.getX(), sp.getY(), 6, Color.RED);
    smallPane.getChildren().add(circle);
}

Button startBtn = new Button("Start");

StackPane stackPane = new StackPane(pane, smallPane, startBtn);
StackPane.setAlignment(smallPane, Pos.BOTTOM_LEFT);
StackPane.setAlignment(startBtn, Pos.TOP_RIGHT);
StackPane.setMargin(startBtn, new Insets(5));

Scene scene = new Scene(stackPane);

(SerialPoint 是我的内在class)

这是我得到的:

将缩放后的窗格放在一个组中。出于布局目的,StackPane(和其他布局窗格)将忽略节点上的比例因子和其他变换,但组不会。

来自Group javadoc

Any transform, effect, or state applied to a Group will be applied to all children of that group. Such transforms and effects will NOT be included in this Group's layout bounds, however if transforms and effects are set directly on children of this Group, those will be included in this Group's layout bounds.


示例应用程序

import javafx.application.Application;
import javafx.geometry.*;
import javafx.scene.*;
import javafx.scene.control.Button;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

import java.util.Random;

public class AligningPains extends Application {

    private static final int N_POINTS = 5;
    private static final int MAX_POINT_POS = 100;
    private static final int POINT_RADIUS = 6;
    private static final int PREF_PANE_SIZE = 300;
    private static final int BUTTON_INSETS = 5;

    Point2D[] points = new Point2D[N_POINTS];
    Random random = new Random(42);

    @Override
    public void start(Stage stage) {
        initPoints();

        Pane pane = new Pane();
        pane.setPrefSize(PREF_PANE_SIZE, PREF_PANE_SIZE);

        addCircles(pane, Color.GREEN);

        Pane smallPane = new Pane();
        smallPane.setStyle("-fx-border-color: black;");
        smallPane.setPrefSize(PREF_PANE_SIZE, PREF_PANE_SIZE);
        smallPane.setScaleX(0.25);
        smallPane.setScaleY(0.25);

        addCircles(smallPane, Color.RED);

        Group smallGroup = new Group(smallPane);

        Button startBtn = new Button("Start");

        StackPane stackPane = new StackPane(pane, smallGroup, startBtn);
        StackPane.setAlignment(smallGroup, Pos.BOTTOM_LEFT);
        StackPane.setAlignment(startBtn, Pos.TOP_RIGHT);
        StackPane.setMargin(startBtn, new Insets(BUTTON_INSETS));
        stackPane.setPrefSize(PREF_PANE_SIZE, PREF_PANE_SIZE);

        Scene scene = new Scene(stackPane);
        stage.setScene(scene);
        stage.show();
    }

    private void addCircles(Pane pane, Color color) {
        for (Point2D sp : points) {
            Circle circle = new Circle(sp.getX(), sp.getY(), POINT_RADIUS, color);
            pane.getChildren().add(circle);
        }
    }

    private void initPoints() {
        for (int i = 0; i < points.length; i++) {
            points[i] = new Point2D(random.nextInt(MAX_POINT_POS), random.nextInt(MAX_POINT_POS));
        }
    }

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