java swing 垂直…

  1. 最近写一个java UI,需要用到垂直流布局管理器,要求该管理器能够实现内部组件的宽度自适应。看了swing提供的5个布局管理器,尝试的实现效果都不理想,看来只能自己搞一个了,好在网上已有实现,其测试效果如下图:
  2. 图一 垂直流布局管理器实现效果



    具体代码如下:


  3. import java.awt.Component;  
  4. import java.awt.Container;  
  5. import java.awt.Dimension;  
  6. import java.awt.FlowLayout;  
  7. import java.awt.Insets;  
  8.   
  9. import javax.swing.JButton;  
  10. import javax.swing.JFrame;  
  11.   
  12.   
  13. public class VFlowLayout extends FlowLayout  
  14.  
  15.       
  16.     private static final long serialVersionUID 1L;  
  17.   
  18.       
  19.     public static final int TOP 0 
  20.   
  21.       
  22.     public static final int MIDDLE 1 
  23.   
  24.       
  25.     public static final int BOTTOM 2 
  26.   
  27.     int hgap;  
  28.     int vgap;  
  29.     boolean hfill;  
  30.     boolean vfill;  
  31.   
  32.     public static void main(String[] args)  
  33.      
  34.         System.out.println("Just for test ...");  
  35.         JFrame frame new JFrame();  
  36.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  37.         frame.setBounds(00600600);  
  38.         frame.setLayout(new VFlowLayout());  
  39.           
  40.         int 0 
  41.         frame.add(new JButton(String.valueOf(i++)));  
  42.         frame.add(new JButton(String.valueOf(i++)));  
  43.         frame.add(new JButton(String.valueOf(i++)));  
  44.         frame.add(new JButton(String.valueOf(i++)));  
  45.         frame.add(new JButton(String.valueOf(i++)));  
  46.         frame.add(new JButton(String.valueOf(i++)));  
  47.         frame.add(new JButton(String.valueOf(i++)));  
  48.         frame.add(new JButton(String.valueOf(i++)));  
  49.         frame.add(new JButton(String.valueOf(i++)));  
  50.         frame.add(new JButton(String.valueOf(i++)));  
  51.         frame.add(new JButton(String.valueOf(i++)));  
  52.           
  53.         frame.setVisible(true);  
  54.      
  55.       
  56.       
  57.     public VFlowLayout()  
  58.      
  59.         this(TOP, 55truefalse);  
  60.      
  61.   
  62.       
  63.     public VFlowLayout(boolean hfill, boolean vfill)  
  64.      
  65.         this(TOP, 55hfill, vfill);  
  66.      
  67.   
  68.       
  69.     public VFlowLayout(int align)  
  70.      
  71.         this(align, 55truefalse);  
  72.      
  73.   
  74.       
  75.     public VFlowLayout(int align, boolean hfill, boolean vfill)  
  76.      
  77.         this(align, 55hfill, vfill);  
  78.      
  79.   
  80.       
  81.     public VFlowLayout(int align, int hgap, int vgap, boolean hfill, boolean vfill)  
  82.      
  83.         setAlignment(align);  
  84.         this.hgap hgap;  
  85.         this.vgap vgap;  
  86.         this.hfill hfill;  
  87.         this.vfill vfill;  
  88.      
  89.   
  90.       
  91.     public Dimension preferredLayoutSize(Container target)  
  92.      
  93.         Dimension tarsiz new Dimension(00);  
  94.   
  95.         for (int 0target.getComponentCount(); i++)  
  96.          
  97.             Component target.getComponent(i);  
  98.               
  99.             if (m.isVisible())  
  100.              
  101.                 Dimension m.getPreferredSize();  
  102.                 tarsiz.width Math.max(tarsiz.width, d.width);  
  103.                   
  104.                 if (i 0 
  105.                  
  106.                     tarsiz.height += hgap;  
  107.                  
  108.                   
  109.                 tarsiz.height += d.height;  
  110.              
  111.          
  112.           
  113.         Insets insets target.getInsets();  
  114.         tarsiz.width += insets.left insets.right hgap 2 
  115.         tarsiz.height += insets.top insets.bottom vgap 2 
  116.           
  117.         return tarsiz;  
  118.      
  119.   
  120.       
  121.     public Dimension minimumLayoutSize(Container target)  
  122.      
  123.         Dimension tarsiz new Dimension(00);  
  124.   
  125.         for (int 0target.getComponentCount(); i++)  
  126.          
  127.             Component target.getComponent(i);  
  128.               
  129.             if (m.isVisible())  
  130.              
  131.                 Dimension m.getMinimumSize();  
  132.                 tarsiz.width Math.max(tarsiz.width, d.width);  
  133.                   
  134.                 if (i 0  
  135.                  
  136.                     tarsiz.height += vgap;  
  137.                  
  138.                   
  139.                 tarsiz.height += d.height;  
  140.              
  141.          
  142.           
  143.         Insets insets target.getInsets();  
  144.         tarsiz.width += insets.left insets.right hgap 2 
  145.         tarsiz.height += insets.top insets.bottom vgap 2 
  146.           
  147.         return tarsiz;  
  148.      
  149.   
  150.       
  151.     public void setVerticalFill(boolean vfill)  
  152.      
  153.         this.vfill vfill;  
  154.      
  155.   
  156.       
  157.     public boolean getVerticalFill()  
  158.      
  159.         return vfill;  
  160.      
  161.   
  162.       
  163.     public void setHorizontalFill(boolean hfill)  
  164.      
  165.         this.hfill hfill;  
  166.      
  167.   
  168.       
  169.     public boolean getHorizontalFill()  
  170.      
  171.         return hfill;  
  172.      
  173.   
  174.       
  175.     private void placethem(Container target, int x, int y, int width, int height, int first, int last)  
  176.      
  177.         int align getAlignment();  
  178.           
  179.         if (align == MIDDLE)  
  180.          
  181.             += height 2 
  182.          
  183.           
  184.         if (align == BOTTOM)  
  185.          
  186.             += height;  
  187.          
  188.           
  189.         for (int first; last; i++)  
  190.          
  191.             Component target.getComponent(i);  
  192.             Dimension md m.getSize();  
  193.               
  194.             if (m.isVisible())  
  195.              
  196.                 int px (width md.width) 2 
  197.                 m.setLocation(px, y);  
  198.                 += vgap md.height;  
  199.              
  200.          
  201.      
  202.   
  203.       
  204.     public void layoutContainer(Container target)  
  205.      
  206.         Insets insets target.getInsets();  
  207.         int maxheight target.getSize().height (insets.top insets.bottom vgap 2);  
  208.         int maxwidth target.getSize().width (insets.left insets.right hgap 2);  
  209.         int numcomp target.getComponentCount();  
  210.         int insets.left hgap, 0 
  211.         int colw 0start 0 
  212.   
  213.         for (int 0numcomp; i++)  
  214.          
  215.             Component target.getComponent(i);  
  216.               
  217.             if (m.isVisible())   
  218.              
  219.                 Dimension m.getPreferredSize();  
  220.                   
  221.                 // fit last component to remaining height  
  222.                 if ((this.vfill) && (i == (numcomp 1)))   
  223.                  
  224.                     d.height Math.max((maxheight y), m.getPreferredSize().height);  
  225.                  
  226.   
  227.                 // fit component size to container width  
  228.                 if (this.hfill)  
  229.                  
  230.                     m.setSize(maxwidth, d.height);  
  231.                     d.width maxwidth;  
  232.                   
  233.                 else  
  234.                  
  235.                     m.setSize(d.width, d.height);  
  236.                  
  237.   
  238.                 if (y d.height maxheight)  
  239.                  
  240.                     placethem(target, x, insets.top vgap, colw, maxheight y, start, i);  
  241.                     d.height;  
  242.                     += hgap colw;  
  243.                     colw d.width;  
  244.                     start i;  
  245.                   
  246.                 else  
  247.                  
  248.                     if (y 0 
  249.                      
  250.                         += vgap;  
  251.                      
  252.                       
  253.                     += d.height;  
  254.                     colw Math.max(colw, d.width);  
  255.                  
  256.              
  257.          
  258.           
  259.         placethem(target, x, insets.top vgap, colw, maxheight y, start, numcomp);  
  260.      
  261. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值