佈局管理器
在 java.awt 包中提供了 5 種佈局管理器,分別是 FlowLayout( 流式佈局管理器)、BorderLayout(邊界佈局管理器)、GridLayout(網格佈局管理器)、GridBagLayout(網格包佈局管理器)和 CardLayout(卡片佈局管理器)。每個容器在創建時都會使用一種默認的佈局管理器,在程序中可以通過調用容器對象的 setLayout() 方法設置佈局管理器,通過佈局管理器自動進行組件的佈局管理;
例如, 把一個 Frame 窗體的佈局管理器設置爲 FlowLayout,代碼如下:
Frame frame = new Frame();
frame.setLayout(new FlowLayout());
1. 流式佈局管理器
流式佈局管理器(FlowLayout)是最簡單的佈局管理器,在這種佈局下,容器會將組件按照添加順序從左向右放置。當到達容器的邊界時,會自動將組件放到下一行的開始位置。這些組件可以左對齊、居中對齊(默認方式)或右對齊的方式排列;
java.awt.FlowLayout,詳見:Class FlowLayout ;
流式佈局管理器 FlowLayout 的應用:
import java. awt. *;
public class Test {
public static void main ( String[ ] args) {
Frame f = new Frame ( "Regino" ) ;
f. setLayout ( new FlowLayout ( FlowLayout. LEFT, 20 , 30 ) ) ;
f. setSize ( 200 , 300 ) ;
f. setLocation ( 300 , 200 ) ;
f. setVisible ( true ) ;
f. add ( new Button ( "First" ) ) ;
f. add ( new Button ( "Second" ) ) ;
f. add ( new Button ( "Third" ) ) ;
f. add ( new Button ( "Forth" ) ) ;
}
}
效果圖:
2. 邊界佈局管理器
BorderLayout(邊界佈局管理器)是一種較爲複雜的佈局方式,它將容器劃分爲5個區域,分別是東(EAST)、南(SOUTH)、西(WEST)、北(NORTH)、中(CENTER)。組件可以被放置在這 5 個區域中的任意一個;
java.awt.BorderLayout,詳見:Class BorderLayout ;
邊界佈局管理器 BordLayout 的應用:
import java. awt. *;
public class Test {
public static void main ( String[ ] args) {
Frame f = new Frame ( "Regino" ) ;
f. setLayout ( new BorderLayout ( ) ) ;
f. setSize ( 200 , 300 ) ;
f. setLocation ( 300 , 200 ) ;
f. setVisible ( true ) ;
f. add ( new Button ( "First" ) , BorderLayout. EAST) ;
f. add ( new Button ( "Second" ) , BorderLayout. WEST) ;
f. add ( new Button ( "Third" ) , BorderLayout. SOUTH) ;
f. add ( new Button ( "Forth" ) , BorderLayout. NORTH) ;
f. add ( new Button ( "Fifth" ) , BorderLayout. CENTER) ;
}
}
效果圖:
3. 網格佈局管理器
GridLayout(網格佈局管理器)使用縱橫線將容器分成 n 行 m 列大小相等的網格,每個網格中放置一個組件。添加到容器中的組件首先放置在第1行第1列(左上角)的網格中, 然後在第1行的網格中從左向右依次放置其他組件,第1行滿後,繼續在下一行中從左到右放置組件。與 FlowLayout 不同的是,放置在 GridLayout 佈局管理器中的組件將自動佔據網格的整個區域;
java.awt.GridLayout,詳見:Class GridLayout ;
網格佈局管理器 GridLayout 的應用:
import java. awt. *;
public class Test {
public static void main ( String[ ] args) {
Frame f = new Frame ( "Regino" ) ;
f. setLayout ( new GridLayout ( 2 , 2 ) ) ;
f. setSize ( 200 , 300 ) ;
f. setLocation ( 300 , 200 ) ;
f. setVisible ( true ) ;
f. add ( new Button ( "1" ) ) ;
f. add ( new Button ( "2" ) ) ;
f. add ( new Button ( "3" ) ) ;
f. add ( new Button ( "4" ) ) ;
}
}
效果圖:
4. 網格包佈局管理器
GridBagLayout(網格包佈局管理器)是最靈活、最複雜的佈局管理器。它與 GridLayout 佈局管理器類似,不同的是,它允許網格中的組件大小各不相同,而且允許一個組件跨越一個或者多個網格;
java.awt.GridBagLayout,詳見:Class GridBagLayout ;
網格包佈局管理器 GridBagLayout 的應用:
import java. awt. *;
class Layout extends Frame {
Layout ( String title) {
GridBagLayout layout = new GridBagLayout ( ) ;
GridBagConstraints c = new GridBagConstraints ( ) ;
this . setLayout ( layout) ;
c. fill = GridBagConstraints. BOTH;
c. weightx = 1 ;
c. weighty = 1 ;
this . addComponent ( "11" , layout, c) ;
this . addComponent ( "12" , layout, c) ;
this . addComponent ( "13" , layout, c) ;
c. gridwidth = GridBagConstraints. REMAINDER;
this . addComponent ( "14" , layout, c) ;
c. weightx = 0 ;
c. weighty = 0 ;
addComponent ( "21" , layout, c) ;
c. gridwidth = 1 ;
this . addComponent ( "31" , layout, c) ;
c. gridwidth = GridBagConstraints. REMAINDER;
this . addComponent ( "32" , layout, c) ;
c. gridheight = 2 ;
c. gridwidth = 1 ;
c. weightx = 2 ;
c. weighty = 2 ;
this . addComponent ( "41" , layout, c) ;
c. gridwidth = GridBagConstraints. REMAINDER;
c. gridheight = 1 ;
this . addComponent ( "42" , layout, c) ;
this . addComponent ( "42" , layout, c) ;
this . setTitle ( title) ;
this . pack ( ) ;
this . setVisible ( true ) ;
}
private void addComponent ( String name, GridBagLayout layout, GridBagConstraints c) {
Button b = new Button ( name) ;
layout. setConstraints ( b, c) ;
this . add ( b) ;
}
}
public class Test {
public static void main ( String[ ] args) {
new Layout ( "Regino" ) ;
}
}
效果圖:
5. 卡片佈局管理器
在操作程序時,經常會通過選項卡來切換程序中的界面,這些界面就相當於一張張卡片,而管理這些卡片的佈局管理器就是卡片佈局管理器(CardLayout)。卡片佈局管理器將界面看作一系列卡片,在任何時候只有其中一張卡片是可見的,這張卡片佔據容器的整個區域;
java.awt.CardLayout,詳見:Class CardLayout ;
卡片佈局管理器 CardLayout 的應用:
import java. awt. *;
import java. awt. event. ActionEvent;
import java. awt. event. ActionListener;
import java. awt. event. WindowAdapter;
import java. awt. event. WindowEvent;
class Cardlayout extends Frame implements ActionListener {
Panel cardPanel = new Panel ( ) ;
Panel controlpaPanel = new Panel ( ) ;
Button nextbutton, preButton;
CardLayout cardLayout = new CardLayout ( ) ;
public Cardlayout ( ) {
setSize ( 300 , 200 ) ;
setVisible ( true ) ;
this . addWindowListener ( new WindowAdapter ( ) {
public void windowClosing ( WindowEvent e) {
Cardlayout. this . dispose ( ) ;
}
} ) ;
cardPanel. setLayout ( cardLayout) ;
cardPanel. add ( new Label ( "page 1" , Label. CENTER) ) ;
cardPanel. add ( new Label ( "page 2" , Label. CENTER) ) ;
cardPanel. add ( new Label ( "page 3" , Label. CENTER) ) ;
nextbutton = new Button ( "next" ) ;
preButton = new Button ( "pre" ) ;
nextbutton. addActionListener ( this ) ;
preButton. addActionListener ( this ) ;
controlpaPanel. add ( preButton) ;
controlpaPanel. add ( nextbutton) ;
this . add ( cardPanel, BorderLayout. CENTER) ;
this . add ( controlpaPanel, BorderLayout. SOUTH) ;
}
public void actionPerformed ( ActionEvent e) {
if ( e. getSource ( ) == nextbutton) {
cardLayout. next ( cardPanel) ;
}
if ( e. getSource ( ) == preButton) {
cardLayout. previous ( cardPanel) ;
}
}
}
public class Test {
public static void main ( String[ ] args) {
Cardlayout cardlayout = new Cardlayout ( ) ;
}
}
效果圖:
6. 自定義佈局
當一個容器被創建後,都會有一個默認的佈局管理器。Window、Frame 和 Dialog 的默認佈局管理器是 BorderLayout,Panel 的默認佈局管理器是 FlowLayout。如果不希望通過佈局管理器對容器進行佈局,也可以調用容器的 setLayout(null) 方法,將佈局管理器取消。在這種情況下,程序必須調用容器中每個組件的 setSize() 和 setLocation() 方法或者 setBounds() 方法( 這個方法接收 4 個參數,分別是左上角的 x、 y 座標和組件的長、 寬),爲這些組件在容器中定位;
應用自定義佈局:
import java. awt. *;
public class Test {
public static void main ( String[ ] args) {
Frame f = new Frame ( "Regino" ) ;
f. setLayout ( null) ;
f. setSize ( 300 , 150 ) ;
Button btn1 = new Button ( "press" ) ;
Button btn2 = new Button ( "pop" ) ;
btn1. setBounds ( 40 , 60 , 100 , 30 ) ;
btn2. setBounds ( 140 , 90 , 100 , 30 ) ;
f. add ( btn1) ;
f. add ( btn2) ;
f. setVisible ( true ) ;
}
}
效果圖: