Java高级教程:事件处理

来源:互联网 发布:matlab如何分析数据 编辑:程序博客网 时间:2024/05/16 11:30

Applet类从Container类继承了许多事件处理方法。Container类定义了几个方法,例如:processKeyEvent()和processMouseEvent(),用来处理特别类型的事件,还有一个捕获所有事件的方法叫做processEvent。

 



 为了响应一个事件,applet必须重写合适的事件处理方法。
 

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
importjava.awt.event.MouseListener;
importjava.awt.event.MouseEvent;
importjava.applet.Applet;
importjava.awt.Graphics;
  
publicclass ExampleEventHandling extendsApplet
                             implementsMouseListener {
  
    StringBuffer strBuffer;
  
    publicvoid init() {
         addMouseListener(this);
         strBuffer = newStringBuffer();
        addItem("initializing the apple ");
    }
  
    publicvoid start() {
        addItem("starting the applet ");
    }
  
    publicvoid stop() {
        addItem("stopping the applet ");
    }
  
    publicvoid destroy() {
        addItem("unloading the applet");
    }
  
    voidaddItem(String word) {
        System.out.println(word);
        strBuffer.append(word);
        repaint();
    }
  
    publicvoid paint(Graphics g) {
         //Draw a Rectangle around the applet's display area.
        g.drawRect(00,
                      getWidth() - 1,
                      getHeight() - 1);
  
         //display the string inside the rectangle.
        g.drawString(strBuffer.toString(), 1020);
    }
     
    publicvoid mouseEntered(MouseEvent event) {
    }
    publicvoid mouseExited(MouseEvent event) {
    }
    publicvoid mousePressed(MouseEvent event) {
    }
    publicvoid mouseReleased(MouseEvent event) {
    }
  
    publicvoid mouseClicked(MouseEvent event) {
         addItem("mouse clicked! ");
    }
}



Java新手学习群 202250194


0 0
原创粉丝点击