Ask discuss on java awt events

** #Java #awt #events #gui #Java swing **

** Sự kiện Java AWT **

Bộ công cụ cửa sổ trừu tượng (AWT) là một tập hợp các lớp Java cung cấp chức năng cơ bản để tạo và quản lý giao diện người dùng đồ họa (GUI).Các sự kiện AWT là các thông điệp được gửi đến một thành phần AWT khi có điều gì đó xảy ra ảnh hưởng đến thành phần.Ví dụ: khi người dùng nhấp vào nút, sự kiện nhấp chuột được gửi đến nút.

Các sự kiện AWT được xử lý bởi cơ chế người nghe sự kiện.Người nghe sự kiện là một lớp thực hiện một hoặc nhiều giao diện người nghe sự kiện AWT.Khi một sự kiện xảy ra, khung AWT gọi phương thức thích hợp trong trình nghe sự kiện.

Sau đây là các giao diện người nghe sự kiện AWT chính:

*** Mouselistener ** - Giao diện này được triển khai bởi các lớp muốn nhận các sự kiện chuột, chẳng hạn như nhấp chuột và chuyển động của chuột.
*** Keylistener ** - Giao diện này được triển khai bởi các lớp muốn nhận các sự kiện bàn phím, chẳng hạn như nhấn phím và phát hành khóa.
*** Windowlistener ** - Giao diện này được triển khai bởi các lớp muốn nhận các sự kiện cửa sổ, chẳng hạn như cửa sổ mở, đóng cửa sổ và biểu tượng cửa sổ.

Để thêm trình nghe sự kiện vào một thành phần AWT, bạn gọi phương thức addXxxListener () của thành phần, trong đó xxx là tên của giao diện người nghe sự kiện.Ví dụ: để thêm trình nghe chuột vào nút, bạn sẽ gọi phương thức addMousElistener () của nút.

Khi một sự kiện xảy ra, khung AWT gọi phương thức thích hợp trong trình nghe sự kiện.Phương thức người nghe sự kiện thường xử lý sự kiện bằng cách thực hiện một số hành động, chẳng hạn như thay đổi trạng thái của thành phần hoặc sơn lại thành phần.

Dưới đây là một ví dụ về cách thêm người nghe chuột vào nút và xử lý sự kiện nhấp chuột:

`` `java
nhập java.awt.*;
nhập java.awt.event.*;

lớp công khai mybutton mở rộng nút {

mybutton (chuỗi văn bản) công khai {
siêu (văn bản);

// Thêm người nghe chuột vào nút
addMousElistener (mousead CHƯƠNG () {
@Ghi đè
công khai void mouseClicky (mouseEvent e) {
// Xử lý sự kiện nhấp chuột
System.out.println ("nút đã được nhấp!");
}
});
}

công khai void void main (String [] args) {
// Tạo khung và thêm một nút vào nó
Khung khung = khung mới ("khung của tôi");
Nút myButton = new MyButton ("Nhấp vào tôi!");
frame.add (nút);

// Hiển thị khung
frame.pack ();
frame.setVisible (true);
}
}
`` `

Để biết thêm thông tin về các sự kiện Java AWT, vui lòng xem các tài nguyên sau:

* [Hướng dẫn Java: Sự kiện AWT] (Lesson: Writing Event Listeners (The Java™ Tutorials > Creating a GUI With Swing))
* [Tài liệu API Java: Các sự kiện AWT] (java.awt.event (Java Platform SE 8 ))
=======================================
**#java #awt #events #gui #Java swing**

**Java AWT Events**

The Abstract Window Toolkit (AWT) is a set of Java classes that provide the basic functionality for creating and managing graphical user interfaces (GUIs). AWT events are the messages that are sent to an AWT component when something happens that affects the component. For example, when a user clicks on a button, a mouse click event is sent to the button.

AWT events are handled by the event listener mechanism. An event listener is a class that implements one or more of the AWT event listener interfaces. When an event occurs, the AWT framework calls the appropriate method in the event listener.

The following are the main AWT event listener interfaces:

* **MouseListener** - This interface is implemented by classes that want to receive mouse events, such as mouse clicks and mouse movements.
* **KeyListener** - This interface is implemented by classes that want to receive keyboard events, such as key presses and key releases.
* **WindowListener** - This interface is implemented by classes that want to receive window events, such as window opens, window closes, and window iconifies.

To add an event listener to an AWT component, you call the component's addXXXListener() method, where XXX is the name of the event listener interface. For example, to add a mouse listener to a button, you would call the button's addMouseListener() method.

When an event occurs, the AWT framework calls the appropriate method in the event listener. The event listener method typically handles the event by performing some action, such as changing the state of the component or repainting the component.

Here is an example of how to add a mouse listener to a button and handle the mouse click event:

```java
import java.awt.*;
import java.awt.event.*;

public class MyButton extends Button {

public MyButton(String text) {
super(text);

// Add a mouse listener to the button
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
// Handle the mouse click event
System.out.println("The button was clicked!");
}
});
}

public static void main(String[] args) {
// Create a frame and add a button to it
Frame frame = new Frame("My Frame");
MyButton button = new MyButton("Click Me!");
frame.add(button);

// Display the frame
frame.pack();
frame.setVisible(true);
}
}
```

For more information on Java AWT events, please see the following resources:

* [The Java Tutorials: AWT Events](https://docs.oracle.com/javase/tutorial/uiswing/events/index.html)
* [The Java API Documentation: AWT Events](https://docs.oracle.com/javase/8/docs/api/java/awt/event/package-summary.html)
 
Join Telegram ToolsKiemTrieuDoGroup
Back
Top