GridLayout
的默認行爲是組件按行,從左到右填充。我想知道是否可以使用它,以便組件由列(從左到右)填充?謝謝。如何從頂部到底部然後從左到右填充GridLayout?
4
A
回答
5
您可以擴展GridLayout並覆蓋只有一個方法 而不是int i = r * ncols + c;
使用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);
}
}
}
}
}
}
5
GridLayout管理器不支持這種用例。
我建議你看看GridBagLayout
而不是,它允許你通過GridBagConstraints.gridx
和GridBagConstraints.gridy
來設置位置。
(要獲得類似GridLayout
行爲一定要設置的權重,並填寫正確。)
2
你不能用一個單一的GridLayout
實現這一目標。但是,您可以在一行中有一個GridLayout
,每個單元具有包含多行的單列的GridLayout
。雖然使用不同的LayoutManager如TableLayout
可能是一個更容易的選擇。
1
我建議你試試MigLayout。您可以切換流動方向:
setLayout(new MigLayout("flowy"));
add(component1);
add(component2);
add(component3, "wrap");
add(component4);
add(component5);
add(component6);
這裏有很多方式與MigLayout來實現這一點,我覺得這麼多友好比GridBagLayout的使用,只是爲能夠,如果不是更多的話。您不再需要BorderLayout,FlowLayout,BoxLayout等,MigLayout也可以做到這一點。
相關問題
- 1. 從底部和從右到左的順序填充內容
- 2. 如何從底部到頂部和從右到左閱讀文件?
- 3. 帶有CSS的條形圖,如何從底部到頂部填充,而不是從頂部到底部?
- 4. 滾動頂部,然後底部,然後頂部,然後底部
- 5. 排序矢量<Points>從頂部到底部,然後從左到右使用STL在c + +
- 6. 關閉一個UIViewController從底部到頂部而不是從右到左
- 7. LinearLayout從右到左填充
- 8. 從左到右填充Div
- 9. 從左到右的邊框底部
- 10. XPATH - 從底部到頂部
- 11. jquery動畫從右到左頂部
- 12. jQuery Animate頂部(從底部到頂部)
- 13. 如何在iPad上從左側,右側到頂部,底部更改SplitView?
- 14. 如何從底部循環到頂部
- 15. 如何從android.support.v7.widget.Toolbar中刪除頂部和底部填充?
- 16. 動畫從底部到頂部而不是從頂部到底部的高度
- 17. JQUERY - 改變股利尺寸,從頂部左側/右側或底部左/右
- 18. 從頂部填充項目到ListView?
- 19. ListView從底部而不是從頂部填充
- 20. 如何從上到下填充datagridview,從左到右?
- 21. 如何從左到右填充DataGridView,而不是從上到下
- 22. 從頂部和底部以及從底部到頂部拉伸div高度
- 23. 動畫文字填充從左到右
- 24. 從左到右動畫SVG填充
- 25. 如何從頂部到底部減少元素的高度,而不是從默認的底部到頂部?
- 26. 頂部/底部填充的微調
- 27. 填充頂部和底部ErrorProvider控件
- 28. 花式框填充(頂部和底部)
- 29. 如何動畫CALayer的填充顏色,因爲它在iPhone中從底部到頂部填充顏色sdk
- 30. BackGradientStyle從MSChart的底部到頂部?
我認爲這是一個可怕的想法,除非你對'preferredLayoutSize','minimumLayoutSize'等做同樣的事情。 – aioobe
我不這麼認爲。如果你看一下計算,你可以看到它們只是計算組件的最大尺寸並相應地在行/列上相乘。 – StanislavL
啊,好點! +1給你。 :-) – aioobe