11.2 AWT容器
容器(Container)是Component的子類,Component類有如下幾個常用方法.
setLocation(int x , int y): 設置組件位置。
setSize(int width , int height):設置組件大小。
setBounds(int x ,int y ,int width , int height):設置組件的位置及大小。
setVisible(Boolean b):設置組件可見。
Frame 默認使用BorderLayout佈局管理器。
Panel 是 AWT 中一個典型的容器,它不能獨立存在,必須放到其他容器中的容器。
Panel 容器有如下幾個特點:
1.可作爲容器來裝其他的組件,爲放置組件提供空間。
2.不能單獨的存在,必須放到其他容器中。
3. Panel 默認使用FlowLayout作爲其佈局管理器。
使用示例如下:
import java.awt.*;
public class TestPanel
{
public static void main(String[] args)
{
Frame f = new Frame("測試窗口");
//創建一個Panel對象
Panel p = new Panel();
//相Panel對象中添加兩個組件
p.add(new TextField(20));
p.add(new Button("單擊我"));
f.add(p);
//設置窗口的大小、位置
f.setBounds(30, 30 , 250, 120);
//將窗口顯示出來(Frame對象默認處於隱藏狀態)
f.setVisible(true);
}
}
ScrollPane 是一個帶滾動條的容器。它也不能獨立存大,必須添加到其他容器中。
ScrollPane 默認使用BorderLayout佈局管理器。