JavaFX-FlowPane自动调整大小

JavaFX-FlowPane自动调整大小,java,javafx,javafx-8,flowpane,Java,Javafx,Javafx 8,Flowpane,这是我的问题:我希望得到一个水平方向的窗格,并且其宽度与他的内容相匹配(如FlowPane),但是如果宽度太高,则窗格会包装其内容。我不想用孩子们的宽度来计算“prefWidth”或“prefWrappingLength”,因为他们数量很多 在线程中,他们给出了包装文本的解决方案,而不是布局的解决方案 你有什么建议给我吗?对于那些正在寻找答案的人,我终于做到了,忽略了孩子太多的限制: class RuleBox extends FlowPane { int maxWrapLength;

这是我的问题:我希望得到一个水平方向的窗格,并且其宽度与他的内容相匹配(如FlowPane),但是如果宽度太高,则窗格会包装其内容。我不想用孩子们的宽度来计算“prefWidth”或“prefWrappingLength”,因为他们数量很多

在线程中,他们给出了包装文本的解决方案,而不是布局的解决方案


你有什么建议给我吗?

对于那些正在寻找答案的人,我终于做到了,忽略了孩子太多的限制:

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());
    }
}
类规则框扩展了FlowPane{
int maxWrapLength;
整数裕度=30;
规则框(int-maxWrapLength){
超级();
this.maxWrapLength=maxWrapLength;

getChildren().addListener((ListChangeListener对于那些正在寻找答案的人,我最终做了以下几点,忽略了丰富的儿童约束:

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());
    }
}
类规则框扩展了FlowPane{
int maxWrapLength;
整数裕度=30;
规则框(int-maxWrapLength){
超级();
this.maxWrapLength=maxWrapLength;
getChildren().addListener((ListChangeListener