Gwt-html-widget

提供:Dev Guides
移動先:案内検索

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. Constructor & Description
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として処理し、オプションでワードラッピングを無効にします。

クラスメソッド

Sr.No. Method & Description
1

java.lang.String getHTML()

このオブジェクトのコンテンツをHTMLとして取得します。

2

void setHTML(java.lang.String 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アプリケーションを更新します-アプリケーションの作成_の章-

Step Description
1 Create a project with a name HelloWorld under a package com.finddevguides as explained in the GWT - Create Application chapter.
2 Modify HelloWorld.gwt.xml, HelloWorld.css, HelloWorldl and HelloWorld.java as explained below. Keep rest of the files unchanged.
3 Compile and run the application to verify the result of the implemented logic.

以下は、変更されたモジュール記述子 src/com.finddevguides/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.finddevguides.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/HelloWorldl の内容です。

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

HTMLウィジェットの使用方法を示すJavaファイル src/com.finddevguides/HelloWorld.java の内容を見てみましょう。

package com.finddevguides.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);
   }
}

すべての変更が完了したら、link:/gwt/gwt_create_application [GWT-アプリケーションの作成]の章で行ったように、アプリケーションをコンパイルして開発モードで実行します。 すべてがあなたのアプリケーションでうまくいけば、これは次の結果を生成します-

GWT HTMLウィジェット