我需要在JavaFX 2.1中动态创建Pane上的矩形。接下来,我需要在矩形上居中/换行/截断文本。文本必须适合矩形内。我能够通过以下代码对文本进行居中和换行,但是如果文本长度过长,它将出现在矩形外面。我想创建类似于StackPane内的标签的行为,实际上,如果矩形增长,则文本会随之增长,但始终保持在矩形的中心,如果文本无法适合矩形,则相应地被截断。
Rectangle r;
Text t;
...
//center and wrap text within rectangle
t.wrappingWidthProperty().bind(rect.widthProperty().multiply(0.9);
t.xProperty().bind(rect.xProperty().add(rect.widthProperty().subtract(t.boundsInLocalProperty().getValue().getWidth().divide(2)));
t.yProperty().bind(rect.yProperty().add(rect.heightProperty().divide(2)));
t.setTextAlignment(TextAlignment.CENTER);
t.setTextOrigin(VPos.CENTER);
我该使用什么属性来实现这样的效果,或者有更好的方法吗?