10

我正在研究存储瓷砖网格的东西,并且我正在使用 FlowPane 以便在调整窗口大小时瓷砖可以灵活地环绕。

我遇到的问题是屏幕右侧通常有很多多余的空间,我想将它均匀地分布在两侧。将对齐设置为中心类型的作品,但它使每一行中的内容居中,我希望每一行在左侧开始齐平?

我在说什么的可视化:

FlowPane 对齐

FlowPane 对齐

知道我需要做什么吗?

4

2 回答 2

2

您可以调整边框以获得更接近的输出。

import java.util.ArrayList;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;

/**
 *
 * @author blj0011
 */
public class JavaFXApplication95 extends Application
{

    @Override
    public void start(Stage primaryStage)
    {
        FlowPane flowPane = new FlowPane();

        List<Circle> circleContainer = new ArrayList();
        for(int i = 0; i < 5; i++)
        {
            Circle c1 = new Circle();
            c1.setRadius(50);
            c1.setFill(Color.BLUE);
            circleContainer.add(c1);
        }

        flowPane.getChildren().addAll(circleContainer);
//        flowPane.maxHeight(500);
//        flowPane.setMaxWidth(300);
        flowPane.setPadding(new Insets(30, 30, 30, 30));
        StackPane root = new StackPane();
        root.getChildren().add(flowPane);

        Scene scene = new Scene(root, 600, 400);


        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}

在此处输入图像描述 在此处输入图像描述

于 2017-05-02T21:45:26.103 回答
0

您可以使用解决方法:

final FlowPane flowPane = new FlowPane() {
    {
        setAlignment(Pos.TOP_CENTER);
    }

    @Override
    protected void layoutChildren() {
        super.layoutChildren();

        final LinkedHashMap<Double, List<Node>> rows = new LinkedHashMap<>();
        getChildren().forEach(node -> {
            final double layoutY = node.getLayoutY();
            List<Node> row = rows.get(node.getLayoutY());
            if (row == null) {
                row = new ArrayList<>();
                rows.put(layoutY, row);
            }

            row.add(node);
        });
            
        final Object[] keys = rows.keySet().toArray();
        final List<Node> firstRow = rows.get(keys[0]);
        final List<Node> lastRow = rows.get(keys[keys.length - 1]);

        for (int i = 0; i < lastRow.size(); i++) {
            lastRow.get(i).setLayoutX(firstRow.get(i).getLayoutX());
        }
    }
};
于 2021-10-28T07:31:51.963 回答