我的问题是:我想要一个水平方向和宽度与他的内容相匹配的窗格(比如FlowPane),但是如果宽度太高,窗格就会包装它的内容。我不想计算'prefWidth‘或'prefWrappingLength’的孩子宽度,因为他们是充足的。
在线程JavaFX FlowPane Autosize中,它们给出了包装文本的解决方案,但没有为布局提供解决方案。
你有什么建议给我吗?
发布于 2017-05-30 07:29:29
对于那些正在寻找答案的人来说,我终于做到了,忽略了大量的儿童约束:
class RuleBox extends FlowPane {
int maxWrapLength;
int margin = 30;
RuleBox(int maxWrapLength) {
super();
this.maxWrapLength = maxWrapLength;
getChildren().addListener((ListChangeListener<? super Node>) observable -> actualizeWrapLength(observable.getList()));
}
private void actualizeWrapLength(ObservableList<? extends Node> list) {
new Thread(() -> {
try { Thread.sleep(50);
} catch (InterruptedException ignored) {}
Platform.runLater(() -> {
int totalWidth = 0;
for(Node n : list) {
if(n instanceof Control) totalWidth+=((Control)n).getWidth();
else if(n instanceof Region) totalWidth+=((Region)n).getWidth();
}
if(totalWidth+margin>maxWrapLength) setPrefWrapLength(maxWrapLength);
else setPrefWrapLength(totalWidth+margin);
});
}).start();
}
void actualizeWrapLength() {
actualizeWrapLength(getChildren());
}
}这是一段非常脏的代码,特别是对于曾经具有子级宽度的Thread.sleep(50)来说。因此,如果有人拥有一个更好的解决方案,请给它!
https://stackoverflow.com/questions/44134687
复制相似问题