AWT中常用的布局管理器有如下幾個:FlowLayout, BorderLayout, GridLayout, GridBagLayout, CardLayout,Swing還提供了一個BoxLayout.
FlowLayout從左向右排列所有組件,遇到邊界就會折回下一行從新開始.它有三個構造器FlowLayout(),FlowLayout(int align)和 FlowLayout(int align, int hgap, int vgap),其中的hgap和vgap代表水平間距和垂直間距,align指的是組件的排列方向(從左向右,從右向左,從中間向兩邊),我們可以使用FlowLayout的靜態常量來設置這個參數:FlowLayout.LEFT,FlowLayout.CENTER,FlowLayout.RIGHT.
BorderLayout將容器分為EAST,SOUTH,WEST,NORTH,CENTER五個區域,如下圖所示:
我們在向使用此布局管理器的容器中添加組件時,需要制定添加到的區域,否則就默認添加到中間區域里,而當我們向一個區域添加多個組件時,後放入的組件會覆蓋前面的組件.BorderLayout有兩個構造器,BorderLayout()和BorderLayout(int hgap,int vgap),hgap和vgap代表的水平間距和垂直間距.我們在指定組件添加到的區域時,可以使用它的靜態常量:BorderLayout.EAST, BorderLayout.WEST, BorderLayout.NORTH, BorderLayout.SOUTH, BorderLayout.CENTER.例如:
Frame f = new Frame();
f.setLayout(new BorderLayout(5,5));
f.add(new Button("南"),SOUTH);//將一個按鈕添加到南的位置
BorderLayout最多只能放5個組件,但是實際上我們可以先在Panel中添加多個組件,再將Panel添加到BorderLayout布局管理器中,因此我們實際可以放的組件要遠遠超過5個.
GridLayout將容器分割成大小相同的網格,我們在添加組件時將默認從左到右從上到下,將組件依次添加到每個網格中,而每個組件的大小也就由其所添加到的網格的大小所決定.GridLayout同樣也有兩個構造器,GridLayout(int rows,int cols)和GridLayout(int rows ,int cols,int hgap,int vgap),使用GridLayout的典型例子就是計算器的窗口:
import java.awt.*;
public class calculator
{
public static void main(String[] args)
{
Frame f = new Frame("計算器");
Panel p1 = new Panel();
p1.add(new TextField(30));
f.add(p1,BorderLayout.NORTH);
//設置p2採用GridLayout布局管理器
Panel p2 = new Panel();
p2.setLayout(new GridLayout(3,5,4,4));
String[] name = {"0","1","2","3","4","5","6","7","8","9"," ","-","*","/","."};
for(int i=0;i<name.length;i )
{
p2.add(new Button(name[i]));
}
f.add(p2);//默認添加到中間
f.pack();// 設置窗口為最佳大小
f.setVisible(true);
}
}
運行結果如圖:
CardLayout將加入容器的所有組件看成一疊卡片,每次只有最上面的那個Component才可見,它有兩個構造器CardLayout()和CardLayout(int hgap, int vgap),有五個方法用來控制其中的組件:
first(Container target);//顯示target容器中的第一張卡片
last(Container target);
previous(Container target);
next(Container target);
show(Container target,String name);//顯示targer容器中指定名字的卡片
例子:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class calculator {
Frame f;
Panel p1;
Panel p2;
String[] name = {"1","2","3","4","5"};
CardLayout c;
public void init()
{
f = new Frame("yz");
p1 = new Panel();
p2 = new Panel();
c = new CardLayout();
p1.setLayout(c);
for(int i=0;i<name.length;i )
{
p1.add(name[i],new Button(name[i]));
}
//控制顯示上一張的按鈕
Button previous = new Button("上一張");
previous.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c.previous(p1);
}
});
//控制顯示下一張的按鈕
Button next = new Button("下一張");
next.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c.next(p1);
}
});
//控制顯示第一張的按鈕
Button first = new Button("第一張");
first.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c.first(p1);
}
});
//控制顯示
一張的按鈕
Button last = new Button("
一張");
last.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c.last(p1);
}
});
//根據card名顯示的按鈕
Button third = new Button("第三張");
third.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
c.show(p1,"3");
}
});
p2.add(previous);
p2.add(next);
p2.add(first);
p2.add(last);
p2.add(third);
f.add(p1);//默認添加到中間
f.add(p2,BorderLayout.SOUTH);
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
new calculator().init();
}
}
GridBagLayout是功能最強大也是最複雜的布局管理器,添加到其中的組件可以橫跨一個或多個網格,並可以設置各網格的大小各不相同,當窗口大小發生變化時,其也可以準確的控制窗口各部分的反應.為了處理GridBagLayout中組件的大小和跨越性,我們還需要一個GridBagConstraints對象,用這個對象與特定的組件相關聯,來控制組件的大小和跨越性.在使用GridBagLayout時一般需要4步:
1. 創建GridBagLayout,並指定容器使用該布局管理器
GridBagLayout gb = new GridBagLayout();
container.setLayout(gb);
2. 創建GridBagConstraints的對象,並設置該對象的相關屬性
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx=2;
gbc.gridy=1;
gbc.gridwidth=2;
gbc.gridheight=1;
3. 調用GridBagLayout對象的方法來建立GridBagConstraints對象與受控制組件之間的聯繫.
gb.setConstraints(c,gbc);//設置c組件受gbc控制
4. 添加組件
container.add(c);
通常我們可以將2,3,4步寫成一個addComponent方法,為這個方法傳遞所需要的參數,來進行添加組件的化簡.例如:
public void addComponent(Component c, int gx, int gy, int gw,int gh)
{
this.gridx=gx;
this.gridy=gy;
this.gridwidth=gw;
this.gridheight=gh;
gb.setConstraints(c,gbc);
container.add(c);
}
使用GridBagLayout關鍵在於GridBagConstraints,該類具有如下幾個方法:
fill:設置組件如何佔領空白區域,它可取如下幾個值:GridBagConstraints.NONE, GridBagConstraints.HORIZONTAL, GridBagConstraints.VERTICAL, GridBagConstraints.BOTH.
gridx,gridy:設置組件的左上角所在網格的索引(網格的索引從0 開始),此外這兩個值還可以設為GridBagConstraints.RELATIVE,這個值也是默認值,它表明當前組件緊跟在上一個組件之後.
gridwidht和gridheight:設置組件橫向縱向跨越多少個網格,他們的默認值都是1,如果該組件是橫向或縱向的
一個還可以將此值設為GridBagConstraints.REMAINDER,若為倒數第二個組件則可以設值為GridBagConstraints.RELATIVE.
ipadx和ipady:設置組件橫向縱向的內部填充大小,即在組件的最小尺寸上還需要增大多少,若設置了這個值則組件在最小尺寸的基礎上增大ipadx*2或 ipady*2像素.
weightx和weighty(double類型):就是權重,也就是組件組件佔領多餘空間的水平或垂直增加比例,默認值為0也就是不佔領多餘空間.例如有三個組件,我們將他們的水平增加比例分別設為1.0,2.0,3.0,當容器寬度增加60像素時,他們分別增加10,20和30像素.如果我們希望某個組件的大小會隨著容器的變化而變化,我們需要同時設置fill和weightx,weighty屬性.
Swing中的BoxLayout布局管理器提供了一個構造器:BoxLayout(Container targer,int axis),它制定創建基於targer容器的BoxLayout布局管理器,它裡面的組件按axis方向排列,axis有BoxLayout.X_AXIS和BoxLayout.Y_AXIS兩個方向.BoxLayout通常和Box容器結合使用,Box容器有點像Panel,它默認使用BoxLayout布局管理器.Box有兩個靜態方法來創建Box對象:createHorizontalBox()和createVerticalBox(),一旦獲得了Box容器之後,就可以使用Box來承裝普通GUI組件,然後再將這些Box組件添加到其他容器中,從而形成整體的窗口布局.例如:
public class Test
{
private Frame f = new Frame("cs");
private Box horizontal = Box.createHorizontalBox();
private Box vertical = Box.createVerticalBox();
public void init()
{
horizontal.add(new Button("shuiping1"));
horizontal.add(new Button("shuiping2"));
vertical.add(new Button("chuizhi1"));
vertical.add(new Button("chuizhi2"));
f.add(horizontal, BorderLayout.NORTH);
f.add(vertical);
f.pack();
f.setVisible(true);
}
public static void main(String[] args)
{
new Test().init();
}
}
我們還可以使用絕對定位:只需要將Container的布局管理器設為null即可,也就是setLayout(null),往容器中加組件的時候指定組件的位置和大小.一般調用setBounds(int x,int y,int width,int height).