package javawork;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class WindowActionEvent extends JFrame{
private JTextField text;
//ActionListener listener;
public WindowActionEvent()
{
setLayout(new FlowLayout());
setVisible(true);
text=new JTextField(10);
add(text);
text.addActionListener(new ActionListener() {//匿名类创建监视器对象的类以及实现处理事件的接口
public void actionPerformed(ActionEvent event)
{
// String s=event.getActionCommand();//调用getActionCommand获取发生ActionEvent事件时
//和该事件相关的一个“命令”字符串,当发生ActionEvent事件时,默认的“命令”字符串是文本框中的文本
String s=text.getText();//直接通过组件获取字符串
System.out.println("输入的字符串的长度为:"+s.length());
}
});
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}