0

我有这个在应用程序方法上初始化的预定义类模板start(基本上,默认情况下它不是由用户定义的)。它充当root容器,并且已经附加到场景和初级阶段。用户只能将 main 指定content为其需要Nodeas 参数的方法之一,好吧,由于主要内容不会与窗口一起调整大小,我想将其大小绑定到root它是否是一个resizable节点,反之亦然(如果内容的大小更改为根/窗口)。

现在,我能想到的就是检查节点是否是instanceofa Region(这是最常见的布局容器),但也有一些节点resizable不是从它扩展而来的,例如WebView. 在那种情况下,我不想一一指定它们。有没有更方便的方法?


编辑

这就是我的做法。当默认指定内容的大小以及手动调整窗口大小时,两者都有效:

public class Template extends Region {

  /**
   * Sets the main content.
   */
  private Node content;
  public void addContent(Node content) {
    if (this.content != null) {
      getChildren().remove(this.content);
    }
    if (content!= null) {
      getChildren().add(content);

      // Bind size to the content if it is a resizable node.
      if (content instanceof Region) {
        prefWidthProperty().bind(((Region) content).widthProperty());
        prefHeightProperty().bind(((Region) content).heightProperty());
        // Min and max size as well...
      } else if (content instanceof WebView) {
        prefWidthProperty().bind(((WebView) content).widthProperty());
        prefHeightProperty().bind(((WebView) content).heightProperty());
        // ...
      }
    }
  }
  public void getContent() {
    return content;
  }

  @Override
  protected void layoutChildren() {
    super.layoutChildren();
    final Node content = getContent();

    // Layout containers automatically fill the entire root (or if it is a resizable node like WebView)
    if (content instanceof Region || content instanceof WebView) {
      content.resize(getWidth(), getHeight());
    }
  }
}
4

0 回答 0