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

3
我试图创建一个包含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);
是我的内部类。
这是我得到的:enter image description here

3
不要使用StackPane,请使用BorderPaneVBoxHBox - SedJ601
AnchorPane 也许也能满足您的需求。 - SystemGlitch
如果你的“场景”布局是静态的,我建议你使用SceneBuilder - SedJ601
2
我认为发生的情况是,面板在缩放之前只是填充整个堆栈面板。变换是在布局之后应用的,因此小面板填充整个堆栈面板,然后以其中心为中心缩放25%。因此,缩放的面板看起来居中。尝试将小面板包装在“Group”中,将该组添加到堆栈面板中,而不是直接添加小面板,并在组上设置对齐方式。(如果您发布一个我们可以测试的实际[MCVE],我可以在答案中精确说明。) - James_D
1个回答

7
将缩放的 Pane 放置在 Group 内。为了布局目的,StackPane(和其他布局面板)将忽略节点上的缩放因子和其他变换,但 Group 不会。
Group javadoc 中可以看到:
任何应用于 Group 的变换、效果或状态都将应用于该组的所有子项。这些变换和效果不会包含在此 Group 的布局边界中,但是如果直接在此 Group 的子项上设置变换和效果,则这些变换和效果将包含在此 Group 的布局边界中。

示例应用

sample

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接