四种监听器(自身类,外部类,内部类,匿名类)

  1. import javax.swing.*;    
  2. import java.awt.*;    
  3. import java.awt.event.*;    
  4.          
  5. public class ThisClassEvent extends JFrame implements ActionListener{      
  6.     public ThisClassEvent(){          
  7.         setLayout(new FlowLayout());             
  8.         JButton btn=new JButton("ok");  
  9.         add(btn);  
  10.         btn.addActionListener(this);    
  11.     }      
  12.     public void actionPerformed (ActionEvent e){    
  13.      System.out.println("The OK button is clicked");  
  14.     }      
  15.     public static void main(String args[]){  
  16.      ThisClassEvent frame = new ThisClassEvent();  
  17.         frame.setTitle("自身类作为事件监听器");  
  18.         frame.setLocationRelativeTo(null); // Center the frame  
  19.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  20.         frame.setSize(280, 100);  
  21.         frame.setVisible(true);  
  22.         new ThisClassEvent();    
  23.     }    
  24. }  
  25.   
  26. import java.awt.*;    
  27. import java.awt.event.*;    
  28. import javax.swing.*;    
  29.         
  30. public class OuterClassEvent extends JFrame{       
  31.     public OuterClassEvent(){    
  32.      setLayout(new FlowLayout());             
  33.         JButton btn=new JButton("ok");  
  34.         add(btn);  
  35.         OuterClass btListener=new OuterClass();  
  36.         btn.addActionListener(btListener);    
  37.     }      
  38.     public static void main(String args[]){    
  39.      OuterClassEvent frame = new OuterClassEvent();  
  40.         frame.setTitle("外部类作为事件监听器");  
  41.         frame.setLocationRelativeTo(null); // Center the frame  
  42.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  43.         frame.setSize(280, 100);  
  44.         frame.setVisible(true);  
  45.         new ThisClassEvent();    
  46.     }    
  47. }       
  48. class OuterClass implements ActionListener{   
  49.     public void actionPerformed(ActionEvent e){    
  50.      System.out.println("The OK button is clicked");  
  51.     }    
  52. }    
  53.   
  54. import java.awt.*;    
  55. import java.awt.event.*;    
  56. import javax.swing.*;    
  57.         
  58. class InnerClassEvent extends JFrame{     
  59.     public InnerClassEvent(){    
  60.      setLayout(new FlowLayout());             
  61.         JButton btn=new JButton("ok");  
  62.         add(btn);  
  63.         OuterClass btListener=new OuterClass();  
  64.         btn.addActionListener(btListener);    
  65.     }  
  66.     class InnerClass implements ActionListener{    
  67.         public void actionPerformed (ActionEvent e){    
  68.          System.out.println("The OK button is clicked");  
  69.         }    
  70.     }    
  71.     public static void main(String args[]){    
  72.      InnerClassEvent frame = new InnerClassEvent();  
  73.         frame.setTitle("内部类作为事件监听器");  
  74.         frame.setLocationRelativeTo(null); // Center the frame  
  75.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  76.         frame.setSize(280, 100);  
  77.         frame.setVisible(true);  
  78.         new ThisClassEvent();    
  79.     }    
  80.    
  81. }  
  82.   
  83. import java.awt.*;    
  84. import java.awt.event.*;    
  85. import javax.swing.*;    
  86.            
  87. class AnonymousEvent extends JFrame{     
  88.     public AnonymousEvent(){    
  89.      setLayout(new FlowLayout());             
  90.         JButton btn=new JButton("ok");  
  91.         add(btn);       
  92.         btn.addActionListener(    
  93.             new ActionListener(){   //匿名内部类作为参数,new 一个lisenter实际上是创建了一个实现了这个listener的类  
  94.                 public void actionPerformed(ActionEvent e){    
  95.                  System.out.println("The OK button is clicked");  
  96.                 }    
  97.             }    
  98.         );     
  99.     }    
  100.     public static void main(String args[]){    
  101.      AnonymousEvent frame = new AnonymousEvent();  
  102.         frame.setTitle("匿名内部类作为事件监听器");  
  103.         frame.setLocationRelativeTo(null); // Center the frame  
  104.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  105.         frame.setSize(280, 100);  
  106.         frame.setVisible(true);  
  107.         new ThisClassEvent();    
  108.     }    
  109. }    

转载于:https://www.cnblogs.com/nin-w/p/5700182.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值