GUI Panel 容器以及布局管理器,guipanel

來源:互聯網
上載者:User

GUI Panel 容器以及布局管理器,guipanel

一、Panel是AWT中的另一個典型的容器,它代表不能獨立存在、必須放在其他容器中使用。

1、可作為容器來盛裝其他組件,為放置組件提供空間。

2、不能單獨存在,必須放置到其他容器當中。

3、預設使用FlowLayout作為布局管理器。

 1 public class PanelTest extends Frame 2 { 3     public static void main(String[] args) 4     { 5         Frame f = new Frame(); 6         Panel p = new Panel(); 7         p.add(new TextField(20)); 8         p.add(new Button("百度")); 9         f.add(p);10         f.setBounds(50, 50, 300, 300);11         f.setVisible(true);12     }13 }

 

二、FlowLayout布局管理器

在FlowLayout布局管理器中,組件像流水一樣向某方向流動(排列),遇到障礙(邊界)就折回,重頭開始排列。

三個構造器:

FlowLayout:使用預設的對齊及預設的垂直間距、水平間距建立FlowLayout布局管理器。

FlowLayout(int align):使用指定的對齊及預設的垂直間距、水平間距建立FlowLayout布局管理器。

FlowLayout(int align,int hgap,int vgap):使用指定的對齊及指定的垂直間距、水平間距建立FlowLayout布局管理器。

 1 public class FlowLayoutTest 2 { 3     public static void main(String[] args) 4     { 5         Frame f = new Frame(); 6         f.setLayout(new FlowLayout(FlowLayout.LEFT,20,5)); 7         Button b1 = new Button("a1"); 8         Button b2 = new Button("a2"); 9         Button b3 = new Button("a3");10         f.add(b1);11         f.add(b2);12         f.add(b3);13         f.pack();14         f.setVisible(true);15         16     }17 }

 

三、BorderLayout布局管理器

BorderLayout將容器分為EAST(東),SOUTH(南),WEST(西),NORTH(北),CENTER(中)五個地區,普通組件可以放在這5個地區中的任意一個。

構造器:

BorderLayout():使用預設的水平間距、垂直間距建立BorderLayout布局管理器

BorderLayout(int hgap,int vgap):使用指定的水平間距、垂直間距建立BorderLayout布局管理器。

 1 public class BorderLayoutTest extends Frame 2 { 3     public static void main(String[] args) 4     { 5         Frame f = new Frame(); 6         f.setLayout(new BorderLayout(30, 5)); 7         f.add(new Button("南"),BorderLayout.SOUTH); 8         f.add(new Button("北"),BorderLayout.NORTH); 9         f.add(new Button("中"));10         f.add(new Button("東"),BorderLayout.EAST);11         f.add(new Button("西"),BorderLayout.WEST);12         13         f.pack();14         f.setVisible(true);15     }16 }

四、GridLayout布局管理器

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.