获取节点的维度

时间:2017-04-04 09:03:46

标签: java javafx

我正在使用JavaFX中的一个小程序,并尝试遵循MVC模型。 我有一个包含信息的类,2个字符串列表和一个标题,它们作为模型。我有另一个类,它是信息的直观表示,是视图。我想得到尺寸(高度和宽度),最好是在信息类中。

我将在Information类中创建的height和width属性绑定到Visual类中的属性。我的问题是,我希望这些高度和宽度在它们被渲染后立即显示,但似乎它们不会立即更新。我该如何解决这个问题?

public class Information {

    private String title;
    private List<String> info1;
    private List<String> info2;

    private SimpleDoubleProperty height = new SimpleDoubleProperty();
    private SimpleDoubleProperty width = new SimpleDoubleProperty();

    public String getTitle() {
        return title;
    }

    public List<String> getInfo1() {
        return info1;
    }

    public List<String> getInfo2() {
        return info2;
    }

    public double getHeight() {
        return height.get();
    }

    public SimpleDoubleProperty heightProperty() {
        return height;
    }

    public void setHeight(double height) {
        this.height.set(height);
    }

    public double getWidth() {
        return width.get();
    }

    public SimpleDoubleProperty widthProperty() {
        return width;
    }

    public void setWidth(double width) {
        this.width.set(width);
    }
}

public class Visual extends VBox {

    public Visual(Information i) {
        getChildren().add(new Label(i.getTitle()));

        for (String s1: i.getInfo1()) {
            getChildren().add(new Label(s1));
        }

        for (String s2: i.getInfo2()) {
            getChildren().add(new Label(s2));
        }

        i.widthProperty().bind(widthProperty());
        i.heightProperty().bind(heightProperty());
    }
}

0 个答案:

没有答案