如何使用 uibinder 创建带有子级的 gwt 复合组件?

发布于 2024-10-02 20:45:06 字数 410 浏览 1 评论 0原文

我想创建一个组件来装饰它的子组件,例如:

mycomponent.ui.xml:

<g:FlowPanel addStyleNames="myStyle">
    <!-- how can i render children ? -->
</g:FlowPanel>

然后其他人可以使用:

<myapp:mycomponent>
    <g:Label>Decorated child</g:Label>
</myapp:mycomponent>

How can i render the child in uibinder? (或者用Java,如果我必须的话)

I would like to create a component to decorate its children, such as:

mycomponent.ui.xml:

<g:FlowPanel addStyleNames="myStyle">
    <!-- how can i render children ? -->
</g:FlowPanel>

and then others can use:

<myapp:mycomponent>
    <g:Label>Decorated child</g:Label>
</myapp:mycomponent>

How can i render the children in uibinder? (or in Java, if i must)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独留℉清风醉 2024-10-09 20:45:06

MyComponent 实现 HasWidgets 接口来添加/删除子部件。

MyComponent.ui.xml 看起来就像

<g:FlowPanel ui:field="main" />

将 ind HasWidgets 指定的方法委托给 FlowPanel 一样简单:

public class MyComponent extends Composite implements HasWidgets {

    private static MyComponentUiBinder uiBinder = GWT.create(MyComponentUiBinder.class);

    interface MyComponentUiBinder extends UiBinder<Widget, MyComponent> {}

    @UiField
    FlowPanel main;

    public MyComponent() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public void add(Widget w) {
        main.add(w);
    }

    @Override
    public void clear() {
        main.clear();
    }

    @Override
    public Iterator<Widget> iterator() {
        return main.iterator();
    } 

    @Override
    public boolean remove(Widget w) {
        return main.remove(w);
    }
}

调用

<M:MyComponent>
    <g:Label text="some text" />
</M:MyComponent>

将以这种方式进行。

Let MyComponent implement the HasWidgets interface for adding/removing child widgets.

The MyComponent.ui.xml looks as simple as

<g:FlowPanel ui:field="main" />

while you delegate the methods specified ind HasWidgets to the FlowPanel:

public class MyComponent extends Composite implements HasWidgets {

    private static MyComponentUiBinder uiBinder = GWT.create(MyComponentUiBinder.class);

    interface MyComponentUiBinder extends UiBinder<Widget, MyComponent> {}

    @UiField
    FlowPanel main;

    public MyComponent() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public void add(Widget w) {
        main.add(w);
    }

    @Override
    public void clear() {
        main.clear();
    }

    @Override
    public Iterator<Widget> iterator() {
        return main.iterator();
    } 

    @Override
    public boolean remove(Widget w) {
        return main.remove(w);
    }
}

Calling

<M:MyComponent>
    <g:Label text="some text" />
</M:MyComponent>

will work this way.

霞映澄塘 2024-10-09 20:45:06

使用此 XML:

<myapp:mycomponent>
    <g:Label>Decorated child</g:Label> 
</myapp:mycomponent>

将实例化 MyComponent,然后调用 MyComponent.add(label)。您所要做的就是重写类 MyComponent 中的 .add(..) 并应用您想要传递给组件的任何样式。

Using this XML:

<myapp:mycomponent>
    <g:Label>Decorated child</g:Label> 
</myapp:mycomponent>

will instantiate MyComponent and then call MyComponent.add(label). All you have to do is override .add(..) in your class MyComponent and apply any styles that you want to passed components.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文