在Java中使GridLayout垂直
我正在使用在Java中使GridLayout垂直,java,vertical-alignment,Java,Vertical Alignment,我正在使用GridLayout,下面是我希望网格的外观: 1 9 2 10 3 11 4 12 5 13 6 14 7 15 8 16 根据这一点,这是一个组件定位的问题 ComponentOrientation本身就是一个类,可以传递到Container类的setComponentOrientation方法中,但似乎没有构造函数,也无法更改其字段。通过扩展内置的GridLayout并覆盖单一方法。我已经对这两行进行了注释,它们实际上与父类实现不
GridLayout
,下面是我希望网格的外观:
1 9
2 10
3 11
4 12
5 13
6 14
7 15
8 16
根据这一点,这是一个组件定位的问题
ComponentOrientation
本身就是一个类,可以传递到Container
类的setComponentOrientation
方法中,但似乎没有构造函数,也无法更改其字段。通过扩展内置的GridLayout
并覆盖单一方法。我已经对这两行进行了注释,它们实际上与父类实现不同。其余的注释是原始类的一部分
public class VerticalGridLayout
extends GridLayout
{
/**
* Creates a grid layout with a default of one column per component, in a single row.
*/
public VerticalGridLayout()
{
super();
}
/**
* Creates a grid layout with the specified number of rows and columns. All components in the
* layout are given equal size.
* <p>
* One, but not both, of <code>rows</code> and <code>cols</code> can be zero, which means that any
* number of objects can be placed in a row or in a column.
*
* @param rows the rows, with the value zero meaning any number of rows.
* @param cols the columns, with the value zero meaning any number of columns.
*/
public VerticalGridLayout( int rows, int cols )
{
super( rows, cols );
}
/**
* Creates a grid layout with the specified number of rows and columns. All components in the
* layout are given equal size.
* <p>
* In addition, the horizontal and vertical gaps are set to the specified values. Horizontal gaps
* are placed between each of the columns. Vertical gaps are placed between each of the rows.
* <p>
* One, but not both, of <code>rows</code> and <code>cols</code> can be zero, which means that any
* number of objects can be placed in a row or in a column.
* <p>
* All <code>GridLayout</code> constructors defer to this one.
*
* @param rows the rows, with the value zero meaning any number of rows
* @param cols the columns, with the value zero meaning any number of columns
* @param hgap the horizontal gap
* @param vgap the vertical gap
* @exception IllegalArgumentException if the value of both <code>rows</code> and
* <code>cols</code> is set to zero
*/
public VerticalGridLayout( int rows, int cols, int hgap, int vgap )
{
super( rows, cols, hgap, vgap );
}
@Override
public void layoutContainer( Container parent )
{
synchronized ( parent.getTreeLock() )
{
Insets insets = parent.getInsets();
int ncomponents = parent.getComponentCount();
int nrows = getRows();
int ncols = getColumns();
boolean ltr = parent.getComponentOrientation().isLeftToRight();
if ( ncomponents == 0 )
{
return;
}
if ( nrows > 0 )
{
ncols = (ncomponents + nrows - 1) / nrows;
}
else
{
nrows = (ncomponents + ncols - 1) / ncols;
}
// 4370316. To position components in the center we should:
// 1. get an amount of extra space within Container
// 2. incorporate half of that value to the left/top position
// Note that we use trancating division for widthOnComponent
// The reminder goes to extraWidthAvailable
int totalGapsWidth = (ncols - 1) * getHgap();
int widthWOInsets = parent.getWidth() - (insets.left + insets.right);
int widthOnComponent = (widthWOInsets - totalGapsWidth) / ncols;
int extraWidthAvailable = (widthWOInsets - (widthOnComponent * ncols + totalGapsWidth)) / 2;
int totalGapsHeight = (nrows - 1) * getVgap();
int heightWOInsets = parent.getHeight() - (insets.top + insets.bottom);
int heightOnComponent = (heightWOInsets - totalGapsHeight) / nrows;
int extraHeightAvailable = (heightWOInsets - (heightOnComponent * nrows + totalGapsHeight)) / 2;
if ( ltr )
{
for ( int c = 0, x = insets.left + extraWidthAvailable; c < ncols; c++, x += widthOnComponent
+ getHgap() )
{
for ( int r = 0, y = insets.top + extraHeightAvailable; r < nrows; r++, y += heightOnComponent
+ getVgap() )
{
int i = c * nrows + r; // In the horizontal version, this is: r * ncols + c
if ( i < ncomponents )
{
parent.getComponent( i ).setBounds( x, y, widthOnComponent, heightOnComponent );
}
}
}
}
else
{
for ( int c = 0, x = (parent.getWidth() - insets.right - widthOnComponent)
- extraWidthAvailable; c < ncols; c++, x -= widthOnComponent + getHgap() )
{
for ( int r = 0, y = insets.top + extraHeightAvailable; r < nrows; r++, y += heightOnComponent
+ getVgap() )
{
int i = c * nrows + r; // In the horizontal version, this is: r * ncols + c
if ( i < ncomponents )
{
parent.getComponent( i ).setBounds( x, y, widthOnComponent, heightOnComponent );
}
}
}
}
}
}
}
根据JavaDoc,定向主要用于特定的语言环境,因此您自己无法轻松创建实例。您是否总是希望有2列(或8行),或者如果空间可用,是否可以有更多?查看JDK 8源GridLayout似乎只区分ltr和rtl方向,这样它就可以自己进行垂直布局。您应该能够手动更改组件的顺序,即按顺序1、9、2、10等添加它们。我正在动态显示标签,左栏中的标签不会更改,但右栏中的标签会更改,我使用循环来执行此操作。我可以使用两个面板和8个单列,但我有点惊讶于在这种情况下缺乏简单的功能。奇怪的是,我必须是蒙古人或其他什么人才能让它发挥作用。。英雄联盟嗯,内置的布局管理器不是最好的;)根据我的经验,很多开发人员都使用第三方库,比如miglayet。我会去看看。谢谢你的提示。