Java事件处理机制- 事件监听器的四种实现方式【转】

原文:http://stefan321.iteye.com/blog/345221

自身类作为监听器

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 *Java事件处理机制:自身类作为事件监听器
 *@author Winty(wintys@gmail.com) 
 *@version 2008-12-3
 */
class ThisClassEvent extends JFrame implements ActionListener{ 
    JButton btn;

    public ThisClassEvent(){
        super("Java事件监听机制");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn=new JButton("点击");
        btn.addActionListener(this);
        getContentPane().add(btn);

        setBounds(200,200,300,160);
        setVisible(true);
    }

    /**************************************/
    public void actionPerformed (ActionEvent e){
        Container c=getContentPane();
        c.setBackground(Color.red);
    }
    /**************************************/

    public static void main(String args[]){
        new ThisClassEvent();
    }
} 

外部类作为监听器

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *Java事件处理机制:外部类作为事件监听器
 *@author Winty(wintys@gmail.com) 
 *@version 2008-12-3
 */
class OuterClassEvent extends JFrame{ 
    JButton btn;

    public OuterClassEvent(){
        super("Java事件监听机制");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn=new JButton("点击");
        btn.addActionListener(new OuterClass(this));
        getContentPane().add(btn);

        setBounds(200,200,300,160);
        setVisible(true);
    }

    public static void main(String args[]){
        new OuterClassEvent();
    }
} 

/*外部类*********************************/
class OuterClass implements ActionListener{
    OuterClassEvent oce;

    public OuterClass(OuterClassEvent oce){
        this.oce = oce;
    }

    public void actionPerformed(ActionEvent e){
        Container c=oce.getContentPane();
        c.setBackground(Color.red);
    }
}

匿名内部类作为监听器

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *Java事件处理机制:匿名内部类作为事件监听器
 *@author Winty(wintys@gmail.com) 
 *@version 2008-12-3
 */
class AnonymousEvent extends JFrame{ 
    JButton btn;

    public AnonymousEvent(){
        super("Java事件监听机制");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn=new JButton("点击");
        /*匿名内部类******************************/
        btn.addActionListener(
            new ActionListener(){
                public void actionPerformed(ActionEvent e){
                    Container c=getContentPane();
                    c.setBackground(Color.red);
                }
            }
        );
        /***************************************/
        getContentPane().add(btn);

        setBounds(200,200,300,160);
        setVisible(true);
    }

    public static void main(String args[]){
        new AnonymousEvent();
    }
} 

内部类作为监听器

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *Java事件处理机制:内部类作为事件监听器
 *@author Winty(wintys@gmail.com) 
 *@version 2008-12-3
 */
class InnerClassEvent extends JFrame{ 
    JButton btn;

    public InnerClassEvent(){
        super("Java事件监听机制");
        setLayout(new FlowLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        btn=new JButton("点击");
        btn.addActionListener(new InnerClass());
        getContentPane().add(btn);

        setBounds(200,200,300,160);
        setVisible(true);
    }

    /*内部类*********************************/
    class InnerClass implements ActionListener{
        public void actionPerformed (ActionEvent e){
            Container c=getContentPane();
            c.setBackground(Color.red);
        }
    }
    /**************************************/

    public static void main(String args[]){
        new InnerClassEvent();
    }
} 
posted @ 2012-09-14 18:14  Leo Forest  阅读(792)  评论(0编辑  收藏  举报