Bài 84: Xử lý sự kiện Java AWT

Ngày đăng: 1/2/2023 3:21:32 PM

Các lớp Java Event và các interface Listener

Các lớp Event

Các interface Listener

ActionEvent

ActionListener

MouseEvent

MouseListener and MouseMotionListener

MouseWheelEvent

MouseWheelListener

KeyEvent

KeyListener

ItemEvent

ItemListener

TextEvent

TextListener

AdjustmentEvent

AdjustmentListener

WindowEvent

WindowListener

ComponentEvent

ComponentListener

ContainerEvent

ContainerListener

FocusEvent

FocusListener


 

Các bước để thực hiện xử lý sự kiện trong Java AWT

Các bước sau được yêu cầu để thực hiện xử lý sự kiện:

  1. Đăng ký thành phần với trình Listener.

Phương thức đăng ký

Để đăng ký thành phần với trình Listener, có nhiều lớp cung cấp các phương thức đăng ký. Ví dụ:

  • Button
    • public void addActionListener(ActionListener a){}
  • MenuItem
    • public void addActionListener(ActionListener a){}
  • TextField
    • public void addActionListener(ActionListener a){}
    • public void addTextListener(TextListener a){}
  • TextArea
    • public void addTextListener(TextListener a){}
  • Checkbox
    • public void addItemListener(ItemListener a){}
  • Choice
    • public void addItemListener(ItemListener a){}
  • List
    • public void addActionListener(ActionListener a){}
    • public void addItemListener(ItemListener a){}

Ví dụ xử lý sự kiện trong Java AWT

Chúng tôi có thể đặt mã xử lý sự kiện vào một trong các vị trí sau:

  1. Bên trong lớp hiện tại.
  2. Bên trong lớp khác.
  3. Bên trong lớp nặc danh.

1. Xử lý sự kiện Java AWT - bên trong lớp hiện tại

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     

package vn.viettuts.awt;

 

import java.awt.Button;

import java.awt.Frame;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

public class AWTEventHandling1 extends Frame implements ActionListener {

    private TextField textField;

 

    public AWTEventHandling1() {

 

        // tạo các thành phần

        textField = new TextField();

        textField.setBounds(60, 80, 170, 20);

        Button button = new Button("click me");

        button.setBounds(100, 120, 80, 30);

 

        // đăng ký trình listener

        button.addActionListener(this);

 

        // thêm thành phần, kích thước, layout, khả năng hiển thị

        setTitle("Vi du xu ly su kien Java AWT");

        add(button);

        add(textField);

        setSize(400, 300);

        setLayout(null);

        setVisible(true);

    }

 

    @Override

    public void actionPerformed(ActionEvent e) {

        textField.setText("Welcome to Java AWT");

    }

 

    public static void main(String args[]) {

        new AWTEventHandling1();

    }

}

Kết quả:

Ví dụ Xử lý sự kiện Java AWT

Xử lý sự kiện Java AWT - bên trong lớp hiện khác

Tạo lớp view: AWTView.java

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     

package vn.viettuts.awt;

 

import java.awt.Button;

import java.awt.Frame;

import java.awt.TextField;

 

public class AWTView extends Frame {

    TextField textField;

 

    AWTView() {

        // tạo các thành phần

        textField = new TextField();

        textField.setBounds(60, 80, 170, 20);

        Button button = new Button("click me");

        button.setBounds(100, 120, 80, 30);

        // đăng ký listener

        Controller obj = new Controller(this);

        button.addActionListener(obj);

        // thêm thành phần, kích thước, layout, khả năng hiển thị

        setTitle("Vi du xu ly su kien Java AWT");

        add(button);

        add(textField);

        setSize(400, 300);

        setLayout(null);

        setVisible(true);

    }

}

Tạo lớp controller: Controller.java

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17     

package vn.viettuts.awt;

 

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

public class Controller implements ActionListener {

    AWTView obj;

 

    Controller(AWTView obj) {

        this.obj = obj;

    }

 

    @Override

    public void actionPerformed(ActionEvent e) {

        obj.textField.setText("Welcome to Java AWT");

    }

}

Tạo lớp AWTEventHandling2.java

1

2     

3

4

5

6

7      

package vn.viettuts.awt;

 

public class AWTEventHandling2 {

    public static void main(String[] args) {

        AWTView view = new AWTView();

    }

}

Kết quả:

Ví dụ Xử lý sự kiện Java AWT

Xử lý sự kiện Java AWT - bên trong lớp nặc danh

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

package vn.viettuts.awt;

 

import java.awt.Button;

import java.awt.Frame;

import java.awt.TextField;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

public class AWTEventHandling3 extends Frame {

    TextField textField;

 

    AWTEventHandling3() {

        textField = new TextField();

        textField.setBounds(60, 50, 170, 20);

        Button button = new Button("click me");

        button.setBounds(50, 120, 80, 30);

 

        button.addActionListener(new ActionListener() {

            @Override

            public void actionPerformed(ActionEvent e) {

                textField.setText("Hello World!");

 

            }

        });

        setTitle("Vi du xu ly su kien Java AWT");

        add(button);

        add(textField);

        setSize(400, 300);

        setLayout(null);

        setVisible(true);

    }

 

    public static void main(String args[]) {

        new AWTEventHandling3();

    }

}

Kết quả:

Ví dụ Xử lý sự kiện Java AWT

Nguồn tin: viettuts