1
2 import java.awt.*
;
3 import javax.swing.*
;
4 import java.awt.event.*
;
5
6 class WindowActionEvent
extends JFrame
7 {
8 JTextField text;
//声明一个文本区
9 ActionListener listener ;
//listener是监视器
10 //设置一个默认的构造函数
11 public WindowActionEvent()
12 {
13 setLayout(
new FlowLayout());
//获此容器管理器的布局管理器
14 text =
new JTextField(10);
//设置文本区的列行数
15 add(text);
//将这个文本区 添加到文本尾
16 listener =
new ReaderListener();
//创建监视器
17 text.addActionListener(listener);
//注册监视器
18 setVisible(
true);
//窗口是否可视化
19 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
20 }
21 }
22
23 public class ActionEvent1 {
24 public static void main(String args[])
25 {
26 WindowActionEvent win =
new WindowActionEvent();
27 win.setTitle("处理ActionEvent事件"
);
28 win.setBounds(100,100,310,260
);
29 }
30 }
View Code
1 import java.awt.event.ActionEvent;
2 import java.awt.event.ActionListener;
3
4 public class ReaderListener
implements ActionListener {
5
6 public void actionPerformed(ActionEvent e) {
7 // TODO Auto-generated method stub
8 String str = e.getActionCommand() ;
//获取封装在事件中的“ 命令 ” 字符串
9 System.out.println(str+":"+
str.length());
10 }
11
12 }
View Code
转载于:https://www.cnblogs.com/gongxijun/p/3716039.html
相关资源:垃圾分类数据集及代码