java布局管理器總結_第1頁
java布局管理器總結_第2頁
java布局管理器總結_第3頁
java布局管理器總結_第4頁
java布局管理器總結_第5頁
已閱讀5頁,還剩22頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、java布局管理器總結(2013-02-25 21:42:32) 轉載標簽: 轉載原文地址:java布局管理器總結作者:技術羊之前在使用的過程中一直對java中swing的布局管理器感到很困惑,以下是在網上找到的一篇文章。其中我重點關注了一下gridbaglayout。寫的比較詳盡:BorderLayoutFlowLayoutGridLayoutGridBagLayoutCardLayoutBoxLayout1.BorderLayoutjava.lang.Object-java.awt.BorderLayout將版面劃分成東、西、南、北、中五個區域,將添加的組件按指定位置放置。BorderLa

2、yout.EAST BorderLayout.WEST BorderLayout.SOUTH BorderLayout.NORTH BorderLayout.CENTER 構造函數:BorderLayout()建立組件間無間距的BorderLayoutBorderLayout(int hgap,int vgap)建立組件間水平間距為hgap,垂直間距為vgap的BorderLayout例一:java view plaincopyprint?import java.awt.BorderLayout; import javax.swing.JFrame; import javax.swing.JB

3、utton; public class BorderLayoutDemo public static void main(String args) /建立一個JFrame,JFrame的默認LayoutManager為BorderLayout JFrame f=new JFrame("BorderLayout"); JButton btn=new JButton("BorderLayout.NORTH"); f.add(btn,BorderLayout.NORTH); btn=new JButton("BorderLayout.SOUTH&qu

4、ot;); f.add(btn,BorderLayout.SOUTH); btn=new JButton("BorderLayout.EAST"); f.add(btn,BorderLayout.EAST); btn=new JButton("BorderLayout.West"); f.add(btn,BorderLayout.WEST); btn=new JButton("BorderLayout.CENTER"); f.add(btn,BorderLayout.CENTER); f.pack(); f.setVisible(tr

5、ue); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 運行結果:在上例代碼的第13,14行之間插入以下代碼java view plaincopyprint?f.setLayout(new BorderLayout(10,10); 運行結果:2.FlowLayoutjava.lang.Object-java.awt.FlowLayout組件按從左到右而后從上到下的順序依次排列,一行不能放完則折到下一行。構造函數:FlowLayout()建立一個默認為居中對齊,組件彼此有5單位的水平與垂直間距的FlowLayoutFlowLayout(int

6、 align)建立一個可設置排列方式且組件彼此有5單位的水平與垂直間距的FlowLayoutFlowLayout(int align,int hgap,int vgap)建立一個可設置排列方式與組件間距的FlowLayout例二:java view plaincopyprint?import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JButton; public class FlowLayoutDemo public static void main(String args) JFrame f=new

7、 JFrame("FlowLayout"); f.setLayout(new FlowLayout(); for(int i=0;i<7;i+) JButton btn=new JButton("Button"+i); f.add(btn); f.setSize(300,150); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 運行結果:3.GridLayoutjava.lang.Object-java.awt.GridLayout矩形網格形式對容器的組件

8、進行布置構造函數:GridLayout()建立一個默認為一行一列的GridLayoutGridLayout(int rows,int cols)建立一個指定行(rows)和列(cols)的GridLayoutGridLayout(int rows,int cols,int hgap,int vgap)建立一個指定行(rows)和列(cols),且組件間水平間距為hgap、垂直間距為vgap的GridLayout例三:java view plaincopyprint?import java.awt.GridLayout; import javax.swing.JFrame; import jav

9、ax.swing.JButton; public class GridLayoutDemo public static void main(String args) JFrame f=new JFrame("GridLayout"); /設置f的布局管理器為3行3列的GridLayout,組件間水平與垂直間距為5 f.setLayout(new GridLayout(3,3,5,5); for(int i=1;i<10;i+) JButton btn=new JButton(String.valueOf(i); f.add(btn); f.pack(); f.setV

10、isible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 運行結果:4.GridBagLayoutjava.lang.Object-java.awt.GridBagLayoutGridBagLayout以表格形式布置容器內的組件,將每個組件放置在每個單元格內,而一個單元格可以跨越多個單元格合并成一個單元格,即多個單元格可以組合成一個單元格,從而實現組件的自由布局。構造函數:GridBagLayout()建立一個默認的GridBagLayout每一個單元格都有各自的屬性,而這些屬性由GridBagConstrainsts類的成

11、員變量來定義,且GridBagConstriaints中的所有成員變量都是public的。java.lang.Object-java.awt.GridBagConstratints構造函數:GridBagConstraints()建立一個默認的GridBagConstraintsGridBagConstraints(intgridx,int gridy,int gridwidth,int gridheight,double weightx,double weighty,int anchor,int fill,Insets insets,int ipadx,int ipady)建立一個指定其參數

12、值的GridBagConstraintsGridBagConstraints的成員變量:int gridxint gridyint gridwidthint gridheightdouble weightxdouble weightyint anchorint fillInsets insetsint ipadxint ipadygridx,gridy:設置組件所處行與列的起始坐標。例如gridx=0,gridy=0表示將組件放置在0行0列單元格內。gridwidth,gridheight:設置組件橫向與縱向的單元格跨越個數。可以通過GridBagConstraints的RELETIVE,和R

13、EMAINDER來進行指定,它的用法是:當把gridx值設置為GridBagConstriants.RELETIVE時,所添加的組件將被放置在前一個組件的右側。同理,對gridy 值設置為GridBagConstraints.RELETIVE時,所添加的組件將被放置在前一個組件的下方,(這是一種根據前一個組件而決定當前組件的相對放置方式)對gridweight和gridheight也可以應用GridBagConstraints的REMAINDER方式,創建的組件會從創建的起點位置開始一直延伸到容器所能允許的界限為止。該功能使得你可以創建跨越某些行或列的組件,從而改變相應方向上組件的數目,即使其

14、后在布局的其他地方添加額外的組件也是如此。weightx,weighty:設置窗口變大時的縮放比例。anchor:設置組件在單元格中的對齊方式。由以下常量來定義GridBagConstraints.CENTERGridBagConstraints.EASTGridBagConstraints.WESTGridBagConstraints.SOUTHGridBagConstraints.NORTHGridBagConstraints.SOUTHEASTGrisBagConstraints.SOUTHWESTGridBagConstraints.NORTHEASTGridBagConstraint

15、s.NORTHWESTfill:當某個組件未能填滿單元格時,可由此屬性設置橫向、縱向或雙向填滿。由以下常量來定義GridBagConstraints.NONEGridBagConstraints.HORIZONTALGridBagConstraints.VERTICALGridBagConstraints.BOTHinsets:設置單元格的間距。java.lang.Object-java.awt.InsetsInsets(int top,int left,int bottom,int right)ipadx,ipady:將單元格內的組件的最小尺寸橫向或縱向擴大。若一個組件的尺寸為30*10像素

16、,ipadx=2,ipady=3,則單元格內的組件最小尺寸為34*16像素例四:java view plaincopyprint?import java.awt.GridBagLayout; import java.awt.GridBagConstraints; import java.awt.Insets; import javax.swing.JFrame; import javax.swing.JButton; public class GridBagLayoutDemo public static void main(String args) JFrame f=new JFrame(&

17、quot;GridBagLayout"); f.setLayout(new GridBagLayout(); JButton btn=new JButton("first"); GridBagConstraints gbc=new GridBagConstraints(); /設定第一個單元格的屬性值 gbc.gridx=0; gbc.gridy=0; gbc.gridwidth=1; gbc.gridheight=1; gbc.weightx=0; gbc.weighty=0; gbc.anchor=GridBagConstraints.NORTHWEST; g

18、bc.fill=GridBagConstraints.NONE; gbc.insets=new Insets(0,0,0,0); gbc.ipadx=0; gbc.ipady=0; f.add(btn,gbc); /設定第二個單元格屬性值 gbc.gridx=1; gbc.gridy=0; gbc.gridwidth=GridBagConstraints.REMAINDER; gbc.gridheight=1; gbc.weightx=1; gbc.weighty=0; gbc.anchor=GridBagConstraints.CENTER; gbc.fill=GridBagConstrai

19、nts.HORIZONTAL; gbc.insets=new Insets(5,5,5,5); gbc.ipadx=0; gbc.ipady=0; btn=new JButton("second"); f.add(btn,gbc); /設定第三個單元格屬性值 gbc.gridx=0; gbc.gridy=1; gbc.gridwidth=1; gbc.gridheight=GridBagConstraints.REMAINDER; gbc.weightx=0; gbc.weighty=1; gbc.anchor=GridBagConstraints.CENTER; gbc.

20、fill=GridBagConstraints.VERTICAL; gbc.insets=new Insets(0,0,0,0); gbc.ipadx=10; gbc.ipady=10; btn=new JButton("three"); f.add(btn,gbc); f.pack(); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 運行結果:將窗口變大后的效果:5.CardLayoutjava.lang.Object-rdLayout以層疊的方式布置組件,如同很多張卡片疊在一起

21、,從而只能看到最上面的那一張卡片。構造函數:CardLayout()建立一個無間距的CardLayoutCardLayout(int hgap,int vgap)建立一個水平間距為hgap、垂直間距為vgap的CardLayout例五:java view plaincopyprint?import java.awt.BorderLayout; imporrdLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JFrame; import javax

22、.swing.JPanel; import javax.swing.JLabel; import javax.swing.JButton; public class CardLayoutDemo private static JPanel p; public static void main(String args) JFrame f=new JFrame("CardLayout"); p=new JPanel(); /將JPanel p的LayoutManager設置為CardLayout p.setLayout(new CardLayout(); /新建兩個JPanel

23、 JPanel p1=new JPanel(); JPanel p2=new JPanel(); JLabel lb=new JLabel("first panel"); p1.add(lb); lb=new JLabel("second panel"); p2.add(lb); /將新建的兩個JPanel p1,p2添加到p中 p.add(p1,"frist"); p.add(p2,"second"); /設置默認顯示first所對應的JPanel p1 (CardLayout)p.getLayout().sho

24、w(p, "frist"); JButton cbtn=new JButton("Change"); cbtn.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e) /當按下Change按鈕時,顯示second 對應的JPanel p2 (CardLayout)p.getLayout().show(p, "second"); ); f.add(cbtn,BorderLayout.NORTH); f.add(p,BorderLa

25、yout.CENTER); f.setSize(400,150); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 運行結果:按下Change按鈕后的結果:6.BoxLayoutjava.lang.Object-javax.swing.BoxLayout以嵌套式盒子來管里容器的布局,通過將組件放入水平或垂直形盒子以多層嵌套的方式進行布局。構造函數:BoxLayout(Container target,int axis)建立一個水平或垂直的BoxLayout,BoxLayout提供兩個常數X_AXIS和Y

26、_AXIS來表示水平或垂直排列。說到BoxLayout,就不得不提到Box這個Container,Box這個Container默認的Layout為BoxLayout,而它只能使用這個Layout,否則編譯時會有Error產生。java.lang.Object-javax.swing.BoxBox有水平的和垂直的兩種形式。構造函數:Box(int axis)建立一個Box Container(容器),并指定組件的排列方式,通過使用BoxLayout提供的兩個常數X_AXIS和Y_AXIS來指定。方法:public static Box createHorizontalBox()構造一個水平排列的

27、Box組件。java view plaincopyprint?import javax.swing.Box; import javax.swing.JFrame; import javax.swing.JButton; public class BoxLayoutDemo public static void main(String args) JFrame f=new JFrame("BoxLayout"); /創建水平Box組件 Box box=Box.createHorizontalBox(); JButton btnA=new JButton("A"); JButton btnB=new JButton("B"); box.add(btnA); box.add(btnB); f.add(bo

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論