GWT - FlowPanel 元件



簡介

FlowPanel 元件表示一個面板,它使用預設的 HTML 佈局行為來格式化其子元件。

類宣告

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

public class FlowPanel
   extends ComplexPanel
      implements InsertPanel.ForIsWidget

類建構函式

序號 建構函式 & 描述
1

FlowPanel()

空 FlowPanel 的建構函式。

類方法

序號 函式名稱 & 描述
1

void add(Widget w)

向面板新增一個新的子元件。

2

void clear()

移除所有子元件。

3

void insert(IsWidget w, int beforeIndex)

4

void insert(Widget w, int beforeIndex)

在指定索引之前插入一個元件。

繼承的方法

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

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

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

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

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

  • java.lang.Object

FlowPanel 元件示例

此示例將引導您完成簡單的步驟,以展示如何在 GWT 中使用 FlowPanel 元件。按照以下步驟更新我們在“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-CheckBox {
   margin: 10px;	
}

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

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

package com.tutorialspoint.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.RootPanel;

public class HelloWorld implements EntryPoint {

   public void onModuleLoad() {
      // Create a flow panel			
      FlowPanel flowPanel = new FlowPanel();

      // Add CheckBoxes to flow Panel
      for(int i = 1;  i <= 10; i++){
         CheckBox checkBox = new CheckBox("Item" + i);
         flowPanel.add(checkBox);
      }

      DecoratorPanel decoratorPanel = new DecoratorPanel();
      decoratorPanel.setWidth("500");
      decoratorPanel.add(flowPanel);

      // Add the widgets to the root panel.
      RootPanel.get().add(decoratorPanel);
   }
}

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

GWT FlowPanel Widget
gwt_layout_panels.htm
廣告

© . All rights reserved.