码迷,mamicode.com
首页 > 编程语言 > 详细

java事件处理

时间:2020-03-04 19:32:57      阅读:84      评论:0      收藏:0      [点我收藏+]

标签:必须   port   ini   getwidth   pac   graphics   sse   body   height   

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

import java.awt.event.MouseListener;

import java.awt.event.MouseEvent;

import java.applet.Applet;

import java.awt.Graphics;

   

public class ExampleEventHandling extends Applet

                             implements MouseListener {

   

    StringBuffer strBuffer;

   

    public void init() {

         addMouseListener(this);

         strBuffer = new StringBuffer();

        addItem("initializing the apple ");

    }

   

    public void start() {

        addItem("starting the applet ");

    }

   

    public void stop() {

        addItem("stopping the applet ");

    }

   

    public void destroy() {

        addItem("unloading the applet");

    }

   

    void addItem(String word) {

        System.out.println(word);

        strBuffer.append(word);

        repaint();

    }

   

    public void paint(Graphics g) {

         //Draw a Rectangle around the applet‘s display area.

        g.drawRect(0, 0,

                      getWidth() - 1,

                      getHeight() - 1);

   

         //display the string inside the rectangle.

        g.drawString(strBuffer.toString(), 10, 20);

    }

   

    

    public void mouseEntered(MouseEvent event) {

    }

    public void mouseExited(MouseEvent event) {

    }

    public void mousePressed(MouseEvent event) {

    }

    public void mouseReleased(MouseEvent event) {

    }

   

    public void mouseClicked(MouseEvent event) {

         addItem("mouse clicked! ");

    }

}

java事件处理

标签:必须   port   ini   getwidth   pac   graphics   sse   body   height   

原文地址:https://www.cnblogs.com/gjdftru/p/12411175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!