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.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-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 - 创建应用程序章节中一样,在开发模式下编译并运行应用程序。如果您的应用程序一切正常,则将生成以下结果:

GWT TextArea Widget
gwt_form_widgets.htm
广告

© . All rights reserved.