事件处理
事件处理:
事件处理的组成:
事件源:发生事件的GUI部件
事件:用户对事件源进行操作触发事件
事件监听者:负责对事件进行处理
事件处理流程:
1.注册监听者:btn.addActionListener(监听者)
2.监听者要继承相应的接口,实现接口中相应的方法:public void actionPerFormed(ActionEvent e){.....}
//示例在AWT写的窗口中添加关闭窗口的监听器,注意如果是在Swing下这个就不用写了,毕竟人家都已经实现这个功能了,其实在生活中很少用到AWT插件了,不过事件监听思想还是要练习的
package com.sunchongwei;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Windowlistener extends Frame {
public Windowlistener() {
super("测试窗口关闭功能");
this.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);//程序退出
}
});
int width=200,height=200;
this.setSize(width, height);
this.setVisible(true);
}
public static void main(String[] args) {
new Windowlistener();
}
}
描述信息 | 接口名称 | 方法 |
单机按钮、菜单项、文本框及按回车等动作 | ActionListener | actionPerformer(ActionEvent e) |
选择了可选择的项目 | ItemListener | itemStateChanged(ItemEvent e) |
文本部件内容的改变 | TextListener | textValueChanged(TextEvent e) |
移动了滚动条等操作 | AdjustmentListener | adjustmendChanged(AdjustmentEvent e) |
鼠标移动 | MouseListener | |
键盘输入 | KeyListener | |
部件失去或收到焦点 | FocusListener | |
部件移动、缩放、显示、/隐藏等 | ComponentListener | |
窗口事件 | WindowListener | windowClosing(WindowEvent e) windowOpened(WindowEvent e) |
容器增加删除部件 | ContainerListener |
在事件处理中添加事件源
一个事件可以由多个监听者,一个监听者也可以监听多个事件源,采用以下方法区分事件源
~.getSource();//用来获取事件源对象的引用。
//附录一个模拟计算的小程序,主要用来讲事件监听
package com.sunchongwei;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class Windowlistener extends Frame implements ActionListener{
TextField resultab;
Button jia,jian,cheng,chu,guiling;
Button get;
double a=0;//用来存储上一阶段的数字
int tar=0;
public Windowlistener() {
super("制作一个简单的计算器示例");
this.setLayout(new FlowLayout());//设置为流式布局
resultab=new TextField(4);
guiling=new Button("归零");
guiling.addActionListener(this);
jia=new Button("+");
jia.addActionListener(this);
get=new Button("=");
get.addActionListener(this);
jian=new Button("-");
jian.addActionListener(this);
cheng=new Button("*");
cheng.addActionListener(this);
chu=new Button("/");
chu.addActionListener(this);
this.add(resultab);
this.add(get);
this.add(jia);
this.add(jian);
this.add(cheng);
this.add(chu);
this.add(guiling);
this.setSize(300, 300);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==guiling) {
resultab.setText("0");
}
if(e.getSource()==jia) {
a=Double.parseDouble(resultab.getText());
tar=1;
resultab.setText(" ");
}
if(e.getSource()==jian) {
a=Double.parseDouble(resultab.getText());
tar=2;
resultab.setText(" ");
}
if(e.getSource()==cheng) {
a=Double.parseDouble(resultab.getText());
tar=3;
resultab.setText(" ");
}
if(e.getSource()==chu) {
a=Double.parseDouble(resultab.getText());
tar=4;
resultab.setText(" ");
}
if(e.getSource()==get) {
double b=Double.parseDouble(resultab.getText());
switch(tar) {
case 1:a+=b;break;
case 2:a-=b;break;
case 3:a*=b;break;
case 4:a/=b;break;
}
resultab.setText(String.valueOf(a));
}
}
public static void main(String[] args) {
Windowlistener window=new Windowlistener();
window.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}