Java AWT TextArea

Till now all the components we have looked at provide the functionality of displaying only a short text. The TextField provides only a single line message to be displayed. But in many practical cases, we require not a single line but an entire paragraph of text to be displayed. For such cases, we have the TextArea class. It provides a multi-line editor area. Users can interact with the text. Whenever the user tries to enter the text that is more than what can be displayed in a given area, a scrollbar appears automatically and allows the user to scroll through the entire text.

到现在为止,我们所研究的所有组件都提供了仅显示短文本的功能。 TextField仅提供要显示的单行消息。 但是在许多实际情况下,我们不需要显示一行,而是显示整个文本段。 对于这种情况,我们有TextArea类 。 它提供了多行编辑器区域。 用户可以与文本进行交互。 每当用户尝试输入的文本超过给定区域中显示的文本时,滚动条就会自动出现,并允许用户滚动浏览整个文本。

Consider the following code -

考虑以下代码-

import java.awt.*;

public class CreateTextArea{

     CreateTextArea()
     {
         Frame f = new Frame();
          
         TextArea t1 = new TextArea("This is text area 1.\n It has scrollbars\n on both sides.");
         TextArea t2 = new TextArea("This is text area 2.\n It has horizontal scrollbar only.", 2, 10, TextArea.SCROLLBARS_HORIZONTAL_ONLY);
         TextArea t3 = new TextArea();
         
         t3.setText("Adding new Text...");
         
         t1.setBounds(50,50, 100, 60);
         t2.setBounds(50,150, 100, 60);
         t3.setBounds(50, 250, 100, 50);
         
         f.setLayout(null);
         f.setVisible(true);
         f.setSize(300,300);
         
         t3.append("\nRows in t1 " + t1.getRows() + "\nColumns in t1 " + t1.getColumns());
                           
         f.add(t1);
         f.add(t2);
         f.add(t3);
     
     }
                  
        public static void main(String []args){
        CreateTextArea ob = new CreateTextArea();
        
     }
}

Output

输出量

Java AWT TextArea

We created 3 objects of the TextArea class. T1 is initialized with a String value. This is the text that is displayed on object creation. T2 has been created with a String value, the row size, the column size, and the scrollbars that will be visible in the TextArea. We have set the number of rows to be displayed as 2 and the number of columns as 10. By default, a TextArea has both the vertical and the horizontal scrollbars. However, in t2, we create and display only the horizontal scrollbar. The various values that can be set for this field are as follows -

我们创建了TextArea类的 3个对象。 T1用字符串值初始化。 这是在对象创建时显示的文本。 T2已创建为具有String值,行大小,列大小和在TextArea中可见的滚动条。 我们将要显示的行数设置为2,将列数设置为10。默认情况下, TextArea同时具有垂直和水平滚动条。 但是,在t2中 ,我们仅创建和显示水平滚动条。 可以为此字段设置的各种值如下:

    static int SCROLLBARS_BOTH - Both vertical and horizontal scrollbars
    static int SCROLLBARS_NONE - No scrollbars
    static int SCROLLBARS_HORIZONTAL_ONLY -Horizontal scrollbar only
    static int SCROLLBARS_VERTICAL_ONLY -Vertical scrollbar only

The setText() method is used to set the text that is displayed in the text area. Users can add, delete, append, and interact with the text being displayed. We can retrieve the text of the TextArea by using the getText() method. These methods are the same as in the TextField class.

setText()方法用于设置在文本区域中显示的文本。 用户可以添加,删除,追加和与正在显示的文本进行交互。 我们可以使用getText()方法检索TextArea的文本。 这些方法与TextField类中的方法相同。

The append() method is used to add/append text on the screen. Text is added at the end of the lines already present in the text area.

append()方法用于在屏幕上添加/添加文本。 在文本区域中已经存在的行的末尾添加文本。

    void append(String str)

The getRows() method returns an integer value, specifying the number of rows present in the text area.

getRows()方法返回一个整数值,指定文本区域中存在的行数。

    int getRows()

Similarly, the getColumns() method is used to retrieve the number of columns present in a text area.

同样, getColumns()方法用于检索文本区域中存在的列数。

    int getColumns()


翻译自: https://www.includehelp.com/java/awt-textarea.aspx

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值