在自定义的JavaFX UI控件中,我想在控件的角落放置一些文本。下面是我的皮肤类的源代码:JavaFX Text构造函数与重定位
double width = control.getWidth();
double height = control.getHeight();
Text test1Text = new Text(0, 0, "top left");
Text test2Text = new Text(0, height-1, "bottom left");
Text test3Text = new Text("top right");
test3Text.relocate(width - test3Text.getLayoutBounds().getWidth(), 0);
Text test4Text = new Text("bottom right");
test4Text.relocate(width - test4Text.getLayoutBounds().getWidth(), height-1);
不幸的是,它似乎有所作为,我是否在给定的坐标构建文本还是我构建它没有坐标和定位它在之后放置:
- 在第一种情况下,构造函数中的坐标将是文本的左下角坐标。
- 在第二种情况下,给定的坐标将是左上角的坐标。
这种奇怪行为的任何想法?
这听起来很合理。非常感谢! – winne2 2013-03-01 15:02:58
补充说明:实际上,可以通过setTextOrigin设置文本坐标系的原点:http://docs.oracle.com/javafx/2/api/javafx/scene/text/Text.html#setTextOrigin%28javafx。 geometry.VPos%29 您可以自行决定是否偏好相对于文本左上角,左下角,中心左侧或基线左侧的坐标。 – winne2 2013-03-07 13:38:53