如何推断TextFlow中的分页符?

时间:2016-06-29 14:00:47

标签: javafx-8 textflow

我正在尝试在我的应用程序中创建一个NotePad屏幕。创建注释后,它们将被锁定且无法编辑,但可以将新页面添加到注释中。

我认为使用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,我不知道该怎么做。

感谢。

2 个答案:

答案 0 :(得分:2)

已经有一个JavaFX内置控件可满足您的需求:Separatorjavadoc)。

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

Separator

答案 1 :(得分:0)

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

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