GWT - 文字框控制元件



簡介

TextBox 控制元件表示一個標準的單行文字框。

類宣告

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

public class TextBox
   extends TextBoxBase
      implements HasDirection

CSS 樣式規則

以下預設 CSS 樣式規則將應用於所有 TextBox 控制元件。您可以根據需要覆蓋它。

.gwt-TextBox {}

.gwt-TextBox-readonly {} 

類建構函式

序號 建構函式 & 描述
1

TextBox()

建立一個空的文字框。

2

TextBox(Element element)

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

類方法

序號 函式名稱 & 描述
1

HasDirection.Direction getDirection()

獲取控制元件的方向性。

2

int getMaxLength()

獲取文字框的最大允許長度。

3

int getVisibleLength()

獲取文字框中可見字元的數量。

4

void setDirection(HasDirection.Direction direction)

設定控制元件的方向性。

5

void setMaxLength(int length)

設定文字框的最大允許長度。

6

void setVisibleLength(int length)

設定文字框中可見字元的數量。

7

static TextBox wrap(Element element)

建立一個 TextBox 控制元件,包裝現有的 <input type='text'> 元素。

繼承的方法

此類繼承自以下類:

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

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

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

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

  • java.lang.Object

TextBox 控制元件示例

此示例將引導您完成簡單的步驟,以演示如何在 GWT 中使用 TextBox 控制元件。按照以下步驟更新我們在“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-TextBox {
   color: green; 
}

.gwt-TextBox-readonly {
   background-color: yellow;	
}

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

讓我們將 Java 檔案 src/com.tutorialspoint/HelloWorld.java 的內容如下,它將演示 TextBox 控制元件的使用。

package com.tutorialspoint.client;

import com.google.gwt.core.client.EntryPoint;
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 {
   public void onModuleLoad() {
      //create textboxes
      TextBox textBox1 = new TextBox(); 
      TextBox textBox2 = new TextBox();

      //add text to text box
      textBox2.setText("Hello World!");

      //set textbox as readonly
      textBox2.setReadOnly(true);

      // Add text boxes to the root panel.
      VerticalPanel panel = new VerticalPanel();
      panel.setSpacing(10);
      panel.add(textBox1);
      panel.add(textBox2);

      RootPanel.get("gwtContainer").add(panel);
   }	
}

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

GWT TextBox Widget
gwt_form_widgets.htm
廣告

© . All rights reserved.