Как вывести разрыв страницы в 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 ответа
Решение
Уже есть встроенный элемент управления 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();
}
Я нашел ответ, который работает - хотя я не уверен, что это лучший способ справиться с этим:
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()));
});
}