gpt4 book ai didi

java - 设置堆栈 Pane 中 Pane 的对齐方式

转载 作者:行者123 更新时间:2023-11-30 06:44:53 26 4
gpt4 key购买 nike

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

问题是我试过使用“setAlignment”但它似乎不起作用(尽管按钮已对齐)。 小 Pane 始终居中。

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

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 是我的内部类)

这是我得到的:enter image description here

最佳答案

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

来自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.


示例应用

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

关于java - 设置堆栈 Pane 中 Pane 的对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49988109/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com