知识大全 Java布局管理器深入讨论

Posted 布局

篇首语:读书不趁早,后来徒悔懊。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 Java布局管理器深入讨论相关的知识,希望对你有一定的参考价值。

Java布局管理器深入讨论  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  我们都知道 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或者是他的一个子接口LayoutManager Interface 后者用于复杂的布局管理 例如GridCardLayout LayoutManager有五个方法需要实现 分别是    public void addLayoutComponent(String name Component p);   public void removeLayoutComponent(Component p);   public Dimension preferredLayoutSize(Container container);   public Dimension minimumLayoutSize(Container container);   public void layoutContainer(Container container);  第一个方法其实就是你在使用container add(String name ponent p);时调用的方法 例如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( );       public VFlowLayout(int i int j)      this hgap=i;      this vgap=j;          public void addLayoutComponent(String name Component p)            public void removeLayoutComponent(Component p)        public Dimension preferredLayoutSize(Container container)      synchronized(container getTreeLock())      Dimension dimension =new Dimension( );      int i=container getComponentCount();      for(int j= ;j       Component ponent = container getComponent(j);        if(ponent isVisible())          Dimension dimension =ponent getPreferredSize();          dimension width=Math max(dimension width dimension width);     if(j> )  dimension height+=vgap;   dimension height+=dimension height;                          Insets insets=container getInsets();        dimension height+=insets top+insets bottom+vgap* ;        dimension width+=insets left+insets right+hgap* ;        Dimension dimension=dimension ;     return dimension;  file://return(new Dimension( ));               public Dimension minimumLayoutSize(Container container)       synchronized(container getTreeLock())       Dimension dimension =new Dimension( );       int i=container getComponentCount();       for(int j= ;j      Component ponent = container getComponent(j);       if(ponent isVisible())        Dimension dimension =ponent getMinimumSize();        dimension width=Math max(dimension width dimension width);        if(j> )         dimension height+=vgap;         dimension height+=dimension height;                      Insets insets=container getInsets();       dimension height+=insets top+insets bottom+vgap* ;       dimension width+=insets left+insets right+hgap* ;       Dimension dimension=dimension ;       return dimension;                  public void layoutContainer(Container container)      synchronized(container getTreeLock())      Insets insets=container getInsets();      int vSpace=container getSize() height (insets top+insets bottom+vgap* );      int ponentCount=container getComponentCount();      int left=insets left+hgap;      int totalHeight= ;      int width= ;      int ponentStart= ;      for(int i= ;i      Component ponent=container getComponent(i);       if(ponent isVisible())        Dimension dimension=ponent getPreferredSize();    ponent setSize(dimension width dimension height);        if(totalHeight== || totalHeight+dimension height<=vSpace)         if(totalHeight> )    totalHeight+=vgap;          totalHeight+=dimension height;          width=Math max(width dimension width);         else          moveComponents(container insets top+vgap left width ponentStart i);      totalHeight= ;          left+=hgap+width;  width=dimension width;    ponentStart=i;                               moveComponents(container insets top+vgap left width ponentStart ponentCount);             private void moveComponents(Container container int top int left int width int          ponentStart int ponentEnd)       synchronized(container getTreeLock())        for(int i=ponentStart;i        Component ponent=container getComponent(i);         if(ponent isVisible())   ponent setLocation(left top);      top+=ponent 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);             大家可以试一下 cha138/Article/program/Java/hx/201311/26862

相关参考

知识大全 Java布局管理器使用方法探讨

Java布局管理器使用方法探讨  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  很多初学者在用Ja

知识大全 Java GUI中布局管理器的使用

JavaGUI中布局管理器的使用  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!lishixinzh

知识大全 深入讨论JAVA字节码加密技术(2)

深入讨论JAVA字节码加密技术(2)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  这个累加载器

知识大全 如何使用Java布局器

如何使用Java布局器?  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  很多初学者在用Java布

知识大全 深入讨论JAVA字节码加密技术(1)

深入讨论JAVA字节码加密技术(1)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  如果把的cl

知识大全 创建布局管理器

  来自于布局管理类LayoutManager  创建方法  a布局管理器必须实现布局管理器类的接口//  b覆蓋五个函数  //在布局管理器中增加或删除组件时调用下面两个函数  voidaddLay

知识大全 GroupLayout布局管理器实例

GroupLayout布局管理器实例  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  GroupL

知识大全 Java:Applet布局问题

Java:Applet布局问题  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  applet布局问

知识大全 C#开发的两个原则的深入讨论

C#开发的两个原则的深入讨论  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  使用属性避免将数据成

知识大全 对C#开发的两个基本原则的深入讨论

对C#开发的两个基本原则的深入讨论  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  使用属性避免将