Bài 100: ActionListener trong Java AWT

Ngày đăng: 1/4/2023 9:47:12 AM

Phương thức actionPerformed()

Phương thức actionPerformed() được gọi tự động bất cứ khi nào bạn nhấp vào thành phần đã đăng ký.

1     

public abstract void actionPerformed(ActionEvent e);

Ví dụ về Java ActionListener: click button

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    

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 ActionListenerExample {

    public static void main(String[] args) {

        Frame frame = new Frame("Ví dụ ActionListener trong Java AWT");

        final TextField textField = new TextField();

        textField.setBounds(505015020);

        Button button = new Button("Click Here");

        button.setBounds(501006030);

 

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                textField.setText("Welcome to VietTuts.Vn");

            }

        });

        frame.add(button);

        frame.add(textField);

        frame.setSize(450200);

        frame.setLayout(null);

        frame.setVisible(true);

    }

}

Kết quả:

Ví dụ ActionListener trong Java AWT

Nguồn tin: viettuts