Sự kiện xử lý button trong java



Tráng thái của một đối tượng bị thay đổi được gọi là một sự kiện (event). Ví dụ, bấm vào nút, kéo chuột, vv Gói java.awt.event cung cấp nhiều lớp sự kiện và interface Listener để xử lý sự kiện Java AWT.


Nội dung chính

  • Các lớp Java Event và các interface Listener
  • Các bước để thực hiện xử lý sự kiện trong Java AWT
    • Phương thức đăng ký
  • Ví dụ xử lý sự kiện trong Java AWT
    • 1. Xử lý sự kiện Java AWT - bên trong lớp hiện tại
    • Xử lý sự kiện Java AWT - bên trong lớp hiện khác
    • Xử lý sự kiện Java AWT - bên trong lớp nặc danh

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

Các lớp EventCá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

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ả:

Sự kiện xử lý button trong java

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

Tạo lớp view: AWTView.java

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

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

package vn.viettuts.awt;

public class AWTEventHandling2 {
    public static void main(String[] args) {
        AWTView view = new AWTView();
    }
}

Kết quả:

Sự kiện xử lý button trong java

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

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ả:

Sự kiện xử lý button trong java