Java文本区(TextArea)

来源:互联网 发布:淘宝男装店排行 编辑:程序博客网 时间:2024/06/03 19:37

http://blog.sina.com.cn/s/blog_63cefe150100gtek.html

文本区可以输入多行文本,其主要方法有:

  • TextArea():构造方法,创建的文本区对象的行数和列数取默认值。
  • TextArea(String s):构造方法,创建的文本区初始字符串为s,文本区有水平和垂直滚动条。
  • TextArea(int x,int y):构造方法,创建的文本区对象的行数为y,列数为x,文本区有水平和垂直滚动条。
  • TextArea(String s,int x,int y):构造方法,创建的文本区对象的初始字符串为s,行数为y,列数为x,文本区有水平和垂直滚动条。
  • TextArea(String s,int x,int y,int scrollbar):构造方法,创建的文本区对象的初始字符串为s,行数为y,列数为x,scrollbar取值:

TextArea.SCROLLBARS_BOTH

TextArea.SCROLLBARS_VERTICAL_ONLY

TextArea.SCROLLBARS_HORIZONAL_ONLY

TextArea.SCROLLBARS_NONE

  • public void setText(String s):设置文本区中的文本为s。
  • public String getText():获取文本区中的文本。
  • public void setEditable(boolean b):设置文本区是否可编辑,默认为可编辑的。
  • public boolean isEditable(boolean b):文本区可编辑时,返回true,否则返回false。
  • public void insert(String s,int x):向文本区指定的位置x(文本区开始处字符的个数)插入指定文本s。
  • public void replaceRange(String s,int start,int end):用字符串s替换从指定位置start开始到指定位置end结束的文本。
  • public void append(String s):在文本区尾部追加文本。
  • int getCaretPosition:获取文本区中输入光标的位置。
  • public void setCaretPosition(int position):设置文本区中输入光标的位置。
  • String getSelectedText:获取文本区中选中的文本。
  • public int getSelectionStart:获取被选中的文本的开始位置。
  • public int getSelectionEnd:获取被选中的文本的结束位置。
  • public void setSelectionStart(int n):设置文本区中被选中文本的开始位置。
  • public void setSelectionEnd(int n):设置文本区中被选中文本的结束位置。
  • public void selectAll():选中文本区的全部文本。
  • public void addTextListener(TextListener):向文本区中增加文本监视器。
  • public void removeTextListener(TextLisener):删除文本区上的文本监视器。

  文本区上的TextEvent事件

  • 当文本区的内容发生变化时(如:插入字符、删除字符),TextEvent类将自动创建一个事件对象。
  • 发生TextEvent事件的事件源获得监视器的方法是addTextListener(监视器)
  • 处理发生TextEvent事件的接口是TextListener,该接口只有一个方法:

textValueChanged(TextEvent e);

  • TextEvent类中的方法有:

public Object getSource():获取发生TextEvent事件的事件源对象的引用。

0 0