我使用一个Text
节点来计算字符串的尺寸,如所描述这里,归纳为:
new Text("some text").getLayoutBounds();
Run Code Online (Sandbox Code Playgroud)
返回的维度在不同的执行点并不总是相同的,对于相同的输入 string,并且其他所有内容都相同(字体等)。似乎不一致在某种程度上与退格键与 a 的交互有关TextArea
,即使此 TextArea 与 Text 节点无关。
这是一个间歇性/非确定性问题,但其他人能够使用以下代码重现它:
public class Test extends Application {
private Text text = new Text();
public static void main(String... args) {
launch(args);
}
public void start(Stage stage) {
TextArea textArea = new TextArea();
textArea.textProperty().addListener((observable, oldValue, newValue) -> {
text.setText(newValue);
System.out.println(String.format("height=%.1f", text.getLayoutBounds().getHeight()));
});
stage.setScene(new Scene(new Group(textArea)));
stage.show();
}
}
Run Code Online (Sandbox Code Playgroud)
输入几个“X”字符,我得到(在 Windows 上):height=16.0
. 键入退格后(但当字符串中仍有几个“X”字符时),我得到height=17.0
. 如果我输入更多字符,高度保持17.0
,然后任意移回 16.0,然后是 17.0 等。其他用户报告不同类型的不稳定行为。
TextArea
用TextField
,问题消失(!)Text
节点在哪里初始化并不重要。valid
标志false
在所有情况下都在 中LazyBoundsProperty#get
,因此似乎为每个新字符重新计算边界。是什么导致了这种不一致,我该如何避免?
归档时间: |
|
查看次数: |
160 次 |
最近记录: |