参考二 AWT事件处理模型
事件处理模型:以对象形式封装。
必备三要素:
- 事件源:通俗的就是各个组件,用来接受各种事件的对象。(李先生)
- 事件对象:包装“在事件源上运用输入设备操作所引发事件”这些事件,它记录事件源已经处理该事件所需要的各种信息。(各种法律纠纷)
- 事件监听器:对事件进行处理。(某律师)
引一段通俗的话:
有一位李先生,李先生可能会发生很多法律纠纷,可能是民事法律纠纷,也可能是刑事法律纠纷。那么李先生可以自己打官司(如果有能力的话),更方便的是可以请律师。他可以授权王律师负责帮他打民事法律的官司,同时也可以授权张律师帮他打刑事法律的官司。
这个请律师的过程从李先生的角度来看,就是授权的过程,而从王律师和张律师的角度来看,一旦被授权,他们就得时刻对李先生负责,"监听"着李先生,一旦发生民事纠纷了,王律师就要马上去处理,而一旦发生刑事纠纷了,张律师就要马上进行处理。此时此刻,李先生就是事件源,王律师是一个事件处理者,张律师是另外一个事件处理者,民事纠纷和刑事纠纷就是不同类型的事件。
具体步骤:
- 在事件源中登记事件监听器
- 当事件发生,JVM产生一个事件对象。当事件源接收到事件对象,启动对应这一类事件的注册过的事件监听器,并将事件对象传递给它当参数。
- 事件监听器接收到了这个事件对象参数,开始处理。
以下是三种方式写的测试实例。效果如下:
两个输入框,随便一个输入一下中文单词,最下面的文本框会给出英文解释。
效果如下:
(1)在类的后面写实现XXListener。
比如下面的例子, MyWindow类在声明时通过implements ActionListener这一句指明了本类(即MyWindow类)实现了ActionListener接口,所以凡是MyWindow类的对象都可以作为addActionListener方法的实例参数.而this可以理解为指向对象本身(MyWindow类对象)的引用,所以可以作为addActionListener的实例参数.
package AddActionListener;
import java.awt.*;
import java.awt.event.*;
class MyWindow extends Frame implements ActionListener
{ TextField text1,text2,text3;
MyWindow()
{
setLayout(new FlowLayout());
text1=new TextField(8);
text2=new TextField(8);
text3=new TextField(15);
add(text1);
add(text2);
add(text3);
text1.addActionListener(this);//这里两个this是指win吗?为什么不能写win?不用this还可以怎么写?
text2.addActionListener(this);
setBounds(100,100,150,150);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==text1)
{ String word=text1.getText();
if(word.equals("boy"))
{ text3.setText("男孩");
}
else if (word.equals("女孩"))
{ text3.setText("girl");
}
else if (word.equals("太阳"))
{ text3.setText("sun");
}
else
{ text3.setText("没有该单词");
}
}
else if(e.getSource()==text2)
{ String word=text2.getText();
if(word.equals("男孩"))
{ text3.setText("boy");
}
else if (word.equals("女孩"))
{ text3.setText("girl");
}
else if (word.equals("太阳"))
{ text3.setText("sun");
}
else
{ text3.setText("没有该单词");
}
}
}
}
public class MyWindowExample
{ public static void main(String args[])
{ MyWindow win=new MyWindow();
}
}
(2)直接用一般内部类的方式
package AddActionListener;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class MyWindow2 extends Frame
{
public TextField text1, text2, text3;
MyWindow2()
{
setLayout(new FlowLayout());
text1=new TextField(8);
text2=new TextField(8);
text3=new TextField(15);
add(text1);
add(text2);
add(text3);
MyActionListener mal = new MyActionListener();
text1.addActionListener(mal);//这里两个this是指win吗?为什么不能写win?不用this还可以怎么写?
text2.addActionListener(mal);
setBounds(100,100,150,150);
setVisible(true);
validate();
}
class MyActionListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{ if(e.getSource()== text1)
{ String word= text1.getText();
if(word.equals("boy"))
{ text3.setText("男孩");
}
else if (word.equals("女孩"))
{ text3.setText("girl");
}
else if (word.equals("太阳"))
{ text3.setText("sun");
}
else
{ text3.setText("没有该单词");
}
}
else if(e.getSource()==text2)
{ String word= text2.getText();
if(word.equals("男孩"))
{ text3.setText("boy");
}
else if (word.equals("女孩"))
{ text3.setText("girl");
}
else if (word.equals("太阳"))
{ text3.setText("sun");
}
else
{ text3.setText("没有该单词");
}
}
}
}
}
public class MyWindowExample2 {
public static void main(String args[])
{ MyWindow2 win=new MyWindow2();
}
}
(3)采取匿名类的方式如下例子
package AddActionListener;
import java.awt.*;
import java.awt.event.*;
class MyWindow3 extends Frame
{ TextField text1,text2,text3;
MyWindow3()
{ setLayout(new FlowLayout());
text1=new TextField(8);
text2=new TextField(8);
text3=new TextField(15);
add(text1);
add(text2);
add(text3);
text1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==text1)
{ String word=text1.getText();
if(word.equals("男孩"))
{ text3.setText("boy");
}
else if (word.equals("女孩"))
{ text3.setText("girl");
}
else if (word.equals("太阳"))
{ text3.setText("sun");
}
else
{ text3.setText("没有该单词");
}
}
}
}
);//这里两个this是指win吗?为什么不能写win?不用this还可以怎么写?
text2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==text2)
{ String word=text2.getText();
if(word.equals("男孩"))
{ text3.setText("boy");
}
else if (word.equals("女孩"))
{ text3.setText("girl");
}
else if (word.equals("太阳"))
{ text3.setText("sun");
}
else
{ text3.setText("没有该单词");
}
}
}
}
);
setBounds(100,100,150,150);
setVisible(true);
validate();
}
}
public class MyWindowExample3
{ public static void main(String args[])
{ MyWindow3 win=new MyWindow3();
}
}
根据上面三种方法的考量,在接下来的计算器的各个按钮触发事件的处理时,我觉得用方法一比较简洁。