http://www.cnblogs.com/TLightSky/p/3482454.html

——————————————————————————————————————————————————————————————————————————————

原来的WrapLayout有点小bug,会引起抖动,稍微改了一下,现在比较好用了

  1. package com.miui.theme.tool.gui;
  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.JScrollPane;
  10. import javax.swing.SwingUtilities;
  11.  
  12. /**
  13. * FlowLayout subclass that fully supports wrapping of components.
  14. */
  15. public class WrapLayout extends FlowLayout {
  16. private Dimension preferredLayoutSize;
  17.  
  18. /**
  19. * Constructs a new <code>WrapLayout</code> with a left alignment and a
  20. * default 5-unit horizontal and vertical gap.
  21. */
  22. public WrapLayout() {
  23. super();
  24. }
  25.  
  26. /**
  27. * Constructs a new <code>FlowLayout</code> with the specified alignment and
  28. * a default 5-unit horizontal and vertical gap. The value of the alignment
  29. * argument must be one of <code>WrapLayout</code>, <code>WrapLayout</code>,
  30. * or <code>WrapLayout</code>.
  31. *
  32. * @param align
  33. * the alignment value
  34. */
  35. public WrapLayout(int align) {
  36. super(align);
  37. }
  38.  
  39. /**
  40. * Creates a new flow layout manager with the indicated alignment and the
  41. * indicated horizontal and vertical gaps.
  42. * <p>
  43. * The value of the alignment argument must be one of
  44. * <code>WrapLayout</code>, <code>WrapLayout</code>, or
  45. * <code>WrapLayout</code>.
  46. *
  47. * @param align
  48. * the alignment value
  49. * @param hgap
  50. * the horizontal gap between components
  51. * @param vgap
  52. * the vertical gap between components
  53. */
  54. public WrapLayout(int align, int hgap, int vgap) {
  55. super(align, hgap, vgap);
  56. }
  57.  
  58. /**
  59. * Returns the preferred dimensions for this layout given the <i>visible</i>
  60. * components in the specified target container.
  61. *
  62. * @param target
  63. * the component which needs to be laid out
  64. * @return the preferred dimensions to lay out the subcomponents of the
  65. * specified container
  66. */
  67. @Override
  68. public Dimension preferredLayoutSize(Container target) {
  69. return layoutSize(target, true);
  70. }
  71.  
  72. /**
  73. * Returns the minimum dimensions needed to layout the <i>visible</i>
  74. * components contained in the specified target container.
  75. *
  76. * @param target
  77. * the component which needs to be laid out
  78. * @return the minimum dimensions to lay out the subcomponents of the
  79. * specified container
  80. */
  81. @Override
  82. public Dimension minimumLayoutSize(Container target) {
  83. Dimension minimum = layoutSize(target, false);
  84. minimum.width -= (getHgap() + 1);
  85. return minimum;
  86. }
  87.  
  88. /**
  89. * Returns the minimum or preferred dimension needed to layout the target
  90. * container.
  91. *
  92. * @param target
  93. * target to get layout size for
  94. * @param preferred
  95. * should preferred size be calculated
  96. * @return the dimension to layout the target container
  97. */
  98. private Dimension layoutSize(Container target, boolean preferred) {
  99. synchronized (target.getTreeLock()) {
  100. // Each row must fit with the width allocated to the containter.
  101. // When the container width = 0, the preferred width of the
  102. // container
  103. // has not yet been calculated so lets ask for the maximum.
  104. int targetWidth = target.getSize().width;
  105.  
  106. if (targetWidth == 0)
  107. return new Dimension();
  108. // if (targetWidth == 0)
  109. // targetWidth = Integer.MAX_VALUE;
  110.  
  111. int hgap = getHgap();
  112. int vgap = getVgap();
  113. Insets insets = target.getInsets();
  114. int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
  115. int maxWidth = targetWidth - horizontalInsetsAndGap;
  116.  
  117. // Fit components into the allowed width
  118.  
  119. Dimension dim = new Dimension(0, 0);
  120. int rowWidth = 0;
  121. int rowHeight = 0;
  122.  
  123. int nmembers = target.getComponentCount();
  124.  
  125. for (int i = 0; i < nmembers; i++) {
  126. Component m = target.getComponent(i);
  127.  
  128. if (m.isVisible()) {
  129. Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize();
  130.  
  131. // Can't add the component to current row. Start a new row.
  132.  
  133. if (rowWidth + d.width > maxWidth) {
  134. addRow(dim, rowWidth, rowHeight);
  135. rowWidth = 0;
  136. rowHeight = 0;
  137. }
  138.  
  139. // Add a horizontal gap for all components after the first
  140.  
  141. if (rowWidth != 0) {
  142. rowWidth += hgap;
  143. }
  144.  
  145. rowWidth += d.width;
  146. rowHeight = Math.max(rowHeight, d.height);
  147. }
  148. }
  149.  
  150. addRow(dim, rowWidth, rowHeight);
  151.  
  152. dim.width += horizontalInsetsAndGap;
  153. dim.height += insets.top + insets.bottom + vgap * 2;
  154.  
  155. // When using a scroll pane or the DecoratedLookAndFeel we need to
  156. // make sure the preferred size is less than the size of the
  157. // target containter so shrinking the container size works
  158. // correctly. Removing the horizontal gap is an easy way to do this.
  159.  
  160. Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target);
  161.  
  162. if (scrollPane != null && target.isValid()) {
  163. dim.width -= (hgap + 1);
  164. }
  165.  
  166. return dim;
  167. }
  168. }
  169.  
  170. /*
  171. * A new row has been completed. Use the dimensions of this row to update
  172. * the preferred size for the container.
  173. *
  174. * @param dim update the width and height when appropriate
  175. *
  176. * @param rowWidth the width of the row to add
  177. *
  178. * @param rowHeight the height of the row to add
  179. */
  180. private void addRow(Dimension dim, int rowWidth, int rowHeight) {
  181. dim.width = Math.max(dim.width, rowWidth);
  182.  
  183. if (dim.height > 0) {
  184. dim.height += getVgap();
  185. }
  186.  
  187. dim.height += rowHeight;
  188. }
  189. }

