- 自身类作为事件监听器
- 外部类作为事件监听器
- 匿名内部类作为事件监听器
- 内部类作为事件监听器
自身类作为事件监听器 :
- 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();
- }
- }