Java- 事件处理机制
以前买一些编程语言的书,例如VB和C#方面的,有一些书我买来也就是翻翻,然后放在架上当摆设,有时候坐着看到架上有几本像样的书,虽然没怎么看过,但心里也觉的还不错。
在我以前买过的几本编程书里,好像没有专门把事件处理当作一章来讲解的。而我在网上下载的几本JAVA电子书里,却都把事件处理拿出来作了很祥细的说明。
在大多数的编程语言里,事件处理机制都是大同小异的,都有事件、事件源、事件处理方法。只是实现方式有些不一样。在C#里产生一个事件很方便很简单,例如想产生一个按扭的CLICK事件,拉个BUTTON按扭,双击一下,就可以进入事件处理方法里直接写代码了。因为这么方便的,中间VS工具一定帮我们完成了很多事情。
暂时抛开C#产生事件的简单方便,来看一下JAVA事件处理机制大概的图:
上面的图很好理解的,事件源就好比较是一个Button按扭,产生的事件对象有多种不同的类型,例如有单击(Click)、双击(DbClick)、键盘按下(KeyDown)等。而事件处理器就是方法里面的代码。
在JAVA里有一个事件监听器,他和事件源相关联,当某个事件源上发生某种事件,例如CLICK事件,监听器知道了这个事件后就发出事件对象,这时事件处理方法里的代码就可以WORK了。
看一下JAVA里几种实现事件处理机制的代码写法,实现事件监听接口
View Code
import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class Demo1 { public static void main(String[] args){ Frame f = new Frame("This is a Test"); f.setBounds(100, 100, 300, 300); f.setVisible(true); f.addWindowListener(new MyWindowListener()); } } class MyWindowListener implements WindowListener{ @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosed(WindowEvent e) { } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.exit(0); } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } }
继承事件适配器,重写方法
import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Demo1 { public static void main(String[] args){ Frame f = new Frame("This is a Test"); f.setBounds(100, 100, 300, 300); f.setVisible(true); f.addWindowListener(new MyWindowListener()); } } class MyWindowListener extends WindowAdapter{ @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub super.windowClosing(e); System.exit(0); } }
匿名方法的实现一
View Code
import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class Demo1 { public static void main(String[] args){ Frame f = new Frame("This is a Test"); f.setBounds(100, 100, 300, 300); f.setVisible(true); f.addWindowListener(new WindowListener(){ @Override public void windowActivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosed(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub System.exit(0); } @Override public void windowDeactivated(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowDeiconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowIconified(WindowEvent e) { // TODO Auto-generated method stub } @Override public void windowOpened(WindowEvent e) { // TODO Auto-generated method stub } }); } }
匿名方法的实现二
import java.awt.Frame; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; public class Demo1 { public static void main(String[] args){ Frame f = new Frame("This is a Test"); f.setBounds(100, 100, 300, 300); f.setVisible(true); f.addWindowListener(new WindowAdapter(){ @Override public void windowClosing(WindowEvent e) { // TODO Auto-generated method stub super.windowClosing(e); System.exit(0); } }); } }