首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaFX - FlowPane自动尺寸

JavaFX - FlowPane自动尺寸
EN

Stack Overflow用户
提问于 2017-05-23 12:21:36
回答 1查看 810关注 0票数 1

我的问题是:我想要一个水平方向和宽度与他的内容相匹配的窗格(比如FlowPane),但是如果宽度太高,窗格就会包装它的内容。我不想计算'prefWidth‘或'prefWrappingLength’的孩子宽度,因为他们是充足的。

在线程JavaFX FlowPane Autosize中,它们给出了包装文本的解决方案,但没有为布局提供解决方案。

你有什么建议给我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-30 07:29:29

对于那些正在寻找答案的人来说,我终于做到了,忽略了大量的儿童约束:

代码语言:javascript
复制
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)来说。因此,如果有人拥有一个更好的解决方案,请给它!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44134687

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档