Bài 97: Panel trong Java AWT

Ngày đăng: 1/2/2023 4:18:52 PM

Khai báo lớp AWT Panel

1      

public class Panel extends Container implements Accessible


Ví dụ Panel 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

28

29

30

31

package vn.viettuts.awt;

 

import java.awt.Button;

import java.awt.Color;

import java.awt.Frame;

import java.awt.Panel;

 

public class PanelExample {

    PanelExample() {

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

        Panel panel = new Panel();

        panel.setBounds(40, 80, 200, 200);

        panel.setBackground(Color.gray);

        Button button1 = new Button("Button 1");

        button1.setBounds(50, 100, 80, 30);

        button1.setBackground(Color.yellow);

        Button button2 = new Button("Button 2");

        button2.setBounds(100, 100, 80, 30);

        button2.setBackground(Color.green);

        panel.add(button1);

        panel.add(button2);

        frame.add(panel);

        frame.setSize(400, 400);

        frame.setLayout(null);

        frame.setVisible(true);

    }

 

    public static void main(String args[]) {

        new PanelExample();

    }

}

Kết quả:

Ví dụ Panel trong Java AWT

Nguồn tin: viettuts