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 { }

類建構函式

序號 建構函式 & 描述
1

HTML()

建立一個空的 HTML。

2

protected HTML(Element element)

子類可以使用此建構函式顯式使用現有元素。

3

HTML(java.lang.String html)

使用指定的 html 內容建立一個 HTML。

4

HTML(java.lang.String html, boolean wordWrap)

建立一個具有指定內容的 HTML 元件,可以選擇將其視為 HTML,並可以選擇停用自動換行。

類方法

序號 方法 & 描述
1

java.lang.String getHTML()

獲取此物件的 HTML 內容。

2

void setHTML(java.lang.String html)

透過 HTML 設定此物件的 HTML 內容。

3

static HTML wrap(Element element)

建立一個包裝現有 <div> 或 <span> 元素的 HTML 元件。

繼承的方法

此類繼承自以下類的方法:

  • 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 元件示例

此示例將引導您完成簡單的步驟,以演示如何在 GWT 中使用 HTML 元件。請按照以下步驟更新我們在GWT - 建立應用章節中建立的 GWT 應用程式:

步驟 描述
1 按照GWT - 建立應用章節中的說明,建立一個名為HelloWorld的專案,放在com.tutorialspoint包下。
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() {
      // create two HTML widgets
      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.");

      // use UIObject methods to set HTML widget properties.
      html1.addStyleName("gwt-Green-Border");
      html2.addStyleName("gwt-Blue-Border");

      // add widgets to the root panel.
      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.htm
廣告
© . All rights reserved.