
原文地址:http://blog.sina.com.cn/s/blog_4b650d650100nqws.html
Java中四种事件监听器的实现方式分别为:
- 自身类做为事件监听器
- 外部类作为事件监听器
- 匿名内部类作为事件监听器
- 内部类作为事件监听器.
下面分别描述:
//---------------------------------------------------------------------
1. 自身类作为事件监听器:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*; //ActionListener:java.awt.event.ActionListener(interface),用来监听Action事件
class ThisClassEvent extends JFrame implements ActionListener
{
JButton btn; public ThisClassEvent()
{
super("Java事件监听机制");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn=new JButton("点击");
//addActionListerner()原型为:public void addActionListener(ActionListener l)
btn.addActionListener(this);
getContentPane().add(btn); setBounds(200,200,300,160);
setVisible(true);
} /------------------------------
//actionPerformed函数是从ActionListener中继承来的. public void actionPerformed (ActionEvent e)
{
Container c=getContentPane();
c.setBackground(Color.red);
} /------------------------------
public static void main(String args[])
{
new ThisClassEvent();
}
}
2. 外部类作为事件监听器:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; class OuterClassEvent extends JFrame
{
JButton btn;
public OuterClassEvent()
{
super("Java事件监听机制");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn=new JButton("点击"); //addActionListerner()原型为:public void addActionListener(ActionListener l)
//new OuterClass(this)是OuterClass的实例,而OuterClass是从ActionListener继承来的
btn.addActionListener(new OuterClass(this));
getContentPane().add(btn); setBounds(200,200,300,160);
setVisible(true);
} public static void main(String args[])
{
new OuterClassEvent();
}
} //外部类------------------------------ //OuterClass从ActionListener继承
class OuterClass implements ActionListener
{
OuterClassEvent oce;
public OuterClass(OuterClassEvent oce)
{
this.oce = oce;
} //actionPerformed函数是从ActionListener中继承来的.
public void actionPerformed(ActionEvent e)
{
Container c=oce.getContentPane();
c.setBackground(Color.red);
}
} //---------------------------------------------------------------------
3. 匿名内部类作为事件监听器:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; class AnonymousEvent extends JFrame
{
JButton btn; public AnonymousEvent()
{
super("Java事件监听机制");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn=new JButton("点击"); //匿名内部类---------------------------
//addActionListerner()原型为:public void addActionListener(ActionListener l)
btn.addActionListener
(
//new ActionListener()声明了ActionListener的一个实例
new ActionListener()
{ //actionPerformed函数是从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();
}
} //---------------------------------------------------------------------
4.内部类作为事件监听器:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*; class InnerClassEvent extends JFrame {
JButton btn;
public InnerClassEvent() {
super("Java事件监听机制");
setLayout(new FlowLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); btn=new JButton("点击"); //addActionListerner()原型为:public void addActionListener(ActionListener l)
btn.addActionListener(new InnerClass());
getContentPane().add(btn); setBounds(200,200,300,160);
setVisible(true);
} //内部类---------------------------
//InnerClass继承了ActionListener
class InnerClass implements ActionListener
{ //actionPerformed函数是从ActionListener中继承来的.
public void actionPerformed (ActionEvent e)
{
Container c=getContentPane();
c.setBackground(Color.red);
}
} //------------------------------
public static void main(String args[]) {
new InnerClassEvent();
}
} //---------------------------------------------------------------------