如何基于窗格实现自定义JavaFX控件

如何基于窗格实现自定义JavaFX控件,javafx,javafx-8,Javafx,Javafx 8,我希望在JavaFX8中实现一个基本的、可扩展的自定义控件,包括一个窗格,其中添加了其他控件 因此,例如,它可能包括一个容纳文本字段的网格窗格、一个按钮和一个复选框 我不想子类化窗格或网格窗格,因为我不想向用户公开这些API。因此,“由网格窗格组成的节点”与“扩展网格窗格的节点”相对 我发现扩展区域或控制是可能的,建议使用哪种方法?将大小和布局委派给窗格需要什么 public class BasePaneControl extends Control { private final Pa

我希望在JavaFX8中实现一个基本的、可扩展的自定义控件,包括一个窗格,其中添加了其他控件

因此,例如,它可能包括一个容纳
文本字段的
网格窗格
、一个
按钮
和一个
复选框

我不想子类化
窗格
网格窗格
,因为我不想向用户公开这些API。因此,“由网格窗格组成的节点”与“扩展网格窗格的节点”相对

我发现扩展
区域
控制
是可能的,建议使用哪种方法?将大小和布局委派给窗格需要什么

public class BasePaneControl extends Control {
    private final Pane pane;

    public BasePaneControl(Pane pane) {
        this.pane = pane;
        getChildren().add(pane);
    }

    // What do I need to delegate here to the pane to get sizing
    // to affect and be calculated by the pane?
}

public class MyControl extends BasePaneControl {
    private final GridPane gp = new GridPane();
    public MyControl() {
        super(gp);
        gp.add(new TextField(), 0, 0);
        gp.add(new CheckBox(), 0, 1);
        gp.add(new Button("Whatever"), 0, 2);
    }

    // some methods to manage how the control works.
}

我需要帮助实现上面的
BasePaneControl

扩展区域,并重写该方法

可以使用Region.snapdtopinset()方法(以及bottom、left和right)获取BasePaneControl的位置。然后根据可能是BasePaneControl一部分的其他组件计算您想要的窗格的位置

知道窗格的位置后,请致电

/**
 * Invoked during the layout pass to layout this node and all its content.
 */
@Override protected void layoutChildren() {
    // dimensions of this region
    final double width = getWidth();
    final double height = getHeight();

    // coordinates for placing pane
    double top = snappedTopInset();
    double left = snappedLeftInset();
    double bottom = snappedBottomInset();
    double right = snappedRightInset();

    // adjust dimensions for pane based on any nodes that are part of BasePaneControl
    top += titleLabel.getHeight();
    left += someOtherNode.getWidth();

    // layout pane
    pane.resizeRelocate(left,top,width-left-right,height-top-bottom);
}