您的位置:首页 > 编程语言 > Java开发

JAVA事件处理的三种不同方法

2011-07-21 12:50 495 查看
          在java的图形界面开发中,要让图形界面接收到用户的操作,就要给各个组件添加事件处理机制。在事件处理过程中,涉及到3类对象。第一是Event事件,实现用户对图形界面组件的操作,以类的形式出现,比如窗口操作对应类为WindowEvent,键盘操作对应类为KeyEvent;第二是事件源,即事件发生的场所,比如一个按钮、单选框或复选框、文本框等;第三就是事件处理者,接收发生的事件并进行处理,也称监听器。
         事件处理有3种不同的方法,下面进行详细的介绍。
        1、事件适配器
         Java为一些Listener接口提供了适配器类,这样,可以通过继承适配器类实现有关方法,而无关方法不用实现,大大减少了代码量。如:
        
import java.awt.*;
import java.awt.event.*;
public class Example1  extends WindowAdapter implements ActionListener
{
    private Frame win;
    private TextField tf;
    private TextArea ta;
    private Button bun;
   Example1()
   {
       win = new Frame("事件适配器");
       tf = new TextField(20);
       ta = new TextArea(10, 20);
       ta.setEditable(false);
       bun = new Button("添加");
       bun.addActionListener(this);
       win.setLayout(new FlowLayout());
       win.add(tf);
       win.add(bun);
       win.add(ta);
       win.addWindowListener(this);    
       win.setSize(300, 300);
       win.setVisible(true);
  
   }
 
   public void actionPerformed(ActionEvent e)
  {
      String s = tf.getText();
      if(s != "")
      {
         ta.append(s+"\n");
      }
  }
  
  public void windowClosing(WindowEvent e)
  {
     System.exit(0);
  }
 
  public static void main(String args[])
  {
     Example1 w = new Example1();
  }
}
   在上面的程序中,Example1类继承了WindowAdapter适配器类和ActionListener接口,生成的框架中有3个组件:文本框、按钮和文本域,当用户按了按钮后,发生ActionEvent事件,用户在文本框中输入的字符串将添加到文本域中,实现程序在函数actionPerformed中。win.addWindowListener(this);    将Example1类设为监听器,当用户按了框架退出按钮时,触发WindowEvent事件,而Example1类继承了windowClosing接口,退出程序,其它无关接口如windowOpened(打开时)、windowIconified(图标化)则不用实现。
    2、内部类实现事件处理
    
import java.awt.*;
import java.awt.event.*;
public class Example2 implements ActionListener
{
     private Frame win;
     private TextField tf;
     private TextArea ta;
     private Button bun;
    Example2()
    {
       win = new Frame("内部类");
       tf = new TextField(20);
      ta = new TextArea(10, 20);
      ta.setEditable(false);
      bun = new Button("添加");
      bun.addActionListener(this);
      win.setLayout(new FlowLayout());
      win.add(tf);
      win.add(bun);
      win.add(ta);
      win.addWindowListener(new MyWinClose());
      win.setSize(300, 300);
      win.setVisible(true);
  
    }
 
     public void actionPerformed(ActionEvent e)
    {
        String s = tf.getText();
        if(s != "")
       {
         ta.append(s+"\n");
       }
    }
 
  class MyWinClose extends WindowAdapter
  {
     public void windowClosing(WindowEvent e)
    {
         System.exit(0);
     }
  }
 
   public static void main(String args[])
  {
      Example2 w = new Example2();
  
  }
 
}
    win.addWindowListener(new MyWinClose());即把MyWinClose设为监听器,该类继承了WindowAdapter适配器,实现了windowClosing接口。
  使用内部类的原因如下:
   (1)一个内部类的对象可访问外部类的成员方法和变量,包括私有的成员;
   (2)非常容易实现;
   (3)内部类编写时间驱动程序很方便。
 
    3、匿名类
      
import java.awt.*;
import java.awt.event.*;
public class Example3 implements ActionListener
{
   private Frame win;
   private TextField tf;
   private TextArea ta;
   private Button bun;
   Example3()
   {
      win = new Frame("匿名类");
     tf = new TextField(20);
     ta = new TextArea(10, 20);
     ta.setEditable(false);
    bun = new Button("添加");
    bun.addActionListener(this);
    win.setLayout(new FlowLayout());
    win.add(tf);
    win.add(bun);
    win.add(ta);
    win.addWindowListener(new WindowAdapter()
   {
     public void windowClosing(WindowEvent e)
    {
      System.exit(0);
    }
   });
   win.setSize(300, 300);
   win.setVisible(true);
  
  }
 
  public void actionPerformed(ActionEvent e)
  {
     String s = tf.getText();
     if(s != "")
    {
       ta.append(s+"\n");
    }
   }
 
 
   public static void main(String args[])
  {
     Example3 w = new Example3();
  
  }
 
}
   使用匿名类更加方便,匿名就是连名字也没有,只是显式地调用一个无参的父类的构造方法。
 
 
 
 
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息