6

a のデフォルトの動作でGridLayoutは、コンポーネントは行ごとに、左から右に塗りつぶされます。コンポーネントが列で(左から右に)埋められるように使用できるのだろうか?ありがとう。

4

5 に答える 5

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);
            }
        }
        }
    }
  }
}
于 2011-06-30T13:16:26.217 に答える
5

このような使用例は、GridLayout マネージャーではサポートされていません。

GridBagLayout代わりに をご覧になることをお勧めします。これにより、GridBagConstraints.gridxとで場所を設定できますGridBagConstraints.gridy

(同様の動作を得るにはGridLayout、重みと塗りつぶしを適切に設定してください。)

于 2011-06-30T12:36:56.477 に答える
2

単一のGridLayout. GridLayoutただし、各セルが複数の行を持つ 1 つの列を持つ 1 つの行を持つことができGridLayoutます。別の LayoutManager を使用するTableLayout方が簡単な選択かもしれませんが。

于 2011-06-30T12:39:30.660 に答える
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 はそれもすべて行います。

于 2011-06-30T19:15:06.990 に答える