我需要在JavaFX 2.1中动态创建窗格上的矩形.接下来我需要在Rectangle上居中/包装/截断Text.文本必须适合矩形.我能够使用以下代码对文本进行居中和包装,但是,如果文本长度太长,它将显示在矩形之外.我想在StackPane中创建类似Label的行为,基本上如果Rectangle增长,Text将随之增长但始终保持在Rectangle的中心,如果Text不能适合Rectangle,它将相应地被截断.
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);
Run Code Online (Sandbox Code Playgroud)
我可以使用哪些属性来实现这一目标,还是有更好的方法来实现这一目标?