Swing中支持自动换行的WrapLayout的更多相关文章

  1. 在DirectShow中支持DXVA 2.0(Supporting DXVA 2.0 in DirectShow)

    这几天在做dxva2硬件加速,找不到什么资料,翻译了一下微软的两篇相关文档.并准备记录一下用ffmpeg实现dxva2,将在第三篇写到.这是第二篇.,英文原址:https://msdn.microso ...

  2. Swing杂记——Swing中引入Android的NinePatch技术,让Swing拥有Android的外观定制能力

    [摘要] 本文诣在展示如何在Swing中引入 NinePatch技术(早期有文章里中文译作九格图,暂且这么叫吧^_^,但此术非传统移动手机上的功能布局——九格图哦). [准备篇] Q:何为 NineP ...

  3. 【Swing】理解Swing中的事件与线程

    talk is cheap , show me the code. Swing中的事件 事件驱动 所有的GUI程序都是事件驱动的.Swing当然也是. GUI程序不同于Command Line程序,一 ...

  4. iOS6:在你的企业系统中支持Passbook

    前言 这是一篇翻译,感谢Jonathan Tang. 原文地址:iOS 6 Tutorial: Supporting Passbook within Your Enterprise Systems 正 ...

  5. webView中支持input的file的选择和alert弹出

    alert()弹出 input的file现选择(特别说明:不同的android版本弹出的样式不同,选择文件后自动上传) webView.setWebChromeClient(new WebChrome ...

  6. Java学习笔记——可视化Swing中JTable控件绑定SQL数据源的两种方法

    在 MyEclipse 的可视化 Swing 中,有 JTable 控件. JTable 用来显示和编辑常规二维单元表. 那么,如何将 数据库SQL中的数据绑定至JTable中呢? 在这里,提供两种方 ...

  7. <td style="word-break:break-all"> 在html中控制自动换行

    在html中控制自动换行   其实只要在表格控制中添加一句 <td style="word-break:break-all">就搞定了. 其中可能对英文换行可能会分开一 ...

  8. EntityFramework中支持BulkInsert扩展

    EntityFramework中支持BulkInsert扩展 本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 前言 很显然,你应该不至于使用 Ent ...

  9. 详解Swing中JTree组件的功能

    JTree组件是另外一个复杂组件,它不像 JTable 那样难用,但是也不像 JList 那么容易.使用 JTree 时麻烦的部分是它要求的数据模型. JTree组件的功能来自树的概念,树有分支和叶子 ...

随机推荐

  1. osds have slow requests

    ceph health detailHEALTH_WARN 14 requests are blocked > 32 sec; 11 osds have slow requests7 ops a ...

  2. Angularjs 根据数据结构创建动态菜单无限嵌套循环--指令版

    目标:根据数据生成动态菜单,希望可以根据判断是否有子集无限循环下去. 菜单希望的样子是这样的: 菜单数据是这样的: $scope.expanders = [{ title: 'title1', lin ...

  3. internet连接共享

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_shai nternet连接共享 允许其他网络用户通过此计算机的internet连接来连接

  4. POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀数组 倍增)

    题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 比后缀自动机慢好多(废话→_→). \(Description\) 求两个字符串最长公共子串 ...

  5. Python3.x使用PyMysql连接MySQL数据库

    Python3.x使用PyMysql连接MySQL数据库 由于Python3.x不向前兼容,导致Python2.x中的很多库在Python3.x中无法使用,例如Mysqldb,我前几天写了一篇博客Py ...

  6. (netty宝贵知识)

    例子:https://segmentfault.com/a/1190000013122610?utm_source=tag-newest#articleHeader0 netty官方文档http:// ...

  7. 与TIME_WAIT相关的几个内核参数修改测试讨论结论

    以下来结论自tcpcopy & gryphon讨论群 经过试验测试得出,不保证肯定正确.   net.ipv4.tcp_tw_recycle net.ipv4.tcp_tw_reuse net ...

  8. python图片和分形树

    链接: 这10个Python项目很有趣! Python 绘制分形图(曼德勃罗集.分形树叶.科赫曲线.分形龙.谢尔宾斯基三角等)附代码 使用Python生成树形图案 神奇的代码:用 Python 生成分 ...

  9. Linux.Centos.yum命令的“No module named yum”错误

    Centos版本: uname -a Linux ygpiao -.el6.x86_64 # SMP Tue Jun :: UTC x86_64 x86_64 x86_64 GNU/Linux 在一次 ...

  10. __Linux__文件和目录

    Linux 目录 /:根目录,一般根目录下只存放目录,在Linux下有且只有一个根目录.所有的东西都是从这里开始.当你在终端里输入“/home”,你其实是在告诉电脑,先从/(根目录)开始,再进入到ho ...