java垂直布局_Java Swing 垂直流布局管理器实现

importjava.awt.Component;

importjava.awt.Container;

importjava.awt.Dimension;

importjava.awt.FlowLayout;

importjava.awt.Insets;

importjavax.swing.JButton;

importjavax.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.

*/

publicclassVFlowLayoutextendsFlowLayout

{

/**

*

*/

privatestaticfinallongserialVersionUID = 1L;

/**

* Specify alignment top.

*/

publicstaticfinalintTOP =0;

/**

* Specify a middle alignment.

*/

publicstaticfinalintMIDDLE =1;

/**

* Specify the alignment to be bottom.

*/

publicstaticfinalintBOTTOM =2;

inthgap;

intvgap;

booleanhfill;

booleanvfill;

publicstaticvoidmain(String[] args)

{

System.out.println("Just for test ...");

JFrame frame =newJFrame();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setBounds(0,0,600,600);

frame.setLayout(newVFlowLayout());

inti =0;

frame.add(newJButton(String.valueOf(i++)));

frame.add(newJButton(String.valueOf(i++)));

frame.add(newJButton(String.valueOf(i++)));

frame.add(newJButton(String.valueOf(i++)));

frame.add(newJButton(String.valueOf(i++)));

frame.add(newJButton(String.valueOf(i++)));

frame.add(newJButton(String.valueOf(i++)));

frame.add(newJButton(String.valueOf(i++)));

frame.add(newJButton(String.valueOf(i++)));

frame.add(newJButton(String.valueOf(i++)));

frame.add(newJButton(String.valueOf(i++)));

frame.setVisible(true);

}

/**

* Construct a new VerticalFlowLayout with a middle alignment, and the fill

* to edge flag set.

*/

publicVFlowLayout()

{

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.

*/

publicVFlowLayout(booleanhfill,booleanvfill)

{

this(TOP,5,5, hfill, vfill);

}

/**

* Construct a new VerticalFlowLayout with a middle alignment.

*

* @param align

*            the alignment value

*/

publicVFlowLayout(intalign)

{

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.

*/

publicVFlowLayout(intalign,booleanhfill,booleanvfill)

{

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.

*/

publicVFlowLayout(intalign,inthgap,intvgap,booleanhfill,booleanvfill)

{

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

*/

publicDimension preferredLayoutSize(Container target)

{

Dimension tarsiz =newDimension(0,0);

for(inti =0; 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;

returntarsiz;

}

/**

* Returns the minimum size needed to layout the target container.

*

* @param target

*            the component to lay out.

* @return the minimum layout dimension.

*/

publicDimension minimumLayoutSize(Container target)

{

Dimension tarsiz =newDimension(0,0);

for(inti =0; 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;

returntarsiz;

}

/**

* Set true to fill vertically.

*

* @param vfill

*            true to fill vertically.

*/

publicvoidsetVerticalFill(booleanvfill)

{

this.vfill = vfill;

}

/**

* Returns true if the layout vertically fills.

*

* @return true if vertically fills the layout using the specified.

*/

publicbooleangetVerticalFill()

{

returnvfill;

}

/**

* Set to true to enable horizontally fill.

*

* @param hfill

*            true to fill horizontally.

*/

publicvoidsetHorizontalFill(booleanhfill)

{

this.hfill = hfill;

}

/**

* Returns true if the layout horizontally fills.

*

* @return true if horizontally fills.

*/

publicbooleangetHorizontalFill()

{

returnhfill;

}

/**

* 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.

*/

privatevoidplacethem(Container target,intx,inty,intwidth,intheight,intfirst,intlast)

{

intalign = getAlignment();

if(align == MIDDLE)

{

y += height /2;

}

if(align == BOTTOM)

{

y += height;

}

for(inti = first; i 

{

Component m = target.getComponent(i);

Dimension md = m.getSize();

if(m.isVisible())

{

intpx = x + (width - md.width) /2;

m.setLocation(px, y);

y += vgap + md.height;

}

}

}

/**

* Lays out the container.

*

* @param target

*            the container to lay out.

*/

publicvoidlayoutContainer(Container target)

{

Insets insets = target.getInsets();

intmaxheight = target.getSize().height - (insets.top + insets.bottom + vgap *2);

intmaxwidth = target.getSize().width - (insets.left + insets.right + hgap *2);

intnumcomp = target.getComponentCount();

intx = insets.left + hgap, y =0;

intcolw =0, start =0;

for(inti =0; 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);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值