AWT TextArea類


介紹

AWT TextArea控制元件提供給我們多行編輯區。使用者可以在此處鍵入盡可能多他想要的。當文字在文字區域中成為大於可視區域,捲動條會自動出現,幫助我們捲動文字向上和向下,左與右。

 

類的宣告

以下是宣告為java.awt.TextArea類:

public class TextArea
   extends TextComponent

欄位域

以是是類java.awt.TextArea欄位:

  • static int SCROLLBARS_BOTH -- 建立和顯示垂直和水平捲動條。

  • static int SCROLLBARS_HORIZONTAL_ONLY -- 建立和水平捲動條顯示。

  • static int SCROLLBARS_NONE --不要建立或顯示任何文字區域的捲動條。

  • static int SCROLLBARS_VERTICAL_ONLY -- 建立和垂直捲動條顯示。

類別建構函式

S.N. 建構函式與說明
1 TextArea() 
Constructs a new text area with the empty string as text.
2 TextArea(int rows, int columns) 
Constructs a new text area with the specified number of rows and columns and the empty string as text.
3 TextArea(String text) 
Constructs a new text area with the specified text.
4 TextArea(String text, int rows, int columns) 
Constructs a new text area with the specified text, and with the specified number of rows and columns.
5 TextArea(String text, int rows, int columns, int scrollbars) 
Constructs a new text area with the specified text, and with the rows, columns, and scroll bar visibility as specified.

類方法

S.N. 方法& 描述
1 void addNotify() 
Creates the TextArea's peer.
2 void append(String str) 
Appends the given text to the text area's current text.
3 void appendText(String str) 
Deprecated. As of JDK version 1.1, replaced by append(String).
4 AccessibleContext getAccessibleContext() 
Returns the AccessibleContext associated with this TextArea.
5 int getColumns() 
Returns the number of columns in this text area.
6 Dimension getMinimumSize() 
Determines the minimum size of this text area.
7 Dimension getMinimumSize(int rows, int columns) 
Determines the minimum size of a text area with the specified number of rows and columns.
8 Dimension getPreferredSize() 
Determines the preferred size of this text area.
9 Dimension getPreferredSize(int rows, int columns) 
Determines the preferred size of a text area with the specified number of rows and columns.
10 int getRows() 
Returns the number of rows in the text area.
11 int getScrollbarVisibility() 
Returns an enumerated value that indicates which scroll bars the text area uses.
12 void insert(String str, int pos) 
Inserts the specified text at the specified position in this text area.
13 void insertText(String str, int pos) 
Deprecated. As of JDK version 1.1, replaced by insert(String, int).
14 Dimension minimumSize() 
Deprecated. As of JDK version 1.1, replaced by getMinimumSize().
15 Dimension minimumSize(int rows, int columns) 
Deprecated. As of JDK version 1.1, replaced by getMinimumSize(int, int).
16 protected String paramString() 
Returns a string representing the state of this TextArea.
17 Dimension preferredSize() 
Deprecated. As of JDK version 1.1, replaced by getPreferredSize().
18 Dimension preferredSize(int rows, int columns) 
Deprecated. As of JDK version 1.1, replaced by getPreferredSize(int, int).
19 void replaceRange(String str, int start, int end) 
Replaces text between the indicated start and end positions with the specified replacement text.
20 void replaceText(String str, int start, int end) 
Deprecated. As of JDK version 1.1, replaced by replaceRange(String, int, int).
21 void setColumns(int columns) 
Sets the number of columns for this text area.
22 void setRows(int rows) 
Sets the number of rows for this text area.

繼承的方法

這個類從以下類繼承的方法:

  • java.awt.TextComponent

  • java.awt.Component

  • java.lang.Object

TextArea 範例

選擇使用任何編輯器建立以下java程式 D:/ > AWT > com > yiibai > gui >

AwtControlDemo
package com.yiibai.gui;

import java.awt.*;
import java.awt.event.*;

public class AwtControlDemo {

   private Frame mainFrame;
   private Label headerLabel;
   private Label statusLabel;
   private Panel controlPanel;

   public AwtControlDemo(){
      prepareGUI();
   }

   public static void main(String[] args){
      AwtControlDemo  awtControlDemo = new AwtControlDemo();
      awtControlDemo.showTextAreaDemo();
   }

   private void prepareGUI(){
      mainFrame = new Frame("Java AWT Examples");
      mainFrame.setSize(400,400);
      mainFrame.setLayout(new GridLayout(3, 1));
      mainFrame.addWindowListener(new WindowAdapter() {
         public void windowClosing(WindowEvent windowEvent){
            System.exit(0);
         }        
      });    
      headerLabel = new Label();
      headerLabel.setAlignment(Label.CENTER);
      statusLabel = new Label();        
      statusLabel.setAlignment(Label.CENTER);
      statusLabel.setSize(350,100);

      controlPanel = new Panel();
      controlPanel.setLayout(new FlowLayout());

      mainFrame.add(headerLabel);
      mainFrame.add(controlPanel);
      mainFrame.add(statusLabel);
      mainFrame.setVisible(true);  
   }

   private void showTextAreaDemo(){
      headerLabel.setText("Control in action: TextArea"); 

      Label  commentlabel= new Label("Comments: ", Label.RIGHT);

      final TextArea commentTextArea = new TextArea("This is a AWT tutorial "
      +"to make GUI application in Java.",5,30);

      Button showButton = new Button("Show");

      showButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {     
            statusLabel.setText( commentTextArea.getText());        
         }
      }); 

      controlPanel.add(commentlabel);
      controlPanel.add(commentTextArea);        
      controlPanel.add(showButton);
      mainFrame.setVisible(true);  
   }
}

編譯程式,使用命令提示字元。到 D:/ > AWT 然後鍵入以下命令。

D:AWT>javac comyiibaiguiAwtControlDemo.java

如果沒有錯誤出現,這意味著編譯成功。使用下面的命令來執行程式。

D:AWT>java com.yiibai.gui.AwtControlDemo

驗證下面的輸出

AWT TextArea