我有一个 TextArea,其中有一些提示文本,我想将其拆分为几行不同的行,但是,由于某种原因,换行符在提示文本中不起作用。
代码:
TextArea paragraph = new TextArea();
paragraph.setWrapText(true);
paragraph.setPromptText(
"Stuff done today:\n"
+ "\n"
+ "- Went to the grocery store\n"
+ "- Ate some cookies\n"
+ "- Watched a tv show"
);
Run Code Online (Sandbox Code Playgroud)
结果:
正如您所看到的,文本没有正确换行。有谁知道如何解决这一问题?
提示在内部由可以处理换行符的 Text 类型的节点显示。所以有趣的问题是为什么它们不显示?通过查看promptText属性可以揭示原因:它默默地\n用空字符串替换所有内容:
private StringProperty promptText = new SimpleStringProperty(this, "promptText", "") {
@Override protected void invalidated() {
// Strip out newlines
String txt = get();
if (txt != null && txt.contains("\n")) {
txt = txt.replace("\n", "");
set(txt);
}
}
};
Run Code Online (Sandbox Code Playgroud)
一种解决方法(不确定它是否适用于所有平台 - 在我的胜利上)是使用\r:
paragraph.setPromptText(
"Stuff done today:\r"
+ "\r"
+ "- Went to the grocery store\r"
+ "- Ate some cookies\r"
+ "- Watched a tv show"
);
Run Code Online (Sandbox Code Playgroud)