设为首页 加入收藏
 
首 页 开源技术和代码信息库 软硬件共享平台 软件技术文献库 呼叫中心
关键字:      热门搜索:JAVA  .NET  JSP  PHP  WEB开发  
用户登录
用户名:
密 码:
验证码: 验证码,看不清楚请用鼠标点击此处!
编辑推荐 更多 >>  
推荐给好友
您的姓名
好友邮箱
   
 
当前位置:首页 > 软件技术文献 > 深入Java布局管理器

深入Java布局管理器

 作者:佚名  出处:IT专家网论坛 发表时间: 2009-03-12 打印 下载 收藏 推荐给好友
虽然Java在GUI方面由于Awt和Swing复杂的结构而不是特别成功,但GUI已成为程序发展的必然方向。本文详细讲解了Java GUI开发中的布局管理器的具体实现。

  Java的GUI界面定义是由awt类和swing类来完成的。它在布局管理上面采用了容器和布局管理分离的方案。也就是说,容器只管将其他小件放入其中,而不管这些小件是如何放置的。对于布局的管理交给专门的布局管理器类(LayoutManager)来完成。

  其实,Java在GUI方面应该是并不成功的。Awt类和swing类的结构非常复杂,加上充斥其间的子类继承和接口实现,使得要想掌握这两个类非常困难。这也是很多的Java程序员抱怨的事情,但GUI已经成了程序发展的方向,所以这里我们也得勉为其难了。

  现在我们来看Java中布局管理器的具体实现。我们前面说过,Java中的容器类(Container),它们只管加入小件(Meta),也就是说,它只使用自己的add()方法向自己内部加入小件。同时他记录这些加入其内部的小件的个数,可以通过container.getComponentCount()方法类获得小件的数目,通过container.getComponent(i)来获得相应小件的句柄。然后LayoutManager类就可以通过这些信息来实际布局其中的小件了。

  Java已经为我们提供了几个常用的布局管理器类,例如:BorderLayout、FlowLayout、GridBagLayout等等。但在实际的布局上,我们还是会有其他的需要。我在不久前的一个问题中曾经要一个垂直的流式布局,我称之为VflowLayout,其实BoxLayout和GridBagLayout可以完成类似的工作,但前者是swing类的成员,我的客户端是一个applet,不能使用,而后者必须在类生成的时候指定列数,而失去了灵活性,所以我决定重写一个自己的布局管理器来实现。经过分析,所有的LayoutManager都要实现一个接口,就是LayoutManager Inerface或者是他的一个子接口LayoutManager2 Interface,后者用于复杂的布局管理,例如GridCardLayout。LayoutManager有五个方法需要实现,分别是:

  1、public void addLayoutComponent(String name, Component comp);

  2、public void removeLayoutComponent(Component comp);

  3、public Dimension preferredLayoutSize(Container container);

  4、public Dimension minimumLayoutSize(Container container);

  5、public void layoutContainer(Container container);

  第一个方法其实就是你在使用container.add(String name,component comp);时调用的方法,例如BorderLayout为布局管理器时。但在FlowLayout中由于没有其他的附加信息,所以不需要填充这个方法。相应的第二个方法也就不需要填充了。真正核心的方法是第三个和第五个方法,前者是最终确定Container有多大的,而后者就是决定Container中各个小件的实际位置的了。也就是说,当我们用container.setLayout(LayoutManager)后,再加入小件后,最后系统做的工作其实是LayoutManager. layoutContainer(container);和container.setSize(LayoutManager. PreferredLayoutSize(container));。

  下面是我的新类:VflowLayout

  

    package render_account;

  import java.awt.*;

  import java.io.*;

  public class VFlowLayout implements LayoutManager,Serializable{

  int hgap;

  int vgap;

  public VFlowLayout(){

  this(5,5);

  }

  public VFlowLayout(int i,int j){

  this.hgap=i;

  this.vgap=j;

  }

  public void addLayoutComponent(String name, Component comp){

  }

  public void removeLayoutComponent(Component comp){

  }

  public Dimension preferredLayoutSize(Container container){

  synchronized(container.getTreeLock()){

  Dimension dimension1=new Dimension(0,0);

  int i=container.getComponentCount();

  for(int j=0;j Component component = container.getComponent(j);

  if(component.isVisible()){

  Dimension dimension2=component.getPreferredSize();

  dimension1.width=Math.max(dimension1.width,dimension2.width);

  if(j>0)

  dimension1.height+=vgap;

  dimension1.height+=dimension2.height;

  }

  }

  Insets insets=container.getInsets();

  dimension1.height+=insets.top+insets.bottom+vgap*2;

  dimension1.width+=insets.left+insets.right+hgap*2;

  Dimension dimension=dimension1;

  return dimension;

  file://return(new Dimension(50,200));

  }

  }

  public Dimension minimumLayoutSize(Container container){

  synchronized(container.getTreeLock()){

  Dimension dimension1=new Dimension(0,0);

  int i=container.getComponentCount();

  for(int j=0;j  Component component = container.getComponent(j);

  if(component.isVisible()){

  Dimension dimension2=component.getMinimumSize();

  dimension1.width=Math.max(dimension1.width,dimension2.width);

  if(j>0)

  dimension1.height+=vgap;

  dimension1.height+=dimension2.height;

  }

  }

  Insets insets=container.getInsets();

  dimension1.height+=insets.top+insets.bottom+vgap*2;

  dimension1.width+=insets.left+insets.right+hgap*2;

  Dimension dimension=dimension1;

  return dimension;

  }

  }

  public void layoutContainer(Container container){

  synchronized(container.getTreeLock()){

  Insets insets=container.getInsets();

  int vSpace=container.getSize().height-(insets.top+insets.bottom+vgap*2);

  int componentCount=container.getComponentCount();

  int left=insets.left+hgap;

  int totalHeight=0;

  int width=0;

  int componentStart=0;

  for(int i=0;i  Component component=container.getComponent(i);

  if(component.isVisible()){

  Dimension dimension=component.getPreferredSize();

  component.setSize(dimension.width,dimension.height);

  if(totalHeight==0 || totalHeight+dimension.height<=vSpace){

  if(totalHeight>0)

  totalHeight+=vgap;

  totalHeight+=dimension.height;

  width=Math.max(width,dimension.width);

  }else{

  moveComponents(container,insets.top+vgap,left,width,componentStart,i);

  totalHeight=0;

  left+=hgap+width;

  width=dimension.width;

  componentStart=i;

  }

  }

  }

  moveComponents(container,insets.top+vgap,left,width,componentStart,componentCount);

  }

  }

  private void moveComponents(Container container,int top,int left,int width,int  componentStart,int componentEnd){

  synchronized(container.getTreeLock()){

  for(int i=componentStart;i  Component component=container.getComponent(i);

  if(component.isVisible()){

  component.setLocation(left,top);

  top+=component.getPreferredSize().height+vgap;

  }

  }

  }

  }

  public void setHgap(int i){

  this.hgap=i;

  }

  public void setVgap(int i){

  this.vgap=i;

  }

  public int getHgap(){

  return(this.hgap);

  }

  public int getVgap(){

  return(this.vgap);

  }

  }

