如何基于窗格

时间:2017-08-16 10:20:31

标签: javafx javafx-8

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

因此,例如,它可能包含GridPane TextFieldButtonCheckBox

我不想继承PaneGridPane,因为我不想将这些API暴露给用户。因此,"一个由网格窗格组成的节点"而不是"一个扩展网格窗格的节点"。

我发现可以延长RegionControl,这是推荐的吗?将大小调整和布局委派给窗格需要什么?

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

1 个答案:

答案 0 :(得分:1)

扩展区域,并覆盖layoutChildren方法。

您可以使用Region.snappedTopInset()方法(以及底部,左侧和右侧)来获取BasePaneControl的位置。然后根据可能属于BasePaneControl的其他组件计算您想要Pane的位置。

了解了窗格的位置后,请致电resizeRelocate

/**
 * 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);
}