900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > Java左上到右下 java – 如何从上到下然后从左到右填充Gri...

Java左上到右下 java – 如何从上到下然后从左到右填充Gri...

时间:2024-06-26 04:22:24

相关推荐

Java左上到右下 java  – 如何从上到下然后从左到右填充Gri...

您可以扩展GridLayout并仅覆盖一个方法

而不是int i = r * ncols c; use int i = c * nrows r;我认为这就够了.

public void layoutContainer(Container parent) {

synchronized (parent.getTreeLock()) {

Insets insets = parent.getInsets();

int ncomponents = parent.getComponentCount();

int nrows = rows;

int ncols = cols;

boolean ltr = parent.getComponentOrientation().isLeftToRight();

if (ncomponents == 0) {

return;

}

if (nrows > 0) {

ncols = (ncomponents + nrows - 1) / nrows;

} else {

nrows = (ncomponents + ncols - 1) / ncols;

}

int w = parent.width - (insets.left + insets.right);

int h = parent.height - (insets.top + insets.bottom);

w = (w - (ncols - 1) * hgap) / ncols;

h = (h - (nrows - 1) * vgap) / nrows;

if (ltr) {

for (int c = 0, x = insets.left ; c < ncols ; c++, x += w + hgap) {

for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) {

int i = r * ncols + c;

if (i < ncomponents) {

parent.getComponent(i).setBounds(x, y, w, h);

}

}

}

} else {

for (int c = 0, x = parent.width - insets.right - w; c < ncols ; c++, x -= w + hgap) {

for (int r = 0, y = insets.top ; r < nrows ; r++, y += h + vgap) {

int i = r * ncols + c;

if (i < ncomponents) {

parent.getComponent(i).setBounds(x, y, w, h);

}

}

}

}

}

}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。