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

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

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

package com.miui.theme.tool.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets; import javax.swing.JScrollPane;
import javax.swing.SwingUtilities; /**
* FlowLayout subclass that fully supports wrapping of components.
*/
public class WrapLayout extends FlowLayout {
private Dimension preferredLayoutSize; /**
* Constructs a new <code>WrapLayout</code> with a left alignment and a
* default 5-unit horizontal and vertical gap.
*/
public WrapLayout() {
super();
} /**
* Constructs a new <code>FlowLayout</code> with the specified alignment and
* a default 5-unit horizontal and vertical gap. The value of the alignment
* argument must be one of <code>WrapLayout</code>, <code>WrapLayout</code>,
* or <code>WrapLayout</code>.
*
* @param align
* the alignment value
*/
public WrapLayout(int align) {
super(align);
} /**
* Creates a new flow layout manager with the indicated alignment and the
* indicated horizontal and vertical gaps.
* <p>
* The value of the alignment argument must be one of
* <code>WrapLayout</code>, <code>WrapLayout</code>, or
* <code>WrapLayout</code>.
*
* @param align
* the alignment value
* @param hgap
* the horizontal gap between components
* @param vgap
* the vertical gap between components
*/
public WrapLayout(int align, int hgap, int vgap) {
super(align, hgap, vgap);
} /**
* Returns the preferred dimensions for this layout given the <i>visible</i>
* components in the specified target container.
*
* @param target
* the component which needs to be laid out
* @return the preferred dimensions to lay out the subcomponents of the
* specified container
*/
@Override
public Dimension preferredLayoutSize(Container target) {
return layoutSize(target, true);
} /**
* Returns the minimum dimensions needed to layout the <i>visible</i>
* components contained in the specified target container.
*
* @param target
* the component which needs to be laid out
* @return the minimum dimensions to lay out the subcomponents of the
* specified container
*/
@Override
public Dimension minimumLayoutSize(Container target) {
Dimension minimum = layoutSize(target, false);
minimum.width -= (getHgap() + 1);
return minimum;
} /**
* Returns the minimum or preferred dimension needed to layout the target
* container.
*
* @param target
* target to get layout size for
* @param preferred
* should preferred size be calculated
* @return the dimension to layout the target container
*/
private Dimension layoutSize(Container target, boolean preferred) {
synchronized (target.getTreeLock()) {
// Each row must fit with the width allocated to the containter.
// When the container width = 0, the preferred width of the
// container
// has not yet been calculated so lets ask for the maximum.
int targetWidth = target.getSize().width; if (targetWidth == 0)
return new Dimension();
// if (targetWidth == 0)
// targetWidth = Integer.MAX_VALUE; int hgap = getHgap();
int vgap = getVgap();
Insets insets = target.getInsets();
int horizontalInsetsAndGap = insets.left + insets.right + (hgap * 2);
int maxWidth = targetWidth - horizontalInsetsAndGap; // Fit components into the allowed width Dimension dim = new Dimension(0, 0);
int rowWidth = 0;
int rowHeight = 0; int nmembers = target.getComponentCount(); for (int i = 0; i < nmembers; i++) {
Component m = target.getComponent(i); if (m.isVisible()) {
Dimension d = preferred ? m.getPreferredSize() : m.getMinimumSize(); // Can't add the component to current row. Start a new row. if (rowWidth + d.width > maxWidth) {
addRow(dim, rowWidth, rowHeight);
rowWidth = 0;
rowHeight = 0;
} // Add a horizontal gap for all components after the first if (rowWidth != 0) {
rowWidth += hgap;
} rowWidth += d.width;
rowHeight = Math.max(rowHeight, d.height);
}
} addRow(dim, rowWidth, rowHeight); dim.width += horizontalInsetsAndGap;
dim.height += insets.top + insets.bottom + vgap * 2; // When using a scroll pane or the DecoratedLookAndFeel we need to
// make sure the preferred size is less than the size of the
// target containter so shrinking the container size works
// correctly. Removing the horizontal gap is an easy way to do this. Container scrollPane = SwingUtilities.getAncestorOfClass(JScrollPane.class, target); if (scrollPane != null && target.isValid()) {
dim.width -= (hgap + 1);
} return dim;
}
} /*
* A new row has been completed. Use the dimensions of this row to update
* the preferred size for the container.
*
* @param dim update the width and height when appropriate
*
* @param rowWidth the width of the row to add
*
* @param rowHeight the height of the row to add
*/
private void addRow(Dimension dim, int rowWidth, int rowHeight) {
dim.width = Math.max(dim.width, rowWidth); if (dim.height > 0) {
dim.height += getVgap();
} dim.height += rowHeight;
}
}

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. OutputStreramWriter和InputStreamReader类

    整个IO类中除了字节流和字符流还包括字节和字符转换流. OutputStreramWriter将输出的字符流转化为字节流 InputStreamReader将输入的字节流转换为字符流 但是不管如何操作 ...

  2. HDU.4757.Tree(可持久化Trie)

    题目链接 \(Description\) 给定一棵树,点有点权.\(Q\)次询问\(x,y,z\),求\(x\)到\(y\)的简单路径中,与\(z\)异或能得到的最大的数是多少. \(Solution ...

  3. pwcrack--一款集合多种md5解密的工具

    项目开源地址:https://github.com/L-codes/pwcrack-framework Ruby2.5+ (tested with Ruby2.5.3 & Ruby 2.6.3 ...

  4. Python3练习题系列(08)——代码阅读方法及字典跳转表理解

    问题:分析下面代码 cities['_find'] = find_city city_found = cities['_find'](cities, state) 分析过程: 一个函数也可以作为一个变 ...

  5. C++ 模板应用举例_模板实现STL类(堆栈)

    //stack集合类是一个简单的堆栈的实现. //这里有两个模板参数,T和size,指定堆栈中的元素类型和堆栈中项数的最大值. //push 和 pop成员函数添加和删除堆栈中的项,并在堆栈底部增加. ...

  6. linux 关于时间日期date

    一.查看和修改Linux的时区 1. 查看当前时区 命令 : "date -R" 2. 修改设置Linux服务器时区 方法 A 命令 : "tzselect" ...

  7. QT.Qt qmake报错(TypeError: Property 'asciify' of object Core::Internal::UtilsJsExtension)

    出错信息 打开左边的"项目" 把右侧的"构建目录"修改成你项目所在的文件夹 再次运行试试 成功!

  8. 对比 PHP 中 new static() 与 new self()

    通过new static()与new self()都能产生实例对象,new static()是在PHP5.3版本中引入的新特性,本文对二者稍作对比. 一.当直接通过本类创建实例时 class Test ...

  9. Reactor 3 学习笔记(1)

    Reactor 3 与之前学习的RxJava是同一类(反应式编程)框架,基本概念大致差不多,简单记录一下: Reactor 3 利用了java 8中的CompletableFuture.Stream. ...

  10. C#如何测量字符串的高度宽度和精确取得字符串的高度宽度

    C#如何测量字符串的高度宽度和精确取得字符串的高度宽度 因为MFC中CDC有GetTextExtent()可以获得字符串的高度宽度 像素单位,所以自然想到c#的GDI+的MeasureString,这 ...