JAVA监听器添加的四种方式(自身、外部类、内部类、匿名类)

来源:互联网 发布:软件项目经理管理办法 编辑:程序博客网 时间:2024/05/22 00:54
[java] view plaincopyprint?
  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(280100);  
  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(280100);  
  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(280100);  
  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(280100);  
  106.         frame.setVisible(true);  
  107.         new ThisClassEvent();    
  108.     }    
  109. }     
0 0
原创粉丝点击