AWT是JFC(Java基本類)的核心,為JFC的構(gòu)成提供了以下的基本結(jié)構(gòu):
- 代理事件模型
- 輕量構(gòu)件
- 剪貼板和數(shù)據(jù)傳輸
- 打印和無鼠標(biāo)操作
Java抽象窗口工具集有四個主要的類,即:構(gòu)件類(Component)、容器類(Container)、圖形類(Graphics)、和布局管理類(LayoutManager和LayoutMangager2)。容器包含構(gòu)件,布局管理器確定容器內(nèi)構(gòu)件的位置和形狀。圖形(Graphics)類提供在構(gòu)件中顯示文本框和圖形的方法。
1.強(qiáng)制一個容器布置它的構(gòu)件
在任何時間給定的構(gòu)件都是有效(valid)或無效(invalid)的。無效的構(gòu)件需要被布置,而有效的構(gòu)件不需要。訪問一個 無效容器(它的同位體已經(jīng)產(chǎn)生)的validate()引起對容器layout()方法的調(diào)用。使構(gòu)件無效的過程不僅使構(gòu)件自己變得無效,而且也會使構(gòu)件所在的容器變的無效。
使構(gòu)件無效的Component和Container方法:
void Component.addNotify()
void Component.show() (invalidates parent container)
void Component.hide() (invalidates parent container)
void Component.reshap(int x,int y,int width,int height)
void Component.setSize(int width,int height)
void Component.setBounds(int x,int y,int width,int height)
void Component.setLayout(LayoutManager)
void Container.add(Component)
void Container.remove(Component)
void Container.removeAll(Component)
void Container.setLayout(LayoutManager)
因此我們可以把希望重新布置的構(gòu)件設(shè)置為無效,然后調(diào)用該構(gòu)件父容器的validate()方法即可。