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组件的功能来自树的概念,树有分支和叶子 ...
随机推荐
- osds have slow requests
ceph health detailHEALTH_WARN 14 requests are blocked > 32 sec; 11 osds have slow requests7 ops a ...
- Angularjs 根据数据结构创建动态菜单无限嵌套循环--指令版
目标:根据数据生成动态菜单,希望可以根据判断是否有子集无限循环下去. 菜单希望的样子是这样的: 菜单数据是这样的: $scope.expanders = [{ title: 'title1', lin ...
- internet连接共享
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_shai nternet连接共享 允许其他网络用户通过此计算机的internet连接来连接
- POJ.2774.Long Long Message/SPOJ.1811.LCS(后缀数组 倍增)
题目链接 POJ2774 SPOJ1811 LCS - Longest Common Substring 比后缀自动机慢好多(废话→_→). \(Description\) 求两个字符串最长公共子串 ...
- Python3.x使用PyMysql连接MySQL数据库
Python3.x使用PyMysql连接MySQL数据库 由于Python3.x不向前兼容,导致Python2.x中的很多库在Python3.x中无法使用,例如Mysqldb,我前几天写了一篇博客Py ...
- (netty宝贵知识)
例子:https://segmentfault.com/a/1190000013122610?utm_source=tag-newest#articleHeader0 netty官方文档http:// ...
- 与TIME_WAIT相关的几个内核参数修改测试讨论结论
以下来结论自tcpcopy & gryphon讨论群 经过试验测试得出,不保证肯定正确. net.ipv4.tcp_tw_recycle net.ipv4.tcp_tw_reuse net ...
- python图片和分形树
链接: 这10个Python项目很有趣! Python 绘制分形图(曼德勃罗集.分形树叶.科赫曲线.分形龙.谢尔宾斯基三角等)附代码 使用Python生成树形图案 神奇的代码:用 Python 生成分 ...
- 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 在一次 ...
- __Linux__文件和目录
Linux 目录 /:根目录,一般根目录下只存放目录,在Linux下有且只有一个根目录.所有的东西都是从这里开始.当你在终端里输入“/home”,你其实是在告诉电脑,先从/(根目录)开始,再进入到ho ...