package layout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Insets;
import javax.swing.JButton;
import javax.swing.JFrame;
/**
* VerticalFlowLayout is similar to FlowLayout except it lays out components
* vertically. Extends FlowLayout because it mimics much of the behavior of the
* FlowLayout class, except vertically. An additional feature is that you can
* specify a fill to edge flag, which causes the VerticalFlowLayout manager to
* resize all components to expand to the column width Warning: This causes
* problems when the main panel has less space that it needs and it seems to
* prohibit multi-column output. Additionally there is a vertical fill flag,
* which fills the last component to the remaining height of the container.
*/
public class VFlowLayout extends FlowLayout
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* Specify alignment top.
*/
public static final int TOP = 0;
/**
* Specify a middle alignment.
*/
public static final int MIDDLE = 1;
/**
* Specify the alignment to be bottom.
*/
public static final int BOTTOM = 2;
int hgap;
int vgap;
boolean hfill;
boolean vfill;
public static void main(String[] args)
{
System.out.println("Just for test ...");
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(0, 0, 600, 600);
frame.setLayout(new VFlowLayout());
int i = 0;
frame.add(new JButton(String.valueOf(i++)));
frame.add(new JButton(String.valueOf(i++)));
frame.add(new JButton(String.valueOf(i++)));
frame.add(new JButton(String.valueOf(i++)));
frame.add(new JButton(String.valueOf(i++)));
frame.add(new JButton(String.valueOf(i++)));
frame.add(new JButton(String.valueOf(i++)));
frame.add(new JButton(String.valueOf(i++)));
frame.add(new JButton(String.valueOf(i++)));
frame.add(new JButton(String.valueOf(i++)));
frame.add(new JButton(String.valueOf(i++)));
frame.setVisible(true);
}
/**
* Construct a new VerticalFlowLayout with a middle alignment, and the fill
* to edge flag set.
*/
public VFlowLayout()
{
this(TOP, 5, 5, true, false);
}
/**
* Construct a new VerticalFlowLayout with a middle alignment.
*
* @param hfill
* the fill to edge flag
* @param vfill
* the vertical fill in pixels.
*/
public VFlowLayout(boolean hfill, boolean vfill)
{
this(TOP, 5, 5, hfill, vfill);
}
/**
* Construct a new VerticalFlowLayout with a middle alignment.
*
* @param align
* the alignment value
*/
public VFlowLayout(int align)
{
this(align, 5, 5, true, false);
}
/**
* Construct a new VerticalFlowLayout.
*
* @param align
* the alignment value
* @param hfill
* the horizontalfill in pixels.
* @param vfill
* the vertical fill in pixels.
*/
public VFlowLayout(int align, boolean hfill, boolean vfill)
{
this(align, 5, 5, hfill, vfill);
}
/**
* Construct a new VerticalFlowLayout.
*
* @param align
* the alignment value
* @param hgap
* the horizontal gap variable
* @param vgap
* the vertical gap variable
* @param hfill
* the fill to edge flag
* @param vfill
* true if the panel should vertically fill.
*/
public VFlowLayout(int align, int hgap, int vgap, boolean hfill, boolean vfill)
{
setAlignment(align);
this.hgap = hgap;
this.vgap = vgap;
this.hfill = hfill;
this.vfill = vfill;
}
/**
* Returns the preferred dimensions given the components in the target
* container.
*
* @param target
* the component to lay out
*/
public Dimension preferredLayoutSize(Container target)
{
Dimension tarsiz = new Dimension(0, 0);
for (int i = 0; i < target.getComponentCount(); i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getPreferredSize();
tarsiz.width = Math.max(tarsiz.width, d.width);
if (i > 0)
{
tarsiz.height += hgap;
}
tarsiz.height += d.height;
}
}
Insets insets = target.getInsets();
tarsiz.width += insets.left + insets.right + hgap * 2;
tarsiz.height += insets.top + insets.bottom + vgap * 2;
return tarsiz;
}
/**
* Returns the minimum size needed to layout the target container.
*
* @param target
* the component to lay out.
* @return the minimum layout dimension.
*/
public Dimension minimumLayoutSize(Container target)
{
Dimension tarsiz = new Dimension(0, 0);
for (int i = 0; i < target.getComponentCount(); i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getMinimumSize();
tarsiz.width = Math.max(tarsiz.width, d.width);
if (i > 0)
{
tarsiz.height += vgap;
}
tarsiz.height += d.height;
}
}
Insets insets = target.getInsets();
tarsiz.width += insets.left + insets.right + hgap * 2;
tarsiz.height += insets.top + insets.bottom + vgap * 2;
return tarsiz;
}
/**
* Set true to fill vertically.
*
* @param vfill
* true to fill vertically.
*/
public void setVerticalFill(boolean vfill)
{
this.vfill = vfill;
}
/**
* Returns true if the layout vertically fills.
*
* @return true if vertically fills the layout using the specified.
*/
public boolean getVerticalFill()
{
return vfill;
}
/**
* Set to true to enable horizontally fill.
*
* @param hfill
* true to fill horizontally.
*/
public void setHorizontalFill(boolean hfill)
{
this.hfill = hfill;
}
/**
* Returns true if the layout horizontally fills.
*
* @return true if horizontally fills.
*/
public boolean getHorizontalFill()
{
return hfill;
}
/**
* places the components defined by first to last within the target
* container using the bounds box defined.
*
* @param target
* the container.
* @param x
* the x coordinate of the area.
* @param y
* the y coordinate of the area.
* @param width
* the width of the area.
* @param height
* the height of the area.
* @param first
* the first component of the container to place.
* @param last
* the last component of the container to place.
*/
private void placethem(Container target, int x, int y, int width, int height, int first, int last)
{
int align = getAlignment();
if (align == MIDDLE)
{
y += height / 2;
}
if (align == BOTTOM)
{
y += height;
}
for (int i = first; i < last; i++)
{
Component m = target.getComponent(i);
Dimension md = m.getSize();
if (m.isVisible())
{
int px = x + (width - md.width) / 2;
m.setLocation(px, y);
y += vgap + md.height;
}
}
}
/**
* Lays out the container.
*
* @param target
* the container to lay out.
*/
public void layoutContainer(Container target)
{
Insets insets = target.getInsets();
int maxheight = target.getSize().height - (insets.top + insets.bottom + vgap * 2);
int maxwidth = target.getSize().width - (insets.left + insets.right + hgap * 2);
int numcomp = target.getComponentCount();
int x = insets.left + hgap, y = 0;
int colw = 0, start = 0;
for (int i = 0; i < numcomp; i++)
{
Component m = target.getComponent(i);
if (m.isVisible())
{
Dimension d = m.getPreferredSize();
// fit last component to remaining height
if ((this.vfill) && (i == (numcomp - 1)))
{
d.height = Math.max((maxheight - y), m.getPreferredSize().height);
}
// fit component size to container width
if (this.hfill)
{
m.setSize(maxwidth, d.height);
d.width = maxwidth;
}
else
{
m.setSize(d.width, d.height);
}
if (y + d.height > maxheight)
{
placethem(target, x, insets.top + vgap, colw, maxheight - y, start, i);
y = d.height;
x += hgap + colw;
colw = d.width;
start = i;
}
else
{
if (y > 0)
{
y += vgap;
}
y += d.height;
colw = Math.max(colw, d.width);
}
}
}
placethem(target, x, insets.top + vgap, colw, maxheight - y, start, numcomp);
}
}
分享到:
相关推荐
FlowLayout 是 Java Swing 库提供的一种布局管理器,用于在容器中按照水平或垂直方向依次排列组件。 FlowLayout 的特点如下: 组件按照添加的顺序依次排列,并自动换行。 默认情况下,组件在容器中居中对齐。 组件...
FlowLayout流式布局.rar,太多无法一一验证是否可用,程序如果跑不起来需要自调,部分代码功能进行参考学习。
android-flowlayout A very convenient and powerful flow layout created by Lan. minSdkVersion 11 Dependencies latest version see it as an icon in the beginning of the README file. gradle compile '...
基于JAVA的布局管理器-FlowLayout
流式布局,适合用于标签,分类筛选等。适配器方式调用,支持单选,多选,自用显示,单行显示(类似listview),指定显示条数(类似gridview)
FlowLayout - 适用于iOS的双向布局框架
主要介绍了Java Swing组件布局管理器之FlowLayout(流式布局),结合实例形式分析了Swing组件布局管理器FlowLayout流式布局的常用方法及相关使用技巧,需要的朋友可以参考下
compile 'com.zhy:flowlayout-lib:1.0.3' } 声明 布局文件中声明: 支持属性: max_select:-1为不限制选择数量,>=1的数字为控制选择tag的数量 auto_select_effect 是否开启默认的选中效果,即为selector中设置...
全文详细的介绍了Swing结构与布局管理器
实现流式布局,可配置自定义属性,实现单选与多选两种模式以及点击事件
this is a draggable flow layout lib (android 可拖拽的流布局库) . Sample apk/示例app 特点 1, 类似可拖拽的GridView. 不过gridView 宽度/个数是固定的。 这个布局item宽度是不定的(放不下自动换行)。 2,...
说真的自从对RecyclerView的LayoutManager有新的认识后,完全不用担心很多的复杂布局了。而且对ViewGroup测量过程也不用担心了,因为里面有LayoutManager帮我们实现了。下面就进入该篇文章的主题吧,废话不多说,...
Android 流式布局FlowLayout 实现关键字标签
Android实现FlowLayout流式布局(仿热门标签)
主要介绍了JavaSwing FlowLayout 流式布局的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
自定义view-FlowLayout流式布局!
Android 实现FlowLayout流式布局(类似热门标签) , 使用前请认真阅读文件顶部的使用说明
使用java Swing手写Jtable增删改查 怀旧版,涉及到简单布局(BorderLayout、Flowlayout)、Dialog、Jframe、Jpanel等