对本文的评价
太差! (1)
需提高 (2)
一般 (3)
好文章 (4)
真棒!(5)
评论
发表评论人笔名:
其他用户评论
评论发表人笔名: Kristen Gallegos  发表时间: 2010-09-02  
评  论  内  容: [url=http://mbjibw.com/272]Komp Svoimi Rukami[/url]
[url=http://mbjibw.com/1h6]Rabota Doma Na Kompyutere[/url]
[url=http://mbjibw.com/1jh]Slova Pesni My Vdvoem[/url]
[url=http://mbjibw.com/ek]Dms V Nizhnem Novgorode[/url]
[url=http://mbjibw.com/1hh]Otdyx Na Novogodnie Prazdniki[/url]
[url=http://mbjibw.com/ie]Dlya Skachat Patch Wow 2.4.3[/url]
[url=http://mbjibw.com/21g]Zarabotalo Proxozhdenie Igry[/url]
[url=http://mbjibw.com/17r]Jimm Xattab Skachat[/url]
[url=http://mbjibw.com/1v2]Dzhemete Chastnyj Sektor[/url]
[url=http://mbjibw.com/1wl]Resources Post New Topic[/url]
[url=http://mbjibw.com/1u2]18 Video Add Message[/url]
[url=http://mbjibw.com/tt]Telefon Dom Kino[/url]
[url=http://mbjibw.com/w4]Nokia Centr Ekaterinburg[/url]
[url=http://mbjibw.com/gp]Stixi K Godu Krysy[/url]
[url=http://mbjibw.com/13l]Kak Vzlomat Icq Parol[/url]
[url=http://mbjibw.com/v1]Sony Ericsson K510i B U[/url]
[url=http://mbjibw.com/os]Novosti Futbola Rossii[/url]
[url=http://mbjibw.com/153]Kak Sozdat Sajt Na Narode[/url]
[url=http://mbjibw.com/qj]Xottabych Smotret Onlajn Besplatno[/url]
[url=http://mbjibw.com/140]Magazin Chasov V Ufe[/url]

深入 
评论发表人笔名: Dexter Rollins  发表时间: 2010-09-02  
评  论  内  容: http://mbjibw.com/1ta
http://mbjibw.com/wr
http://mbjibw.com/1du
http://mbjibw.com/ze
http://mbjibw.com/22l
http://mbjibw.com/qe
http://mbjibw.com/1y4
http://mbjibw.com/1ek
http://mbjibw.com/1ih
http://mbjibw.com/1yi
http://mbjibw.com/d
http://mbjibw.com/u4
http://mbjibw.com/1u3
http://mbjibw.com/1sv
http://mbjibw.com/1yj
http://mbjibw.com/ny
http://mbjibw.com/1lj
http://mbjibw.com/1yv
http://mbjibw.com/1xo
http://mbjibw.com/3e

深入 
评论发表人笔名: Shanda Mccall  发表时间: 2010-09-01  
评  论  内  容: http://osepzg.com/c8
http://osepzg.com/4ep
http://osepzg.com/8b
http://osepzg.com/53f
http://osepzg.com/1zf
http://osepzg.com/136
http://osepzg.com/3k4
http://osepzg.com/18t
http://osepzg.com/56
http://osepzg.com/25m
http://osepzg.com/2t3
http://osepzg.com/2je
http://osepzg.com/g7
http://osepzg.com/25w
http://osepzg.com/2f8
http://osepzg.com/12n
http://osepzg.com/2lo
http://osepzg.com/2wl
http://osepzg.com/4es
http://osepzg.com/fl

深入 
评论发表人笔名: Marlon Copeland  发表时间: 2010-09-01  
评  论  内  容: http://osepzg.com/1fb
http://osepzg.com/457
http://osepzg.com/5ft
http://osepzg.com/2ps
http://osepzg.com/5jb
http://osepzg.com/3m6
http://osepzg.com/10d
http://osepzg.com/4go
http://osepzg.com/4ak
http://osepzg.com/1ha
http://osepzg.com/1cn
http://osepzg.com/1zp
http://osepzg.com/2uz
http://osepzg.com/3ui
http://osepzg.com/496
http://osepzg.com/1fc
http://osepzg.com/30g
http://osepzg.com/32e
http://osepzg.com/4mj
http://osepzg.com/516

深入 
评论发表人笔名: Lee Rice  发表时间: 2010-08-31  
评  论  内  容: http://osepzg.com/397
http://osepzg.com/4lg
http://osepzg.com/4re
http://osepzg.com/hs
http://osepzg.com/263
http://osepzg.com/35
http://osepzg.com/p6
http://osepzg.com/2fe
http://osepzg.com/4s2
http://osepzg.com/3kg
http://osepzg.com/3un
http://osepzg.com/1ba
http://osepzg.com/2i8
http://osepzg.com/1o6
http://osepzg.com/2jq
http://osepzg.com/2h4
http://osepzg.com/23c
http://osepzg.com/3r8
http://osepzg.com/2an
http://osepzg.com/2lm
http://osepzg.com/s
http://osepzg.com/dk
http://osepzg.com/1y7
http://osepzg.com/5qp
http://osepzg.com/2d0
http://osepzg.com/3s5
http://osepzg.com/45q
http://osepzg.com/51y
http://osepzg.com/st
http://osepzg.com/4kp

深入 
评论发表人笔名: Bryan Austin  发表时间: 2010-08-31  
评  论  内  容: http://osepzg.com/308
http://osepzg.com/4aq
http://osepzg.com/5sg
http://osepzg.com/42d
http://osepzg.com/4gv
http://osepzg.com/4nw
http://osepzg.com/3le
http://osepzg.com/65
http://osepzg.com/5fu
http://osepzg.com/244
http://osepzg.com/4pr
http://osepzg.com/1z7
http://osepzg.com/2qy
http://osepzg.com/3a2
http://osepzg.com/375
http://osepzg.com/5pi
http://osepzg.com/yw
http://osepzg.com/1k4
http://osepzg.com/vx
http://osepzg.com/5us
http://osepzg.com/5t1
http://osepzg.com/326
http://osepzg.com/8k
http://osepzg.com/u9
http://osepzg.com/344
http://osepzg.com/4bo
http://osepzg.com/9m
http://osepzg.com/7s
http://osepzg.com/4w1
http://osepzg.com/2f6

深入 
评论发表人笔名: Melissa Cooley  发表时间: 2010-08-31  
评  论  内  容: [url=http://osepzg.com/2ml]Shkaf Kupe V Spb[/url]
[url=http://osepzg.com/1ds]Mezhkomnatny`e Dveri -ujczkcz[/url]
[url=http://osepzg.com/2a0]Prikaz 37 Ot 29.01.07[/url]
[url=http://osepzg.com/3pt]Predstavitel` Rossii Na Evrovidenii[/url]
[url=http://osepzg.com/58v]Rusifikator Dlya Adobe Premiere Pro Cs3[/url]
[url=http://osepzg.com/2f1]Stroka Dos V Windows[/url]
[url=http://osepzg.com/3h]Shalimov Viktor Ivanovich[/url]
[url=http://osepzg.com/4py]Rabota V G. Irkutske Na 10 Marta[/url]
[url=http://osepzg.com/4io]Ukrasheniya Pirsing[/url]
[url=http://osepzg.com/215]Mebel` Myagkaya Vo Vladivostoke[/url]
[url=http://osepzg.com/489]Igra Vojna Plemen[/url]
[url=http://osepzg.com/5dt]Igra Naruto Na Privet.ru[/url]
[url=http://osepzg.com/8t]Skachat` Trejner Dlya Sa Mp 0.2.2[/url]
[url=http://osepzg.com/2g]Interaktivny`e Karty` Moskvy`[/url]
[url=http://osepzg.com/503]Postroenie Struktury` Kompanii[/url]
[url=http://osepzg.com/10e]Rajon Ae`roport G Moskva[/url]
[url=http://osepzg.com/2bw]Kalendar` Rabochix Dnej Na 2007[/url]
[url=http://osepzg.com/2r8]1383 Shkola[/url]
[url=http://osepzg.com/a5]Skachat` Besplatno Fil`m Svetlyachki V Sadu[/url]
[url=http://osepzg.com/s0]Programma Peredach S 5 Po 11 Maya[/url]
[url=http://osepzg.com/5w8]Prodayu Xonda Akkord 2006[/url]
[url=http://osepzg.com/487]Crack Dlya Alcohol[/url]
[url=http://osepzg.com/1vu]Auto Add Message[/url]
[url=http://osepzg.com/2rh]Remont E`lektrosteklopod``emnika Vaz 2109[/url]
[url=http://osepzg.com/]Samoubijstvo Knigi Skachat`[/url]
[url=http://osepzg.com/4dj]Internet Magazin Citizen[/url]
[url=http://osepzg.com/40e]6 Bol`nicza[/url]
[url=http://osepzg.com/bs]Podruga S Dnem Rozhdeniya[/url]
[url=http://osepzg.com/522]Novy`j Zhurnal[/url]
[url=http://osepzg.com/1xl]Skachat` Kasperskij Dlya Se750i[/url]

深入 
评论发表人笔名: Rene Bradley  发表时间: 2010-08-22  
评  论  内  容: [url=http://2cud53ui3an8cbdn.com/]coxdl9q34d3xaspw[/url]
[link=http://k1004zprvookm73d.com/]hd5ytt2rdo2wix33[/link]
<a href=http://2ttwo18gyzuzrh73.com/>9nd7ljtnxo49qjq6</a>
http://1fwj7zxwbtap9a51.com/

深入 
评论发表人笔名: Vince Mcfadden  发表时间: 2010-08-22  
评  论  内  容: [url=http://eidocf.com/1mq]Pinch Nerve Pain Relif[/url]
[url=http://eidocf.com/ot]Doctor Gyno Exam Free Pics[/url]
[url=http://eidocf.com/2gp]Fleece Baby Blanket No Sew Ontario[/url]
[url=http://eidocf.com/q2]1000 Calorie Vegetarian Plan[/url]
[url=http://eidocf.com/217]Gta Sa Cheats For Codebreaker[/url]
[url=http://eidocf.com/30]Corn Plant Parts[/url]
[url=http://eidocf.com/1yw]Zoids Online Games[/url]
[url=http://eidocf.com/10r]80th Birthday Invitation Wording Ideas[/url]
[url=http://eidocf.com/1r4]Short Female Hairstyles Free Images[/url]
[url=http://eidocf.com/2a9]Create Free Account For Guild Wars[/url]
[url=http://eidocf.com/1kf]Military Jeep Sale[/url]
[url=http://eidocf.com/11l]Apache Indian Weapon Pictures[/url]
[url=http://eidocf.com/1hy]What Were Ancient Greece Family Life[/url]
[url=http://eidocf.com/a0]Hiv Rash Picture[/url]
[url=http://eidocf.com/3i]Printable Bible Quizzes[/url]
[url=http://eidocf.com/2is]Short Funeral Poem[/url]
[url=http://eidocf.com/44]Science Fair 6th Grade Tri Board Ideas[/url]
[url=http://eidocf.com/1n2]Things To Say On Valentines[/url]
[url=http://eidocf.com/289]Vocabulary Math Games[/url]
[url=http://eidocf.com/ow]Free Quilt Patterns Paper Piece[/url]
[url=http://eidocf.com/1ns]Free Restaurant Menu Design Software[/url]
[url=http://eidocf.com/1gw]Writing Letter For Continuance[/url]
[url=http://eidocf.com/1fp]Serial Key Generator Halo[/url]
[url=http://eidocf.com/1j7]Tribes 2 Cd Key Generator[/url]
[url=http://eidocf.com/e0]Chinese Fan Pattern[/url]
[url=http://eidocf.com/1ag]Honda Cbr 600f 1996[/url]
[url=http://eidocf.com/1qr]Toolmaker Jobs Canada[/url]
[url=http://eidocf.com/17v]Dodge Challenger 1969 1973 Sale[/url]
[url=http://eidocf.com/13w]What Removes Crayon From Clothing[/url]
[url=http://eidocf.com/1vg]Porsche 928 For Sale In Florida[/url]

深入 
评论发表人笔名: Robin Chambers  发表时间: 2010-08-22  
评  论  内  容: http://eidocf.com/2ho
http://eidocf.com/1dy
http://eidocf.com/wj
http://eidocf.com/1ep
http://eidocf.com/ut
http://eidocf.com/1sw
http://eidocf.com/1fd
http://eidocf.com/p1
http://eidocf.com/s
http://eidocf.com/t2
http://eidocf.com/lo
http://eidocf.com/19l
http://eidocf.com/qj
http://eidocf.com/4c
http://eidocf.com/2cd
http://eidocf.com/mw
http://eidocf.com/29x
http://eidocf.com/16q
http://eidocf.com/1pb
http://eidocf.com/1ur
http://eidocf.com/1f6
http://eidocf.com/28c
http://eidocf.com/15q
http://eidocf.com/2ad
http://eidocf.com/29v
http://eidocf.com/2id
http://eidocf.com/1x1
http://eidocf.com/2aa
http://eidocf.com/2c7
http://eidocf.com/17o

深入 
合肥服务外包公共服务平台网站群:合肥服务外包网   公共信息平台  公共技术平台   公共培训平台
合肥市外经贸局地址:合肥市政务新区B座9-10层 邮编:230022 监督电话:3538679 传真:3538677
Email:hffetc@mail.hf.ah.cn 合肥市外经贸局行政服务中心窗口电话:3537035、3537377
版权所有:合肥市对外贸易经济合作局 皖ICP备06004470号