Java Swing实现垂直流布局
想实现一个仿QQ好友列表的界面,网上查了好久,发现基本都是用的GridLayout布局实现的,这个实现有一个bug,就是当好友数量较少的时候,组件会填满整个Frame,自己也尝试过其他的布局管理器,发现都不是那么好用。
后来发现有大佬写了一个垂直布局管理器,大佬文章链接:
https://blog.youkuaiyun.com/Dancen/article/details/7581971
大佬的这个垂直布局管理器存在一个bug,当Frame不能全部显示所有组件时,我们自然会想到用scrollPane,使用大佬的这个布局管理器会导致JScrollPane怎么都不会显示滚动条,即超出的组件将永远无法显示。故此,参考大佬的文章以及java.awt.FlowLayout类的写法。自己写了一个垂直布局管理器VerticalFlowLayout类。此类基本实现了FlowLayout的功能,同时增加了组件宽度自适应Frame的功能。现将源码发布如下,欢迎大家一起学习讨论。
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager;
/**
* A vertical flow layout arranges components in a directional flow, much like
* lines of text in a paragraph. VerticalFlow layouts are typically used to
* arrange buttons in a panel. It arranges buttons vertically until no more
* buttons fit on the same line. The line alignment is determined by the
* <code>align</code> property. The possible values are:
* <ul>
* <li>{@link #TOP TOP}
* <li>{@link #BOTTOM BOTTOM}
* <li>{@link #CENTER CENTER}
* </ul>
* A vertical flow layout lets each component assume its natural (preferred)
* size.
*/
public class VerticalFlowLayout implements LayoutManager {
/**
* This value indicates that each row of components should be top-justified.
*/
public static final int TOP = 0;
/**
* This value indicates that each row of components should be centered.
*/
public static final int CENTER = 1;
/**
* This value indicates that each row of components should be
* bottom-justified.
*/
public static final int BOTTOM = 2;
/**
* <code>align</code> is the property that determines how each column
* distributes empty space. It can be one of the following values:
* <ul>
* <li><code>TOP</code>
* <li><code>RIGHT</code>
* <li><code>BOTTOM</code>
* </ul>
*
* @see #getAlignment()
* @see #setAlignment(int)
*/
private int align;
/**
* The vertical flow layout manager allows a separation of components with
* gaps. The horizontal gap will specify the space between columns and
* between the columns and the borders of the <code>Container</code>.
*
*
* @see #getHgap()
* @see #setHgap(int)
*/
private int hgap;
/**
* The vertical flow layout manager allows a separation of components with
* gaps. The vertical gap will specify the space between components and
* between the components and the borders of the <code>Container</code>.
*
* @see #getVgap()
* @see #setVgap(int)
*/
private int vgap;
/**
* If true, components will be filled with <code>Container</code>
* horizontally.
*
* @see #isHfill()
* @see #setHfill(boolean)
*/
private boolean hfill;
/**
* Constructs a new <code>VerticalFlowLayout</code> with a centered
* alignment and a default 5-unit horizontal and vertical gaps and not
* filled with <code>Container</code> horizontally.
*/
public VerticalFlowLayout() {
this(CENTER, 5, 5, false);
}
/**
* Constructs a new <code>FlowLayout</code> with the specified alignment and
* a default 5-unit horizontal and vertical gaps and not filled with
* <code>Container</code> horizontally. The value of the alignment argument
* must be one of <code>FlowLayout.TOP</code>,
* <code>FlowLayout.BOTTOM</code>, <code>FlowLayout.CENTER</code>.
*
* @param align
* the alignment value
*/
public VerticalFlowLayout(int align) {
this(align, 5, 5, false);
}
/**
* Constructs a new <code>VerticalFlowLayout</code> with the indicated
* alignment and the indicated horizontal and vertical gaps and not filled
* with <code>Container</code> horizontally.
* <p>
* The value of the alignment argument must be one of
* <code>FlowLayout.TOP</code>, <code>FlowLayout.BOTTOM</code>,
* <code>FlowLayout.CENTER</code>.
*
* @param align
* the alignment value
* @param hgap
* the horizontal gap between columns and between the columns and
* the borders of the <code>Container</code>
* @param vgap
* the vertical gap between components and between the components
* and the borders of the <code>Container</code>
*/
public VerticalFlowLayout(int align, int hgap, int vgap) {
this(align, hgap, vgap, false);
}
/**
* Constructs a new <code>VerticalFlowLayout</code> with the indicated
* alignment and the indicated horizontal and vertical gaps and the
* indicated filled with <code>Container</code> horizontally.