父节点中的布局节点,但在计算宽度时忽略它

时间:2018-01-11 19:01:37

标签: javafx layout

所以我遇到一个问题,其中有一个节点,我想要渲染一个抬头显示器(HUD)。

当然,我的方法是创建StackPane并将hud置于所述节点的顶部并使鼠标透明。

现在 - HUD还需要一些布局(例如,在不同的角落放置不同的元素)。

现在让我们说hud中有一个Label,它显示一些长度应始终适合Label的字符串(因此字符串完全显示)。通过这种逻辑在HUD的右上方布置这样的变长标签,我们需要说hud有布局传递(所以它将标签放在角落里)。

但是,由于需要对HUD进行管理,因此可能存在文本Label的宽度大于底层(hud)后面的Node的情况,这会导致整个事件(StackPane)调整到HUD而不是实际内容。 HUD应该是布局的,但只有在可能的情况下,才不会被迫这样做。

我希望我解释得足够 - 这种布局是否有一些解决方案,或者我必须自己制作以及如何(提示)?

1 个答案:

答案 0 :(得分:0)

基于@James_D评论。使用StackPane作为基础,因为我很懒:P

public class HUDPane extends StackPane
{
private final Node content;
private final Node hud;

public HUDPane(Node content, Node hud)
{
    this.content = content;
    this.hud = hud;
    this.getChildren().addAll(this.content, this.hud);
}

@Override
protected void layoutChildren()
{
    super.layoutChildren();

    this.hud.resizeRelocate(0, 0, this.getWidth(), this.getHeight());
}

@Override
protected double computePrefWidth(double height)
{
    return this.content.prefWidth(height);
}

@Override
protected double computePrefHeight(double width)
{
    return this.content.prefHeight(width);
}

@Override
protected double computeMinWidth(double height)
{
    return this.content.minWidth(height);
}

@Override
protected double computeMinHeight(double width)
{
    return this.content.minHeight(width);
}

@Override
protected double computeMaxWidth(double height)
{
    return this.content.maxWidth(height);
}

@Override
protected double computeMaxHeight(double width)
{
    return this.content.maxHeight(width);
}
}