4

将 FlowPane 添加到 ScrollPane 中的另一个窗格时出现问题。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class ScrollProblem extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        ScrollPane sc = new ScrollPane();
        VBox vbox = new VBox();
        sc.setContent(vbox);
        sc.setFitToWidth(true);
        sc.setHbarPolicy(ScrollPane.ScrollBarPolicy.NEVER);

        vbox.getChildren().addAll(
                new Lbl("Some text"),
                new Lbl("Some longer text that surpasses the width of the window"),
                // When I remove this FlowPane from the code, then everything works. When I add it
                // to the code, then the Labels do not wrap correctly.
                new FlowPane(
                        new Lbl("A flowpane containing some other text")
                )
        );


        Scene scene = new Scene(sc);
        primaryStage.setScene(scene);
        primaryStage.setWidth(200);
        primaryStage.setHeight(400);
        primaryStage.show();
    }

    private class Lbl extends Label {
        public Lbl(String text) {
            super(text);
            this.setWrapText(true);
        }
    }
}

这是没有流窗格的外观(以及应该看起来的样子) :

它应该是什么样子

当我调整这个窗口的大小时,文本仍然正确换行。

这是流窗格的样子:

在此处输入图像描述

如您所见,流窗格内的标签没有换行。此外,当我调整窗口大小时,其他标签不再换行:

在此处输入图像描述

先感谢您!

4

0 回答 0