Bài 94: Scrollbar trong Java AWT

Ngày đăng: 1/2/2023 4:01:03 PM

Khai báo lớp AWT Scrollbar

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

package vn.viettuts.awt;

 

import java.awt.Frame;

import java.awt.Scrollbar;

 

public class ScrollbarExample1 {

    public ScrollbarExample1() {

        Frame f = new Frame("Ví dụ Scrollbar trong Java AWT");

        Scrollbar s = new Scrollbar();

        s.setBounds(350, 150, 15, 200);

        f.add(s);

        f.setSize(400, 400);

        f.setLayout(null);

        f.setVisible(true);

    }

 

    public static void main(String args[]) {

        new ScrollbarExample1();

    }

}


 

Ví dụ Scrollbar trong Java AWT

Kết quả:

Ví dụ Scrollbar trong Java AWT

Ví dụ Scrollbar trong Java AWT với AdjustmentListener

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

package vn.viettuts.awt;

 

import java.awt.Frame;

import java.awt.Label;

import java.awt.Scrollbar;

import java.awt.event.AdjustmentEvent;

import java.awt.event.AdjustmentListener;

 

public class ScrollbarExample2 {

    public ScrollbarExample2() {

        Frame f = new Frame("Ví dụ Scrollbar trong Java AWT");

        final Label label = new Label();

        label.setAlignment(Label.CENTER);

        label.setSize(400, 100);

        final Scrollbar s = new Scrollbar();

        s.setBounds(350, 150, 15, 200);

        f.add(s);

        f.add(label);

        f.setSize(400, 400);

        f.setLayout(null);

        f.setVisible(true);

        s.addAdjustmentListener(new AdjustmentListener() {

            @Override

            public void adjustmentValueChanged(AdjustmentEvent e) {

                label.setText("Giá trị dọc của Scrollbar là:" + s.getValue());

            }

        });

    }

 

    public static void main(String args[]) {

        new ScrollbarExample2();

    }

}

Kết quả:

Ví dụ Scrollbar trong Java AWT

Nguồn tin: viettuts