1
Paint()

 1 package hello;
 2 import java.awt.*;
 3 public class helloworld
 4 {
 5     public static void main(String[] args)throws Exception
 6     {
 7          new F().launchFrame();
 8     }
 9 }
10 
11 class F extends Frame
12 {
13     public void launchFrame()
14     {
15         setBounds(200, 300, 600, 700);
16         setVisible(true);
17     }
18     public void paint(Graphics g)        //Frame出现自动调用
19     {
20         Color c = g.getColor();        //获得原来的颜色
21         g.setColor(Color.green);    //
22         g.fillOval(50, 50, 30, 30);
23         g.setColor(Color.ORANGE);
24         g.fillRect(90, 90, 30, 30);
25         g.setColor(c);            //恢复原来的颜色
26     }
27 }

2

MouseListener, MouseAdapter(鼠标适配器,适配器可以防止定义无意义方法,每个监听器都有Adapter)子类,实现了所有方法,都是空的
Point类
152

 1 package hello;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 import java.util.*;
 5 public class helloworld
 6 {
 7     public static void main(String[] args)throws Exception
 8     {
 9          new F("wori");
10     }
11 }
12 
13 class F extends Frame 
14 {
15     ArrayList al = new ArrayList();                //存放鼠标点的点
16     public F(String s)
17     {
18         super(s);
19         setLayout(null);
20         setBounds(200, 300, 600, 700);
21         setVisible(true);
22         this.addMouseListener(new M());            //监听鼠标
23         
24     }
25     public void paint(Graphics g)
26     {
27         Iterator i = al.iterator();            //
28         Color c = g.getColor();
29         g.setColor(Color.CYAN);
30         while(i.hasNext())
31         {
32             Point p = (Point)i.next();
33             g.fillOval(p.x, p.y, 10, 10);
34         }
35                 
36     }
37     void addPoint(int a, int b)
38     {
39         al.add(new Point(a, b));
40     }
41     class M extends MouseAdapter                //MouseAdapter是MouseListener的子类
42     {
43         public void mousePressed(MouseEvent e)
44         {
45             F f = (F)e.getSource();            //getSource()是获得监听的对象
46             addPoint(e.getX(), e.getY());        //
47             f.repaint();                //刷新图像
48             
49         }
50     }
51 }

3

WindowEvent WindowListener

 1 package hello;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 import java.util.*;
 5 public class helloworld
 6 {
 7     public static void main(String[] args)throws Exception
 8     {
 9          Frame f = new Frame("wocao");
10         
11          f.setLayout(null);
12          f.setBounds(300, 300, 300, 300);
13          f.addWindowListener(new WL());
14          f.setVisible(true);
15     }
16     
17 }
18 
19 class WL extends WindowAdapter
20 {
21     public void windowClosing(WindowEvent e)
22     {
23         
24         ((Frame)e.getSource()).setVisible(false);        //注意()一定要套(Frame)e.getSource().setVisible(false);不行    ***
25         System.exit(0);                        //0是正常退出
26     }
27 }

4

KeyListener KeyListener

 1 package hello;
 2 import java.awt.*;
 3 import java.awt.event.*;
 4 import java.util.*;
 5 public class helloworld
 6 {
 7     public static void main(String[] args)throws Exception
 8     {
 9          Frame f = new Frame("wocao");
10         
11          f.addKeyListener(new Kl());                //不能加TextField,加了输入进文本框,就不是Frame的事件了
12          f.addWindowListener(new Wl());
13          f.pack();
14          f.setVisible(true);
15     }
16 
17 }
18 
19 
20 class Kl extends KeyAdapter
21 {
22     public void keyTyped(KeyEvent e)
23     {
24         int c = e.getKeyCode();
25 
26             System.out.println(c + " is typed");
27         
28     }
29 }
30 
31 
32 class Wl extends WindowAdapter
33 {
34     public void windowClosing(WindowEvent e)
35     {
36         ((Frame)e.getSource()).setVisible(false);
37         System.exit(0);
38     }
39 }