加载中…
个人资料
马小李
马小李
  • 博客等级:
  • 博客积分:0
  • 博客访问:8,847
  • 关注人气:36
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

JAVA布局管理器小结(八)自定义布局管理器

(2014-09-11 15:54:19)
标签:

it

java

分类: Java/Android

Java除了预定的布局管理器,以及不使用布局管理器,还为绝世高手提供了自定义的布局管理器。Java为自定义布局管理器提供了接口LayoutManagerLayoutManager2两中接口,以实现自定义的布局管理器。其中LayoutManager2继承了LayoutManager

Java预定义的六种布局管理器就是对两种接口的实现,查看源码,这六种布局管理器的实现分布如下:

http://s12/mw690/001ZkYorty6LXtB767p9b&690

 

LayoutManager接口需要实现下列五个方法:

//将组件comp添加到布局

//并与name关联

void addLayoutComponent(String name, Component comp)

//布置指定容器parent

void layoutContainer(Container parent)

//返回parent容器的最小的大小

Dimension minimumLayoutSize(Container parent)

//返回parent容器的首选大小

Dimension preferredLayoutSize(Container parent)

//从布局移除指定组件comp

void removeLayoutComponent(Component comp)

这里有些部分官方文档以及网上的资料有限,一般给出的例子就实现了layoutContainer方法,其他的实现确实不是很清楚,所以让我们来看看接口的代码,我将部分关键的英文注释翻译成了中文,并给出的自己的疑问:

package java.awt;

 

 

public interface LayoutManager {

   

    void addLayoutComponent(String name, Component comp);

 

   

    void removeLayoutComponent(Component comp);

 

   

    Dimension preferredLayoutSize(Container parent);

 

   

    Dimension minimumLayoutSize(Container parent);

 

   

    void layoutContainer(Container parent);

}

其实两个预定义的布局管理器都没有用到上述的addLayoutComponent(String name, Component comp);方法,而是空在了那里,所以在设计自己的布局管理器的时候,如果不要用到这个方法,或者根本不知道这个方法是做什么的可以直接忽略,其实这个方法在BorderLayout里面使用到了,我们使用add( component, “Center” )就是使用addLayoutComponent(String name, Component comp)的方式实现的。其具体调用时,我们在使用add函数时,其对应的是Container中的add方法(原型:add(Component comp, Object constraints)),然后由add方法调用addImpl方法(原型:addImpl(Component comp, Object constraints, int index)),最终添加的Component的对象,被放在了Component的列表的最后。而自动调用的doLayout方法最终显式调用了layoutContainer

对于LayoutManager2接口而言,需要实现下列五种方法:

//使用指定约束对象,将指定组件添加到布局

//想想GridBagLayout设置布局的时候是如何做的

void addLayoutComponent(Component comp, Object constraints)

//返回沿X轴对其的方式

float getLayoutAlignmentX(Container target)

//返回沿Y轴对其的方式

float getLayoutAlignmentY(Container target)

//使布局失效,指示如果布局管理器缓存了信息,则应该将其丢弃

void invalidateLayout(Container target)

//给定指定容器的组件,计算该容器的最大大小维数

Dimension maximumLayoutSize(Container target)

还是来看看这个接口的源码:

package java.awt;

 

 

public interface LayoutManager2 extends LayoutManager {

 

   

    void addLayoutComponent(Component comp, Object constraints);

 

   

    public Dimension maximumLayoutSize(Container target);

 

   

    public float getLayoutAlignmentX(Container target);

 

   

    public float getLayoutAlignmentY(Container target);

 

   

    public void invalidateLayout(Container target);

 

}

 

下面的例子实现LayoutManager接口,以自定义一个布局管理器。用来实现类似GridBagLayout中示例的布局,横向依次编号,顺序添加,示例代码如下:

import java.awt.*;

import java.applet.Applet;

 

public class test extends Applet{

        

        

         private Button b1, b2, b3, b4, b5, b6;

        

         public void init(){

                  

                   b1 = new Button( "NO.1" );

                   b2 = new Button( "NO.2" );

                   b3 = new Button( "NO.3" );

                   b4 = new Button( "NO.4" );

                   b5 = new Button( "NO.5" );

                   b6 = new Button( "NO.6" );

                  

                   //设置布局管理器为自定义

                   setLayout( new testLayout() );

                   //向容器添加组件

                   add( b1 );

                   add( b2 );

                   add( b3 );

                   add( b4 );

                   add( b5 );

                   add( b6 );

                  

         }

        

}

class testLayout implements LayoutManager{

        

  public void addLayoutComponent( String name, Component comp ){}

        

  public void layoutContainer( Container parent ){

          

           int numberOfComponent = parent.getComponentCount();

           //只接受六个组件的布局

           if( numberOfComponent > 6 )

                     numberOfComponent = 6;

          

           int base = 100;

           for( int i = 0; i < numberOfComponent; i++ ){

                     Component comp = parent.getComponent( i );

                    

                     switch( i ){

                     case 0:

                             comp.setBounds( 0, 0, base, 3 * base );

                             break;

                     case 1:

                              comp.setBounds( base, 0, base,  base );

                              break;

                     case 2:

                              comp.setBounds( 2 * base, 0, 3* base, base );

                              break;

                     case 3:

                              comp.setBounds( base, base, 4 * base, base );

                              break;

                     case 4:

                              comp.setBounds( base, 2 * base, 3 * base, base );

                              break;

                     case 5:

                              comp.setBounds( 4 * base, 2 * base, base, base );

                              break;

                     }//end switch

           }//end for i

          

  }//end addLayoutComponent

        

         public Dimension minimumLayoutSize( Container parent ){

                   return new Dimension( 0, 0 );

         }//end minimumLayoutSize

        

         public Dimension preferredLayoutSize( Container parent ){

                   return minimumLayoutSize( parent );

         }//end preferredLayoutSize

        

         public void removeLayoutComponent( Component comp ){

                  

         }//end removeLayoutComponent

        

}//end class

 

示例代码运行效果如下:

http://s13/mw690/001ZkYorty6LXtDdrFW3c&690
       至此,已经整理完成了我现在已知的Java布局管理器,欢迎大家与我共同探讨学习。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有