GWT - SimplePanel 元件



簡介

SimplePanel 元件表示面板的基本類,該面板僅包含一個元件。

類宣告

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

public class SimplePanel
   extends Panel
      implements HasOneWidget

類建構函式

序號 建構函式 & 描述
1

SimplePanel()

建立一個空面板,使用 DIV 作為其內容。

2

protected SimplePanel(Element elem)

建立一個空面板,使用指定的瀏覽器元素作為其內容。

類方法

序號 函式名稱 & 描述
1

void add(Widget w)

將一個元件新增到此面板。

2

protected Element getContainerElement()

重寫此方法以指定面板子元件的容器,而不是根元素。

3

Widget getWidget()

獲取面板的子元件。

4

java.util.Iterator<Widget> iterator()

獲取包含元件的迭代器。

5

boolean remove(Widget w)

移除一個子元件。

6

void setWidget(IsWidget w)

設定接收器的唯一元件,如果存在先前的元件則替換它。

7

void setWidget(Widget w)

設定此面板的元件。

繼承的方法

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

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

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

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

  • java.lang.Object

SimplePanel 元件示例

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

步驟 描述
1 GWT - 建立應用章節中說明的包com.tutorialspoint下建立一個名為HelloWorld的專案。
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>SimplePanel Widget Demonstration</h1>
      <div id = "gwtContainer"></div>
   </body>
</html>

讓我們在 Java 檔案src/com.tutorialspoint/HelloWorld.java中包含以下內容,這將演示 SimplePanel 元件的使用。

package com.tutorialspoint.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;

public class HelloWorld implements EntryPoint {

   public void onModuleLoad() {
      // Create a Simple Panel
      SimplePanel simplePanel = new SimplePanel();
      Label label = new Label("A Simple Label.");
      //add label to simple panel
      simplePanel.add(label);
	  //set height and width of simple panel
      simplePanel.setHeight("200");
      simplePanel.setWidth("200");

      DecoratorPanel decoratorPanel = new DecoratorPanel();
      decoratorPanel.add(simplePanel);

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

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

GWT SimplePanel Widget
gwt_layout_panels.htm
廣告

© . All rights reserved.