【发布时间】:2015-09-12 05:04:19
【问题描述】:
我正在开发一个 javaFX 项目,在该项目中我需要精确控制 Node 的大小。 我的代码当前在需要时正确更新了 prefWidthProperty,并且最小和最大尺寸设置为使用 pref 尺寸,但 widthProperty 没有更新。
我已经通过将打印语句添加为属性的侦听器来验证这一点。 PrefWidthProperty 更新,widthProperty 不更新(直到我单击节点,可能获得焦点?)
我尝试在其自身及其父级上请求布局,与焦点相同,但没有成功。
我目前真的不知道如何修复这个非常烦人的错误。似乎目前一切都落后于 1 个布局传递。
用于设置大小和检查更改的代码:
//Set the size
this.setMinSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
this.setMaxSize(Region.USE_PREF_SIZE, Region.USE_PREF_SIZE);
this.prefWidthProperty().bind(getTotalWidthProperty());
public ObservableValue<? extends Number> getTotalWidthProperty() {
return rightArgument.layoutXProperty().add(rightArgument.translateXProperty()).add(rightArgument.widthProperty()).add(H_GAP);
}
//Check for change:
this.prefWidthProperty().addListener(p -> System.out.println("FB_Pref_"+System.currentTimeMillis() + " " + this.getPrefWidth()));
this.widthProperty().addListener(p -> System.out.println("FB______"+System.currentTimeMillis() + " " + this.getWidth()));
如前所述,FB_Pref 会按时正确打印,而 FB_____ 打印得太晚了。
【问题讨论】:
-
能否提供更多代码?
-
您还想看哪些部分?我添加了一些关于如何设置 prefWidthProperty 的内容。
标签: java user-interface layout properties javafx