a のデフォルトの動作でGridLayout
は、コンポーネントは行ごとに、左から右に塗りつぶされます。コンポーネントが列で(左から右に)埋められるように使用できるのだろうか?ありがとう。
5 に答える
GridLayout を拡張し、int i = r * ncols + c;
使用する代わりに 1 つのメソッドだけをオーバーライドすることができます。それで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);
}
}
}
}
}
}
このような使用例は、GridLayout マネージャーではサポートされていません。
GridBagLayout
代わりに をご覧になることをお勧めします。これにより、GridBagConstraints.gridx
とで場所を設定できますGridBagConstraints.gridy
。
(同様の動作を得るにはGridLayout
、重みと塗りつぶしを適切に設定してください。)
単一のGridLayout
. GridLayout
ただし、各セルが複数の行を持つ 1 つの列を持つ 1 つの行を持つことができGridLayout
ます。別の LayoutManager を使用するTableLayout
方が簡単な選択かもしれませんが。
MigLayoutを試すことをお勧めします。次の方法で流れの方向を切り替えることができます。
setLayout(new MigLayout("flowy"));
add(component1);
add(component2);
add(component3, "wrap");
add(component4);
add(component5);
add(component6);
MigLayout を使用してこれを実現する方法はたくさんありますが、GridBagLayout よりもはるかに使いやすく、それ以上ではないにしても、同等の機能を備えていると思います。BorderLayout、FlowLayout、BoxLayout などはもう必要ありません。MigLayout はそれもすべて行います。