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组件的功能来自树的概念,树有分支和叶子 ...
随机推荐
- 利用SQL为Code128码添加起始符和休止符
在利用code128码字体打印条码是,打印出来的条形码,扫描枪会出现认不出的情况,这种情况是由于直接将文本设置为code128字体而没有给他们指定起始符和休止符引起的. 经过查资料获发现好多人遇到这样 ...
- Linux配置Redis外网访问
下载:redis-3.2.5.tar.gz 拷贝到指定目录解压:tar -xzvf redis-3.2.5.tar.gz 进入到解压目录redis-3.2.5下安装: makemake install ...
- 更新 是 可用的 针对 安卓 软件开发包和工具 Updates are available for android software development packages and tools
作者:韩梦飞沙 Author:han_meng_fei_sha 邮箱:313134555@qq.com E-mail: 313134555 @qq.com 更新 是 可用的 针对 安卓 软件开发包和工 ...
- 2827: 千山鸟飞绝 非旋treap
国际惯例的题面:看起来很不可做的样子,我们先来整理一下题意吧.就是,维护每个点曾经拥有过的最大的两个属性值,支持把点的位置移动.我们用map对每个位置进行离散化,对每个位置建立一个平衡树.为了方便分离 ...
- Bzoj2673 3961: [WF2011]Chips Challenge 费用流
国际惯例题面:如果我们枚举放几个零件的话,第二个限制很容易解决,但是第一个怎么办?(好的,这么建图不可做)考虑我们枚举每行每列最多放几个零件t,然后计算零件总数sum.这样如果可行的话,则有t*B&l ...
- PHP Web项目开发学习,经验谈
列个表格吧,看着也比较方便: 时间 事项 六月 七月 八月 九月 工作任务 前端页面 服务端页面 地图定位 服务器搭建 图片上传 视频文件信息的获取 关键词 Css+div+js PHP,谷歌maps ...
- 我定制的Stylish样式
没有办法上传,只好存到这里. 天涯 .atl-con-ft, .js-zhiyin-area, .ds_seat_foot, .hongbao-btn, .dashang-left-btn, .qrc ...
- ubuntu jdk 安装
sudo add-apt-repository ppa:linuxuprising/java sudo apt-get update sudo apt-get install oracle-java1 ...
- 集合(2)—Collection之List的使用方法
声明集合变量 List list = new ArrayList(); 或者 : public LIst list: public 构造函数(){ this.list = new ArrayList( ...
- 使用Maven自动部署Java Web项目到Tomcat问题小记
导读 首先说说自己为啥要用maven管理项目,一个直接的原因是:我在自己电脑上开发web项目,每次部署到服务器上时都要经历如下步骤: 首先在Eclipse里将项目打包成war包 将服务器上原来的项目文 ...