Java-马士兵设计模式学习笔记-观察者模式-AWT简单例子
1.AWT简单例子
TestFrame.java
1 import java.awt.Button; 2 import java.awt.Frame; 3 import java.awt.event.ActionEvent; 4 import java.awt.event.ActionListener; 5 import java.awt.event.WindowEvent; 6 import java.awt.event.WindowListener; 7 8 public class TestFrame extends Frame { 9 10 public void launch(){ 11 Button b = new Button("按钮"); 12 b.addActionListener(new MyActionListener1()); 13 b.addActionListener(new MyActionListener2()); 14 this.add(b); 15 this.pack(); 16 17 this.addWindowListener(new WindowListener() { 18 19 @Override 20 public void windowClosing(WindowEvent e) { 21 System.out.println("windowClosing"); 22 System.exit(0); 23 } 24 25 @Override 26 public void windowClosed(WindowEvent e) { 27 System.out.println("windowClosed"); 28 } 29 30 @Override 31 public void windowOpened(WindowEvent e) { 32 // TODO Auto-generated method stub 33 34 } 35 36 @Override 37 public void windowIconified(WindowEvent e) { 38 // TODO Auto-generated method stub 39 40 } 41 42 @Override 43 public void windowDeiconified(WindowEvent e) { 44 // TODO Auto-generated method stub 45 46 } 47 48 @Override 49 public void windowActivated(WindowEvent e) { 50 // TODO Auto-generated method stub 51 52 } 53 54 @Override 55 public void windowDeactivated(WindowEvent e) { 56 // TODO Auto-generated method stub 57 58 } 59 60 }); 61 62 this.setVisible(true); 63 } 64 65 private class MyActionListener1 implements ActionListener { 66 67 @Override 68 public void actionPerformed(ActionEvent e) { 69 System.out.println("MyActionListener1"); 70 } 71 72 } 73 74 private class MyActionListener2 implements ActionListener { 75 76 @Override 77 public void actionPerformed(ActionEvent e) { 78 System.out.println("MyActionListener2"); 79 } 80 81 } 82 83 84 public static void main(String[] args) { 85 new TestFrame().launch(); 86 } 87 }
2.运行结果
You can do anything you set your mind to, man!