Swing中支持自动换行的WrapLayout
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的更多相关文章
- 在DirectShow中支持DXVA 2.0(Supporting DXVA 2.0 in DirectShow)
这几天在做dxva2硬件加速,找不到什么资料,翻译了一下微软的两篇相关文档.并准备记录一下用ffmpeg实现dxva2,将在第三篇写到.这是第二篇.,英文原址:https://msdn.microso ...
- Swing杂记——Swing中引入Android的NinePatch技术,让Swing拥有Android的外观定制能力
[摘要] 本文诣在展示如何在Swing中引入 NinePatch技术(早期有文章里中文译作九格图,暂且这么叫吧^_^,但此术非传统移动手机上的功能布局——九格图哦). [准备篇] Q:何为 NineP ...
- 【Swing】理解Swing中的事件与线程
talk is cheap , show me the code. Swing中的事件 事件驱动 所有的GUI程序都是事件驱动的.Swing当然也是. GUI程序不同于Command Line程序,一 ...
- iOS6:在你的企业系统中支持Passbook
前言 这是一篇翻译,感谢Jonathan Tang. 原文地址:iOS 6 Tutorial: Supporting Passbook within Your Enterprise Systems 正 ...
- webView中支持input的file的选择和alert弹出
alert()弹出 input的file现选择(特别说明:不同的android版本弹出的样式不同,选择文件后自动上传) webView.setWebChromeClient(new WebChrome ...
- Java学习笔记——可视化Swing中JTable控件绑定SQL数据源的两种方法
在 MyEclipse 的可视化 Swing 中,有 JTable 控件. JTable 用来显示和编辑常规二维单元表. 那么,如何将 数据库SQL中的数据绑定至JTable中呢? 在这里,提供两种方 ...
- <td style="word-break:break-all"> 在html中控制自动换行
在html中控制自动换行 其实只要在表格控制中添加一句 <td style="word-break:break-all">就搞定了. 其中可能对英文换行可能会分开一 ...
- EntityFramework中支持BulkInsert扩展
EntityFramework中支持BulkInsert扩展 本文为 Dennis Gao 原创技术文章,发表于博客园博客,未经作者本人允许禁止任何形式的转载. 前言 很显然,你应该不至于使用 Ent ...
- 详解Swing中JTree组件的功能
JTree组件是另外一个复杂组件,它不像 JTable 那样难用,但是也不像 JList 那么容易.使用 JTree 时麻烦的部分是它要求的数据模型. JTree组件的功能来自树的概念,树有分支和叶子 ...
随机推荐
- 打开Word时出现“The setup controller has encountered a problem during install. Please ...”
找到C:\Program Files\Common Files\Microsoft Shared\OFFICE12\Office Setup Controller,将这个文件夹删除或改名,就不再出现提 ...
- {}+[]与console.log({}+[])结果不同?从JavaScript的大括号谈起
看到这样一个问题:为什么直接在控制台运行{} + []和用console.log({} + [])输出,两者结果不一样? 于是乎打开chrome的控制台运行了一下: 为什么结果会这样呢?不得已学习一下 ...
- 潭州课堂25班:Ph201805201 第八课:函数基础和函数参数 (课堂笔记)
1, 函数定义 def fun(): print('测试函数') fun() #调用函数 return 运行函数返回值 def fun(): name = [1,3,4,5] return name[ ...
- LED类代码
/* led.c文件 标题: 点亮一个了LED灯 电路:开发板中P2口已接到LED灯的阴极 */ #include <reg52.h> #include "led1.h&qu ...
- zabbix-agentd 安装
[root@node2 ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-1.el7.ce ...
- VMware workstation12 密匙
VMware workstation12 密匙:5A02H-AU243-TZJ49-GTC7K-3C61N
- python 爬虫不停换代理
内网看到的一个小技巧,卧槽,感觉真TM厉害 函数均放到main函数内即可 def get_proxy(): url="http://www.xicidaili.com" req=u ...
- PHP Redis 对象方法手册
redis(Remote Dictionary Server)是一种Nosql技术,它是一个开源的高级kv存储和数据结构存储系统. redis不仅仅是能够存储key和value这种简单的键值对,还能存 ...
- db2执行计划具体操作
explain 1.如果第一次执行,请先(在dbinst用户下) connect to dbname,执行db2 -tvf $HOME/sqllib/misc/EXPLAIN.DDL建立执行计划表 2 ...
- C++.Linux下redis编程:error while loading shared libraries: libhiredis.so.0.13
编译 sudo gcc -o sltest01 sltest01.c -L/usr/local/lib/ -lhiredis 运行 sudo ./sltest01 编译成功后运行报错信息: ./slt ...