Bài 92: List trong Java AWT

Ngày đăng: 1/2/2023 3:55:51 PM

Khai báo lớp AWT List

1       

public class List extends Component implements ItemSelectable, Accessible


Ví dụ List trong Java AWT

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.Frame;

import java.awt.List;

 

public class ListExample1 {

    public ListExample1() {

        Frame frame = new Frame();

        frame.setTitle("Ví dụ List trong Java AWT");

        List list = new List(5);

        list.setBounds(100, 50, 100, 80);

        list.add("C++");

        list.add("Java");

        list.add("PHP");

        list.add("Python");

        list.add("C#");

        list.add("Adroid");

        frame.add(list);

        frame.setSize(400, 250);

        frame.setLayout(null);

        frame.setVisible(true);

    }

 

    public static void main(String args[]) {

        new ListExample1();

    }

}

Kết quả:

Ví dụ List trong Java AWT


Ví dụ List trong Java AWT với ActionListener

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

package vn.viettuts.awt;

 

import java.awt.Button;

import java.awt.Frame;

import java.awt.Label;

import java.awt.List;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

 

public class ListExample2 {

    ListExample2() {

        Frame frame = new Frame();

        final Label label = new Label();

        label.setAlignment(Label.CENTER);

        label.setSize(500, 100);

        Button button = new Button("Show");

        button.setBounds(200, 150, 80, 30);

        // tham so thu hai cua ham khoi tao List = false, không cho phép chọn nhiều

        final List list1 = new List(4, false);

        list1.setBounds(100, 100, 80, 80);

        list1.add("C");

        list1.add("C++");

        list1.add("Java");

        list1.add("PHP");

        // tham so thu hai cua ham khoi tao List = true, cho phép chọn nhiều

        final List list2 = new List(4, true);

        list2.setBounds(100, 200, 80, 80);

        list2.add("Struts");

        list2.add("Spring");

        list2.add("Hibernate");

        list2.add("EJB");

        // add các thành phần vào frame

        frame.setTitle("Ví dụ List trong Java AWT");

        frame.add(list1);

        frame.add(list2);

        frame.add(label);

        frame.add(button);

        frame.setSize(500, 300);

        frame.setLayout(null);

        frame.setVisible(true);

        // đăng ký Listener

        button.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {

                String data = "Ngôn ngữ lập trình được chọn: " +

                        list1.getItem(list1.getSelectedIndex());

                data += ", Framework được chọn:";

                for (String frame : list2.getSelectedItems()) {

                    data += frame + " ";

                }

                label.setText(data);

            }

        });

    }

 

    public static void main(String args[]) {

        new ListExample2();

    }

}

Kết quả:

Ví dụ List trong Java AWT

Nguồn tin: viettuts