当前位置:天才代写 > tutorial > JAVA 教程 > Java机关打点器的详细实现

Java机关打点器的详细实现

2017-11-11 08:00 星期六 所属: JAVA 教程 浏览:272

副标题#e#

我们都知道,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);


#p#副标题#e#

第一个要领其实就是你在利用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);< BR>      }
    }
    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);
     }
    }

各人可以试一下。

 

    关键字:

天才代写-代写联系方式