java自定义布局管理(二)

来源:互联网 发布:重庆时时彩组三软件 编辑:程序博客网 时间:2024/05/21 06:26

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中各个小件(meta)的实际位置的了。也就是说,当我们用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;

//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);

}

}

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孕晚期长妊娠纹怎么办 妊娠纹长出来了怎么办 没提前预约四维怎么办 ct检查冠脉钙化怎么办 残角子宫有内膜怎么办 怀孕了长痔疮了怎么办 人流后子宫内膜薄怎么办 多囊子宫内膜薄怎么办 6岁儿童不爱睡觉怎么办 初生儿嗓子哑了怎么办 6岁儿童睡眠不好怎么办 8岁儿童睡眠不好怎么办 1个月婴儿睡眠少怎么办 三岁宝宝心跳快怎么办 宝宝右心房稍大怎么办 胎儿右心房偏大怎么办 胎心监测波动小怎么办 怀孕6个月不想要了怎么办 高敏肌钙蛋白t高怎么办 龟头沟槽里长疙瘩怎么办 肝郁气滞的体质怎么办 手指甲长在肉里怎么办 甲床与指甲脱离怎么办 指甲往肉里面长怎么办 指甲和肉分离是怎么办 大脚趾指甲空了怎么办 脚趾甲长在肉里怎么办 脚趾甲又厚又硬怎么办 小孩子咳嗽有痰怎么办%3f 支气扩张咳血怎么办小 背部第8块脊椎疼怎么办 坐时间长了背疼怎么办 新生儿总哭怎么办吐奶 婴儿吃饱了还哭怎么办 宝宝喉咙哭哑了怎么办 婴儿哭哑了嗓子怎么办 婴儿胖子哭哑了怎么办 孩子声音哭哑了怎么办 喝咖啡手抖心慌怎么办 累了就心慌发抖怎么办 经常头晕心慌胸闷乏力怎么办