Java 如何更改HTMLPanel的html

Java 如何更改HTMLPanel的html,java,html,gwt,panel,Java,Html,Gwt,Panel,我想声明一个HTMLPanel的子类。 在它的构造函数中,我想给它一些参数来构造包含html的 因为我必须调用超级构造函数作为第一条语句,所以我以后必须在构造函数中更改html 我该怎么做 public class MyHTMLPanel extends HTMLPanel { public MyHTMLPanel(String id, int anotherParameter) { super(""); String html="" // ... some code

我想声明一个HTMLPanel的子类。 在它的构造函数中,我想给它一些参数来构造包含html的

因为我必须调用超级构造函数作为第一条语句,所以我以后必须在构造函数中更改html

我该怎么做

public class MyHTMLPanel extends HTMLPanel
{ 
  public MyHTMLPanel(String id, int anotherParameter)
  { super("");
     String html=""
    // ... some code th construct the html
    //??? this.setHtml(html);  
  }  
}

你可以在下面找到一个我用过的例子,它对我很有用。 我不记得为什么我不将HTMLPanel子类,不管这是不是一个很好的理由。 如果在一个页面中包含多个相同类型的对象,您将注意到一种随机化html ID的机制

public abstract class HtmlPanelBase extends Composite
{
  private String _dynPostfix = "";
  protected final String id(final String staticId) { return staticId + _dynPostfix; }
  private final String wrapId(final String id) { return "id=\"" + id + "\""; }
  private final String wrapDynId(final String refId) { return wrapId(id(refId)); }

  private String _htmlAsText = null;
  public String getHtmlAsText() { return _htmlAsText; }

  abstract protected String htmlPanelBundleHtmlText();
  abstract protected List<String> idList();

  protected HTMLPanel _holder = null;
  private HTMLPanel createHtmlPanel(final boolean defineGloballyUniqueIds)
  {
    // Referent HTML panel text containing the reference id's.
    _htmlAsText = htmlPanelBundleHtmlText();
    if (defineGloballyUniqueIds)
    {
      // List of id's in the HTML Panel reference page to replace with dynamic/unique id's.
      final List<String> refIdList = idList();
      // Replace the reference id's with dynamic/unique id's.
      for (String refId : refIdList)
        _htmlAsText = _htmlAsText.replace(wrapId(refId), wrapDynId(refId));
    }
    // Return the HTMLPanel containing the globally unique id's.
    return new HTMLPanel(_htmlAsText);
  }
  public HtmlPanelBase(final boolean defineGloballyUniqueIds)
  {
    setup(defineGloballyUniqueIds);
    initWidget(_holder);
  }

  private void setup(final boolean defineGloballyUniqueIds)
  {
    if (defineGloballyUniqueIds)
      _dynPostfix = "_" + UUID.uuid().replace("-", "_");
    _holder = createHtmlPanel(defineGloballyUniqueIds);
  }
}
公共抽象类HtmlPanelBase扩展了复合
{
私有字符串_dynPostfix=“”;
受保护的最终字符串id(最终字符串staticId){return staticId+\u dynPostfix;}
私有最终字符串wrapId(最终字符串id){return“id=\”“+id+\”“;}
私有最终字符串wrapDynId(最终字符串refId){return wrapId(id(refId));}
私有字符串_htmlAsText=null;
公共字符串getHtmlastText(){return\u HtmlastText;}
抽象保护字符串htmlPanelBundleHtmlText();
抽象受保护列表idList();
受保护的HTMLPanel _holder=null;
私有HTMLPanel createHtmlPanel(最终布尔定义GloballyUniqueids)
{
//包含引用id的引用HTML面板文本。
_HtmlastText=htmlPanelBundleHtmlText();
如果(定义全局全局Yuniquids)
{
//HTML面板参考页中要替换为动态/唯一id的id列表。
最终列表refIdList=idList();
//将引用id替换为动态/唯一id。
用于(字符串refId:refIdList)
_htmlAsText=_htmlAsText.replace(wrapId(refId),wrapDynId(refId));
}
//返回包含全局唯一id的HTMLPanel。
返回新的HTMLPanel(\u htmlAsText);
}
公共HtmlPanelBase(最终布尔定义GloballyUniqueids)
{
设置(defineGloballyUniqueIds);
initWidget(_holder);
}
专用void设置(最终布尔定义globallyuniqueids)
{
如果(定义全局全局Yuniquids)
_dynPostfix=““+UUID.UUID()。替换(“-”,“”);
_holder=createHtmlPanel(定义全局全局Yuniquids);
}
}
现在,您如何从上述基础中创建子类:

public class HtmlPanelTemplate extends HtmlPanelBase
{
  private final static boolean _defineGloballyUniqueIds = false;
  private final static int _numIdCapacity = 40;

  public HtmlPanelTemplate()
  {
    super(_defineGloballyUniqueIds);
    setup();
  }

  @Override
  protected String htmlPanelBundleHtmlText()
  {
    return YourClientBundle.INSTANCE.getYourFileHtml().getText();
  }

  @Override
  protected List<String> idList()
  {
    final List<String> idList = new ArrayList<String>(_numIdCapacity);
    return idList;
  }

  private void setup()
  {
  }
}
公共类HtmlPanelTemplate扩展HtmlPanelBase
{
私有最终静态布尔值_defineGloballyUniqueIds=false;
专用最终静态整数_numIdCapacity=40;
公共HtmlPanelTemplate()
{
超级(_defineGloballyUniqueIds);
设置();
}
@凌驾
受保护的字符串htmlPanelBundleHtmlText()
{
返回您的ClientBundle.INSTANCE.getYourFileHtml().getText();
}
@凌驾
受保护列表idList()
{
最终列表idList=新的ArrayList(_numIdCapacity);
返回懒汉;
}
私有无效设置()
{
}
}

你可以在下面找到一个我用过的、对我很有用的例子。 我不记得为什么我不将HTMLPanel子类,不管这是不是一个很好的理由。 如果在一个页面中包含多个相同类型的对象,您将注意到一种随机化html ID的机制

public abstract class HtmlPanelBase extends Composite
{
  private String _dynPostfix = "";
  protected final String id(final String staticId) { return staticId + _dynPostfix; }
  private final String wrapId(final String id) { return "id=\"" + id + "\""; }
  private final String wrapDynId(final String refId) { return wrapId(id(refId)); }

  private String _htmlAsText = null;
  public String getHtmlAsText() { return _htmlAsText; }

  abstract protected String htmlPanelBundleHtmlText();
  abstract protected List<String> idList();

  protected HTMLPanel _holder = null;
  private HTMLPanel createHtmlPanel(final boolean defineGloballyUniqueIds)
  {
    // Referent HTML panel text containing the reference id's.
    _htmlAsText = htmlPanelBundleHtmlText();
    if (defineGloballyUniqueIds)
    {
      // List of id's in the HTML Panel reference page to replace with dynamic/unique id's.
      final List<String> refIdList = idList();
      // Replace the reference id's with dynamic/unique id's.
      for (String refId : refIdList)
        _htmlAsText = _htmlAsText.replace(wrapId(refId), wrapDynId(refId));
    }
    // Return the HTMLPanel containing the globally unique id's.
    return new HTMLPanel(_htmlAsText);
  }
  public HtmlPanelBase(final boolean defineGloballyUniqueIds)
  {
    setup(defineGloballyUniqueIds);
    initWidget(_holder);
  }

  private void setup(final boolean defineGloballyUniqueIds)
  {
    if (defineGloballyUniqueIds)
      _dynPostfix = "_" + UUID.uuid().replace("-", "_");
    _holder = createHtmlPanel(defineGloballyUniqueIds);
  }
}
公共抽象类HtmlPanelBase扩展了复合
{
私有字符串_dynPostfix=“”;
受保护的最终字符串id(最终字符串staticId){return staticId+\u dynPostfix;}
私有最终字符串wrapId(最终字符串id){return“id=\”“+id+\”“;}
私有最终字符串wrapDynId(最终字符串refId){return wrapId(id(refId));}
私有字符串_htmlAsText=null;
公共字符串getHtmlastText(){return\u HtmlastText;}
抽象保护字符串htmlPanelBundleHtmlText();
抽象受保护列表idList();
受保护的HTMLPanel _holder=null;
私有HTMLPanel createHtmlPanel(最终布尔定义GloballyUniqueids)
{
//包含引用id的引用HTML面板文本。
_HtmlastText=htmlPanelBundleHtmlText();
如果(定义全局全局Yuniquids)
{
//HTML面板参考页中要替换为动态/唯一id的id列表。
最终列表refIdList=idList();
//将引用id替换为动态/唯一id。
用于(字符串refId:refIdList)
_htmlAsText=_htmlAsText.replace(wrapId(refId),wrapDynId(refId));
}
//返回包含全局唯一id的HTMLPanel。
返回新的HTMLPanel(\u htmlAsText);
}
公共HtmlPanelBase(最终布尔定义GloballyUniqueids)
{
设置(defineGloballyUniqueIds);
initWidget(_holder);
}
专用void设置(最终布尔定义globallyuniqueids)
{
如果(定义全局全局Yuniquids)
_dynPostfix=““+UUID.UUID()。替换(“-”,“”);
_holder=createHtmlPanel(定义全局全局Yuniquids);
}
}
现在,您如何从上述基础中创建子类:

public class HtmlPanelTemplate extends HtmlPanelBase
{
  private final static boolean _defineGloballyUniqueIds = false;
  private final static int _numIdCapacity = 40;

  public HtmlPanelTemplate()
  {
    super(_defineGloballyUniqueIds);
    setup();
  }

  @Override
  protected String htmlPanelBundleHtmlText()
  {
    return YourClientBundle.INSTANCE.getYourFileHtml().getText();
  }

  @Override
  protected List<String> idList()
  {
    final List<String> idList = new ArrayList<String>(_numIdCapacity);
    return idList;
  }

  private void setup()
  {
  }
}
公共类HtmlPanelTemplate扩展HtmlPanelBase
{
私有最终静态布尔值_defineGloballyUniqueIds=false;
专用最终静态整数_numIdCapacity=40;
公共HtmlPanelTemplate()
{
超级(_defineGloballyUniqueIds);
设置();
}
@凌驾
受保护的字符串htmlPanelBundleHtmlText()
{
返回您的ClientBundle.INSTANCE.getYourFileHtml().getText();
}
@凌驾
受保护列表idList()
{
最终列表idList=新的ArrayList(_numIdCapacity);
返回懒汉;
}
私有无效设置()
{
}
}

您不需要为HTMLPanel创建子类。您可以创建一个简单的复合小部件:

public class myPanel extends Composite {

    private HTMLPanel panel = new HTMLPanel();

    public myPanel(String id, int anotherParameter) {
        // set HTML to panel based on your parameters
        initWidget(panel);
    }
}

您不需要为HTMLPanel创建子类。您可以创建一个简单的复合小部件:

public class myPanel extends Composite {

    private HTMLPanel panel = new HTMLPanel();

    public myPanel(String id, int anotherParameter) {
        // set HTML to panel based on your parameters
        initWidget(panel);
    }
}
不知道这在派生类的构造函数中是否有效。但是为特定内容文本设置类并不是一个好的解决方案


不知道这在派生类的构造函数中是否有效。但是,为特定内容文本设置类并不是一个好的解决方案。

这是如何回答问题的?它没有回答问题。它提供了问题中问题的解决方案。你可能更喜欢一个不同的解决方案——这是你的特权,但投反对票显然是不礼貌的。下一个答案提供了相同的方法(扩展复合)和h