GWT - HTML 小部件

简介

HTML 小部件可以包含任意 HTML,它可以被解释为 HTML。此小部件使用 <div> 元素,使其以块布局显示。

类声明

以下是 com.google.gwt.user.client.ui.Label 类的声明 −

public class HTML
    extends Label
        implements HasHTML

CSS 样式规则

以下默认 CSS 样式规则将应用于所有 HTML 小部件。您可以根据您的要求覆盖它。

.gwt-HTML { }

类构造函数

Sr.No. 构造函数 &描述
1

HTML()

创建一个空的 HTML。

2

protected HTML(Element element)

子类可以使用此构造函数来明确使用现有元素。

3

HTML(java.lang.String html)

创建具有指定 html 内容的 HTML。

4

HTML(java.lang.String html,布尔值 wordWrap)

创建具有指定内容的 HTML 小部件,可选择将其视为 HTML,并可选择禁用自动换行。

类方法

Sr.No. 方法 &描述
1

java.lang.String getHTML()

以 HTML 形式获取此对象的内容。

2

void setHTML(java.lang.String html)

通过 HTML 设置此对象的内容。

3

static HTML wrap(Element element)

创建一个 HTML 小部件,用于包装现有的 <div> 或 <span> 元素。

继承的方法

该类继承了以下类的方法 −

  • com.google.gwt.user.client.ui.Label

  • com.google.gwt.user.client.ui.UIObject

  • com.google.gwt.user.client.ui.Widget

  • com.google.gwt.user.client.ui.HasText

  • java.lang.Object

Html Widget 示例

此示例将带您通过简单的步骤展示 GWT 中 HTML Widget 的使用方法。按照以下步骤更新我们在 GWT - 创建应用程序 一章中创建的 GWT 应用程序 −

步骤 描述
1 com.tutorialspoint 包下创建一个名为 HelloWorld 的项目,如 GWT - 创建应用程序 一章中所述。
2 修改 HelloWorld.gwt.xmlHelloWorld.cssHelloWorld.htmlHelloWorld.java,如下所述。其余文件保持不变。
3 编译并运行应用程序以验证实现逻辑的结果。

以下是修改后的模块描述符src/com.tutorialspoint/HelloWorld.gwt.xml的内容。

<?xml version = "1.0" encoding = "UTF-8"?>
<module rename-to = 'helloworld'>
   <!-- Inherit the core Web Toolkit stuff.                        -->
   <inherits name = 'com.google.gwt.user.User'/>

   <!-- Inherit the default GWT style sheet.                       -->
   <inherits name = 'com.google.gwt.user.theme.clean.Clean'/>

   <!-- Specify the app entry point class.                         -->
   <entry-point class = 'com.tutorialspoint.client.HelloWorld'/>

   <!-- Specify the paths for translatable code                    -->
   <source path = 'client'/>
   <source path = 'shared'/>

</module>

以下是修改后的样式表文件war/HelloWorld.css的内容。

body {
   text-align: center;
   font-family: verdana, sans-serif;
}

h1 {
   font-size: 2em;
   font-weight: bold;
   color: #777777;
   margin: 40px 0px 70px;
   text-align: center;
}

.gwt-Green-Border{ 
   border:1px solid green;
}

.gwt-Blue-Border{ 
   border:1px solid blue;
}

以下是修改后的 HTML 主机文件 war/HelloWorld.html 的内容。

<html>
   <head>
      <title>Hello World</title>
      <link rel = "stylesheet" href = "HelloWorld.css"/>
      <script language = "javascript" src = "helloworld/helloworld.nocache.js">
      </script>
   </head>

   <body>
      <h1>HTML Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

让我们来看看 Java 文件 src/com.tutorialspoint/HelloWorld.java 的以下内容,它将演示如何使用 HTML 小部件。

package com.tutorialspoint.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HelloWorld implements EntryPoint {
   public void onModuleLoad() {
      // 创建两个 HTML 小部件
      HTML html1 = 
      new HTML("This is first GWT HTML widget using <b> tag of html.");
      HTML html2 = 
      new HTML("This is second GWT HTML widget using <i> tag of html.");

      // 使用 UIObject 方法设置 HTML 小部件属性。
      html1.addStyleName("gwt-Green-Border");
      html2.addStyleName("gwt-Blue-Border");

      // 将小部件添加到根面板。
      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(10);
      panel.add(html1);
      panel.add(html2);
      
      RootPanel.get("gwtContainer").add(panel);
   }
}

完成所有更改后,让我们像在 GWT - 创建应用程序 一章中一样,在开发模式下编译并运行应用程序。如果您的应用程序一切正常,这将产生以下结果 −

GWT HTML Widget

gwt_basic_widgets.html