GWT - 組合元件



介紹

組合元件是一種可以包裝另一個元件的元件型別,它隱藏了被包裝元件的方法。當新增到面板時,組合元件的行為與直接新增被包裝元件完全相同。組合元件用於透過將多個其他元件包含在一個面板中來建立一個單個元件。

類宣告

以下是com.google.gwt.user.client.ui.Composite類的宣告:

public abstract class Composite
   extends Widget

類建構函式

序號 建構函式及描述
1

Composite()

類方法

序號 函式名稱及描述
1

protected Widget getWidget()

為子類提供訪問定義此組合元件的最頂層元件的許可權。

2

protected void initWidget(Widget widget)

設定要由組合元件包裝的元件。

3

boolean isAttached()

確定此元件當前是否附加到瀏覽器的文件(即,此元件和底層瀏覽器文件之間存在不間斷的元件鏈)。

4

protected void onAttach()

當元件附加到瀏覽器的文件時,將呼叫此方法。

5

void onBrowserEvent(Event event)

每當接收到瀏覽器事件時觸發。

6

protected void onDetach()

當元件從瀏覽器的文件中分離時,將呼叫此方法。

7

protected void setWidget(Widget widget)

已棄用。請改用 initWidget(Widget)。

繼承的方法

此類繼承自以下類:

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

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

  • java.lang.Object

組合元件示例

此示例將引導您完成簡單的步驟,以展示在 GWT 中使用組合元件的方法。按照以下步驟更新我們在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;
}

以下是修改後的 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>Composite Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

讓我們看看 Java 檔案src/com.tutorialspoint/HelloWorld.java的內容,它將演示組合元件的使用。

package com.tutorialspoint.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class HelloWorld implements EntryPoint {

   /**
   * A composite of a TextBox and a CheckBox that optionally enables it.
   */
   private static class OptionalTextBox extends Composite implements
   ClickHandler {

      private TextBox textBox = new TextBox();
      private CheckBox checkBox = new CheckBox();

      /**
      * Constructs an OptionalTextBox with the given caption 
      * on the check.
      * @param caption the caption to be displayed with the check box
      */
      public OptionalTextBox(String caption) {
         // Place the check above the text box using a vertical panel.
         VerticalPanel panel = new VerticalPanel();
         // panel.setBorderWidth(1);
         panel.setSpacing(10);
         panel.add(checkBox);
         panel.add(textBox);

         textBox.setWidth("200");
         // Set the check box's caption, and check it by default.
         checkBox.setText(caption);
         checkBox.setValue(true);
         checkBox.addClickHandler(this);

         DecoratorPanel decoratorPanel = new DecoratorPanel();

         decoratorPanel.add(panel);
         // All composites must call initWidget() in their constructors.
         initWidget(decoratorPanel);
      }

      public void onClick(ClickEvent event) {
         if (event.getSource() == checkBox) {
            // When the check box is clicked,
            //update the text box's enabled state.
            textBox.setEnabled(checkBox.getValue());
         }
      }
   }

   public void onModuleLoad() {
      // Create an optional text box and add it to the root panel.
      OptionalTextBox otb = new OptionalTextBox("Check this to enable me");
      RootPanel.get().add(otb);
   }    
} 

完成所有更改後,讓我們像在GWT - 建立應用章節中一樣,在開發模式下編譯並執行應用程式。如果您的應用程式一切正常,這將產生以下結果:

GWT Composite Widget
gwt_layout_panels.htm
廣告
© . All rights reserved.