- GWT 教程
- GWT - 首頁
- GWT - 概述
- GWT - 環境設定
- GWT - 應用程式
- GWT - 建立應用程式
- GWT - 部署應用程式
- GWT - 使用 CSS 樣式
- GWT - 基本小部件
- GWT - 表單小部件
- GWT - 複雜小部件
- GWT - 佈局面板
- GWT - 事件處理
- GWT - 自定義小部件
- GWT - UIBinder
- GWT - RPC 通訊
- GWT - JUnit 整合
- GWT - 除錯應用程式
- GWT - 國際化
- GWT - 歷史類
- GWT - 書籤支援
- GWT - 日誌框架
- GWT 有用資源
- GWT - 問答
- GWT - 快速指南
- GWT - 有用資源
- GWT - 討論
GWT - 文字區域小部件
介紹
TextArea 小部件表示一個文字框,允許輸入多行文字。
類宣告
以下是 com.google.gwt.user.client.ui.TextArea 類的宣告:
public class TextArea
extends TextBoxBase
implements HasDirection
CSS 樣式規則
以下預設 CSS 樣式規則將應用於所有 TextBox 小部件。您可以根據需要覆蓋它。
.gwt-TextArea {}
.gwt-TextArea-readonly {}
類建構函式
| 序號 | 建構函式和描述 |
|---|---|
| 1 |
TextArea() 建立一個空的文字區域。 |
類方法
| 序號 | 函式名稱和描述 |
|---|---|
| 1 |
int getCharacterWidth() 獲取文字框的請求寬度(這不是一個精確的值,因為並非所有字元都是相同的)。 |
| 2 |
int getCursorPos() 獲取游標的當前位置(這也充當文字選擇的開始)。 |
| 3 |
HasDirection.Direction getDirection() 獲取小部件的方向性。 |
| 4 |
int getSelectionLength() 獲取當前文字選擇的長度。 |
| 5 |
int getVisibleLines() 獲取可見的文字行數。 |
| 6 |
void setCharacterWidth(int width) 設定文字框的請求寬度(這不是一個精確的值,因為並非所有字元都是相同的)。 |
| 7 |
void setDirection(HasDirection.Direction direction) 設定小部件的方向性。 |
| 8 |
void setVisibleLines(int lines) 設定可見的文字行數。 |
繼承的方法
此類繼承自以下類:
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 | 建立一個名為HelloWorld的專案,位於com.tutorialspoint包下,如GWT - 建立應用程式章節中所述。 |
| 2 | 修改HelloWorld.gwt.xml、HelloWorld.css、HelloWorld.html和HelloWorld.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-TextArea {
color: green;
}
.gwt-TextArea-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>TextArea 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.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
public class HelloWorld implements EntryPoint {
public void onModuleLoad() {
//create textarea elements
TextArea textArea1 = new TextArea();
TextArea textArea2 = new TextArea();
//set width as 10 characters
textArea1.setCharacterWidth(20);
textArea2.setCharacterWidth(20);
//set height as 5 lines
textArea1.setVisibleLines(5);
textArea2.setVisibleLines(5);
//add text to text area
textArea2.setText(" Hello World! \n Be Happy! \n Stay Cool!");
//set textbox as readonly
textArea2.setReadOnly(true);
// Add text boxes to the root panel.
VerticalPanel panel = new VerticalPanel();
panel.setSpacing(10);
panel.add(textArea1);
panel.add(textArea2);
RootPanel.get("gwtContainer").add(panel);
}
}
準備好所有更改後,讓我們像在GWT - 建立應用程式章節中一樣,在開發模式下編譯並執行應用程式。如果您的應用程式一切正常,則將生成以下結果: