TextEvent
是Java SE中的一个类,它继承自java.util.EventObject
。TextEvent
主要用于处理文本组件(如文本框、文本区域等)中的文本事件。当文本组件中的文本发生变化时,会触发一个TextEvent
事件。
以下是一个简单的示例,展示了如何使用TextEvent
:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TextEventDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("TextEvent Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 200);
JTextField textField = new JTextField();
textField.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("文本已更改: " + textField.getText());
}
});
frame.add(textField, BorderLayout.NORTH);
frame.setVisible(true);
}
}
在这个示例中,我们创建了一个简单的窗口,其中包含一个文本框。当用户在文本框中输入文本并按下回车键时,会触发一个ActionEvent
事件。我们通过添加一个ActionListener
来监听这个事件,并在事件触发时输出文本框中的文本。
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中
}
}
}