The string call method indexof (string s) retrieves the string s from the header of the current string and returns the location where the first occurrence of s occurs. If the string s is not retrieved, the value returned by the method is - 1. The indexof (string s, int startpoint) method is called to retrieve the string s from the starting point position of the current string, and return the position where s appears for the first time. If the string s is not retrieved, the value returned by the method is - 1. String calls the lastIndexOf (string s) method to retrieve the string s from the beginning of the current string’s header, and returns the location of the last s. If the string s is not retrieved, the value returned by the method is - 1.
import java.awt.*;
import java.awt.event.*;
public class Javase_50 extends Frame //定义主类
{
static TextArea ta1=new TextArea ("",6,10, TextArea.SCROLLBARS_NONE);
static TextArea ta2=new TextArea ("",6,10, TextArea.SCROLLBARS_NONE);
public static void main(String[] args)
{
Javase_50 frm=new Javase_50(); //调用构造方法
frm.setTitle("文本事件处理TextEvent");
frm.setSize(220,150);
frm.setLayout(new FlowLayout(FlowLayout.CENTER));
frm.setVisible(true);
}
public app13_5() //构造方法
{
ta1.addTextListener(new MyTxListener());
add(ta1);
add(ta2);
ta2.setEditable(false);
}
class MyTxListener implements TextListener //定义内部类实现TextListener接口
{
public void textValueChanged(TextEvent e) //事件发生时的处理操作
{
String text=ta1.getText(); //获得ta1的内容
ta2.setText(text); //将ta1的内容放入到ta2中
}
}
}