如何推断 TextFlow 中的分页符?

How to infer a page break in a TextFlow?

我正在尝试在我的应用程序中创建记事本屏幕。创建笔记后,它们将被锁定且无法编辑,但是可以将新页面添加到笔记中。

我认为使用 TextFlow 可以很好地控制它。我想做的是:

**Note Taker Name**
**Date of Note**
Line of Note Text
Line of Note Text
Line of Note Text
Line of Note Text
------------------------------------------
**Note Taker Name**
**Date of Note**
Line of Note Text
Line of Note Text
Line of Note Text
Line of Note Text

我试过这样:

String s1 = "line of text";

textFlow.getChildren().add(new Text(s1));
textFlow.getChildren().add(new Text(System.lineSeparator()));
textFlow.getChildren().add(new Separator(Orientation.HORIZONTAL));
textFlow.getChildren().add(new Text(System.lineSeparator()));
textFlow.getChildren().add(new Text(s1));

scrollPane.setFitToWidth(true);

除了分隔符只是一条细线外,这几乎满足了我的需求。我希望这条线穿过整个 TextFlow,但我不确定该怎么做。

谢谢。

我找到了一个有效的答案 - 虽然我不确定这是否是处理它的最佳方法:

private void loadTextFlowWithNotes() {
    textFlow.getChildren().clear();
     notes.forEach(note -> {
        Text nameText = new Text("Taken By: " + note.takenBy);
        nameText.setFill(Color.CRIMSON);
        textFlow.getChildren().add(nameText);
        textFlow.getChildren().add(new Text(System.lineSeparator()));

        Text takenOn = new Text("Taken On: " + note.takenOn.format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT)));
        takenOn.setFill(Color.CRIMSON);
        textFlow.getChildren().add(takenOn);
        textFlow.getChildren().add(new Text(System.lineSeparator()));
        textFlow.getChildren().add(new Text(System.lineSeparator()));

        textFlow.getChildren().add(new Text(note.noteText));
        textFlow.getChildren().add(new Text(System.lineSeparator()));
        textFlow.getChildren().add(new Text(System.lineSeparator()));

        Line line = new Line();
        line.setStartX(0);
        line.endXProperty().bind(textFlow.widthProperty());
        line.setFill(Color.CRIMSON);
        textFlow.getChildren().add(line);
        textFlow.getChildren().add(new Text(System.lineSeparator()));
        textFlow.getChildren().add(new Text(System.lineSeparator()));
    });
}

已经有一个 JavaFX 内置控件可以满足您的需要:Separator (javadoc)。

@Override
public void start(Stage primaryStage) {
    TextFlow textFlow = new TextFlow();
    Text nameText = new Text("Taken By me ");
    nameText.setFill(Color.CRIMSON);
    textFlow.getChildren().add(nameText);
    textFlow.getChildren().add(new Text(System.lineSeparator()));

    Text takenOn = new Text("Taken On: " + DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT).format(LocalDateTime.now()));
    takenOn.setFill(Color.CRIMSON);
    textFlow.getChildren().add(takenOn);
    textFlow.getChildren().add(new Text(System.lineSeparator()));

    textFlow.getChildren().add(new Text("this is a note"));
    textFlow.getChildren().add(new Text(System.lineSeparator()));

    // Separator
    final Separator separator = new Separator(Orientation.HORIZONTAL);
    separator.prefWidthProperty().bind(textFlow.widthProperty());
    separator.setStyle("-fx-background-color: red;");
    textFlow.getChildren().add(separator);

    textFlow.getChildren().add(new Text(System.lineSeparator()));
    textFlow.getChildren().add(new Text("this is another note"));

    Scene scene = new Scene(textFlow, 300, 250);

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