2008年5月28日 星期三

5 kinds of event handler styles

依處理器所掛的位置,總共有5種寫法,以計時器處理器為例,
A.處理器掛在外部類別下:
  1.  
  2. import javax.swing.Timer;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5. public class TimerTest1
  6. {
  7. public static void main(String args[]) throws Exception
  8. {
  9. ActionListener al = new TimerHandler();
  10. Timer t = new Timer(1000,al);
  11. t.start();
  12. Thread.sleep(10000);
  13. t.stop();
  14. }
  15. }
  16. class TimerHandler implements ActionListener
  17. {
  18. public void actionPerformed(ActionEvent ae)
  19. {
  20. System.out.println("執行每次叫醒要作的事1");
  21. }
  22. }
B.處理器掛在內部匿名類別下:
  1.  
  2. import javax.swing.Timer;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5. public class TimerTest2
  6. {
  7. public static void main(String args[]) throws Exception
  8. {
  9. ActionListener al = new ActionListener()
  10. {
  11. public void actionPerformed(ActionEvent ae)
  12. {
  13. System.out.println("執行每次叫醒要作的事2");
  14. }
  15. };
  16. Timer t = new Timer(1000,al);
  17. t.start();
  18. Thread.sleep(10000);
  19. t.stop();
  20. }
  21. }
C.處理器掛在內部有名類別下:
  1.  
  2. import javax.swing.Timer;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5. public class TimerTest3
  6. {
  7. public static void main(String args[]) throws Exception
  8. {
  9. new TimerTest3();
  10. }
  11.  
  12. TimerTest3() throws Exception
  13. {
  14. ActionListener al = new TimerHandler();
  15. Timer t = new Timer(1000,al);
  16. t.start();
  17. Thread.sleep(10000);
  18. t.stop();
  19. }
  20.  
  21. private class TimerHandler implements ActionListener
  22. {
  23. public void actionPerformed(ActionEvent ae)
  24. {
  25. System.out.println("執行每次叫醒要作的事3");
  26. }
  27. }
  28. }
D.處理器掛在本身類別下:
  1.  
  2. import javax.swing.Timer;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5. public class TimerTest4 implements ActionListener
  6. {
  7. public void actionPerformed(ActionEvent ae)
  8. {
  9. System.out.println("執行每次叫醒要作的事4");
  10. }
  11.  
  12. public static void main(String args[]) throws Exception
  13. {
  14. ActionListener al = new TimerTest4();
  15. Timer t = new Timer(1000,al);
  16. t.start();
  17. Thread.sleep(10000);
  18. t.stop();
  19. }
  20. }
E.處理器同時掛在內部和外部類別下:
  1.  
  2. import javax.swing.Timer;
  3. import java.awt.event.ActionListener;
  4. import java.awt.event.ActionEvent;
  5. public class TimerTest5
  6. {
  7. public static void main(String args[]) throws Exception
  8. {
  9. ActionListener al = new ActionAdapter()
  10. {
  11. public void actionPerformed(ActionEvent ae)
  12. {
  13. System.out.println("執行每次叫醒要作的事5x");
  14. }
  15. };
  16. Timer t = new Timer(1000,al);
  17. t.start();
  18. Thread.sleep(10000);
  19. t.stop();
  20. }
  21. }
  22.  
  23. class ActionAdapter implements ActionListener
  24. {
  25. public void actionPerformed(ActionEvent ae)
  26. {
  27. System.out.println("執行每次叫醒要作的事5");
  28. }
  29. }

沒有留言: