java匿名类监听器

时间:2021-08-07 08:35:27
监听器类是特意为创建一个GUI组件而设计的监听器对象.监听器类是不被其他应用程序所共享的.正确的做法是将它作为一个内部类定义在框架类中.可以使用匿名内部类简化内部类监听器.匿名内部类是没有名字的内部类.例如     public ControlCircle(){          jbtEnlage.addActionListener(               new EnlargeListener());     }
     class EnlargeListener implements ActionListener{           public void actionPerformed(ActionEvent e)           {                canvas.enlarge();           }         }
     可以替换为     public ControlCircle(){          jbtEnlarge.addActionListener(          new AciotnListener(){               public void actionPerformed(AciotnEvent e){                    canvas.enlarge();               }          }          );     }
     匿名内部类的语法如下:     new SuperClassName/InterfaceName(){          //          //     }
匿名内部类必须总是扩展父类或实现接口,但是不能有显式的extends或implements语句匿名内部类必须事先父类或者接口中的所有抽象方法匿名内部类总是使用父类的无参构造方法来创建实例import javax.swing.*;import java.awt.event.*;
public class AnonymousListenerDemo extends JFrame{        public AnonymousListenerDemo(){                            JButton jbtNew = new JButton("New" );              JButton jbtOpen = new JButton("Open" );              JButton jbtSave = new JButton("Save" );              JButton jbtPrint = new JButton("Print" );                            JPanel panel = new JPanel();              panel.add(jbtPrint);              panel.add(jbtSave);              panel.add(jbtOpen);              panel.add(jbtNew);                            add(panel);                            jbtNew.addActionListener(                            new ActionListener(){                                   public void actionPerformed(ActionEvent e){                                         System. out.println("Process new" );                                  }                           }              );                            jbtOpen.addActionListener(                            new ActionListener(){                                   public void actionPerformed(ActionEvent e){                                         System. out.println("Process open" );                                  }                           }              );                            jbtPrint.addActionListener(                            new ActionListener(){                                   public void actionPerformed(ActionEvent e){                                         System. out.println("Process print" );                                  }                           }              );                            jbtSave.addActionListener(                            new ActionListener(){                                   public void actionPerformed(ActionEvent e){                                         System. out.println("Process save" );                                  }                           }              );                     }               public static void main(String[] args)       {              JFrame frame = new AnonymousListenerDemo();              frame.setTitle( "AnonymousListenerDemo");              frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);              frame.setLocationRelativeTo( null);              frame.pack();              frame.setVisible( true);       }
}
也可以只创建一个监听器,将这个监听器注册给按钮,让后让监听器检测出事件源.对于每种事件写不同的执行方法
可修改为:      ButtonListener listener = new ButtonListener();     jbtNew.addActionListener(listener);     jbtSave.addActionListener(listener);     jbtOpen.addActionListener(listener);     jbtPrint.addActionListener(listener);          class ButtonListener implements ActionListener{          public void actionPerformed(ActionEvent e){               if(e.getSource() == jbtNew)               {                    System. out .println("Process new" );               }               else if(e.getSource() == jbtSave)               {                    System. out .println("Process save" );               }               else if(e.getSource() == jbtOpen)               {                    System. out .println("Process open" );               }               else if(e.getSource() == jbtPrint)               {                    System. out .println("Process print" );               }          }     }



窗口事件:     windows类或者windows任何子类都可能触发WindowEvent。JFrame是Windows的子类,因此也可以触发WindowEvent激活窗口,关闭窗口,正在关闭窗口,变成非活动的窗口,最小化窗口,还原窗口或打开窗口。
import java.awt.event.*;import javax.swing.*;
public class TestWindowEvent extends JFrame{        public static void main(String[] args){              TestWindowEvent frame = new TestWindowEvent();              frame.setTitle( "TestWindowEvent");              frame.setSize(220,80);              frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);              frame.setLocationRelativeTo( null);              frame.setVisible( true);       }               public TestWindowEvent(){              addWindowListener( new WindowListener() {                                           public void windowDeiconified(WindowEvent event)                     {                           System. out.println("Window deiconified" );                     }                                           public void windowIconified(WindowEvent event){                           System. out.println("Window iconified" );                     }                                           public void windowActivated(WindowEvent evnet){                           System. out.println("Window activated" );                     }                                           public void windowDeactivated(WindowEvent event){                           System. out.println("Window deactivated" );                     }                                           public void windowOpened(WindowEvent event){                           System. out.println("Window opened" );                     }                                           public void windowClosing(WindowEvent event){                           System. out.println("Window closing" );                     }                                           public void windowClosed(WindowEvent event){                           System. out.println("Window closed" );                     }              }              );       }}
监听器接口适配器:     因为WindowListener接口中的方法是抽象的,所以使用时必须事先所有方法。为了方便,java提供方便适配器的支持类。它提供监听器中所有方法的默认实现。每个XListener的方便监听器适配器命名为XAdapter.     这样如果只对激活窗口事件感兴趣,使用WindowAdapter可以简化前面的例子。     import java.awt.event.*;import javax.swing.*;
public class AdapterDemo extends JFrame{        public static void main(String[] args){              AdapterDemo frame = new AdapterDemo();              frame.setSize(220,80);              frame.setLocationRelativeTo( null);              frame.setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);              frame.setTitle( "AdapterDemo");              frame.setVisible( true);       }               public AdapterDemo(){              addWindowListener( new WindowAdapter(){                      public void windowActivated(WindowEvent event){                           System. out.println("Window activated" );                     }              });       }
}