GWT 的标题框架面板(使用 FIELDSET 和 LEGEND html 标签)

发布于 2024-08-07 00:32:56 字数 1249 浏览 10 评论 0原文

我正在尝试在 GWT 中创建一个带标题的边框框架,结果如下:

Legend+Fieldset

这可以使用HTML 字段集和图例标记,例如

<fieldset> 
    <legend>Connection parameters</legend>
    ... the rest ...
</fieldset>

我想在 GWT 中创建一个实现该功能的自定义小部件。我设法做到了这一点,但问题是,尽管我添加了处理程序,但小部件内部发生的事件(按钮单击等)不会被触发。

我的小部件的实现如下:

public class TitledPanel extends Widget {
private Element legend;
private Widget content = null;

public TitledPanel() {
    Element fieldset = DOM.createFieldSet();
    legend = DOM.createLegend();
    DOM.appendChild(fieldset, legend);
    setElement(fieldset);
}

public TitledPanel(String title) {
    this();
    setTitle(title);
}

@Override
public String getTitle() {
    return DOM.getInnerHTML(legend);
}

@Override
public void setTitle(String html) {
    DOM.setInnerHTML(legend, html);
}

public Widget getContent() {
    return content;
}

public void setContent(Widget content) {
    if (this.content != null) {
        DOM.removeChild(getElement(), this.content.getElement());
    }

    this.content = content;

    DOM.appendChild(getElement(), content.getElement());
}
}

我是否需要扩展 Composite,或者需要手动重新路由事件,或者还有其他方法吗?

I'm trying to create a titled border frame in GWT, which results in this:

Legend+Fieldset

This can be done using HTML fieldset and legend tags, such as

<fieldset> 
    <legend>Connection parameters</legend>
    ... the rest ...
</fieldset>

I want to create a custom widget in GWT that implements that. I managed to do that, but the problem is that events that happen inside the widget (button click etc) does not get fired although I have added the handler.

My implementation of the widget is as follows:

public class TitledPanel extends Widget {
private Element legend;
private Widget content = null;

public TitledPanel() {
    Element fieldset = DOM.createFieldSet();
    legend = DOM.createLegend();
    DOM.appendChild(fieldset, legend);
    setElement(fieldset);
}

public TitledPanel(String title) {
    this();
    setTitle(title);
}

@Override
public String getTitle() {
    return DOM.getInnerHTML(legend);
}

@Override
public void setTitle(String html) {
    DOM.setInnerHTML(legend, html);
}

public Widget getContent() {
    return content;
}

public void setContent(Widget content) {
    if (this.content != null) {
        DOM.removeChild(getElement(), this.content.getElement());
    }

    this.content = content;

    DOM.appendChild(getElement(), content.getElement());
}
}

Do I need to extend Composite, or need to manually reroute the events, or is there other ways?

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

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

发布评论

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

评论(2

未央 2024-08-14 00:32:56

我认为您正在寻找 标题面板

将其内容包裹在边框中的面板,其标题出现在边框的左上角。这是 fieldset HTML 元素的实现。

I think you're looking for CaptionPanel:

A panel that wraps its contents in a border with a caption that appears in the upper left corner of the border. This is an implementation of the fieldset HTML element.

述情 2024-08-14 00:32:56

我认为这里的问题是您只调用 DOM.appendChild - 这不会导致 TitledPanel 采用 Widget。正常的操作过程是扩展Composite,然后调用initWidget(Widget widget) - 在内部调用widget.setParent(this); code>,这又使父级采用此小部件并将其附加到浏览器的文档中。但是 com.google.gwt.user.client.ui.Widget.setParent(Widget) 仅在包中可见,因此您无法从代码中调用它(例如,在 DOM.appendChild 之后) )。

我建议阅读小部件最佳实践/小部件构建< /a>,尤其是自行清理和/或查看某些 GWT 小部件的源代码,以了解 GWT 如何查看自定义小部件创建。

并且,正如罗伯特建议的< /a>,CaptionPanel 是更安全的路线:)

I think the problem here is that you just call DOM.appendChild - this doesn't cause the TitledPanel to adopt the Widget. The normal course of action is that you extend Composite and then call initWidget(Widget widget) - inside the hood it calls widget.setParent(this);, which in turn makes the parent adopt this widget and attach it to the browser's document. However com.google.gwt.user.client.ui.Widget.setParent(Widget) is only package-visible so you can't call it from your code (after, for example, DOM.appendChild).

I'd recommend reading Widget Best Practices / Widget Building, especially the Clean up after yourself and/or look at the source code for some GWT Widgets, to get the idea how the GWT sees custom widget creation.

And, as Robert suggested, CaptionPanel is the safer route :)

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