BorderLayout と FlowLayout の組み合わせで改行しても高さが増えない
http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=5940&forum=12
とかで情報があったので助かった
public Dimension preferredLayoutSize(Container target) { synchronized (target.getTreeLock()) { Dimension dim = new Dimension(0, 0); int nmembers = target.getComponentCount(); boolean firstVisibleComponent = true; for (int i = 0 ; i < nmembers ; i++) { Component m = target.getComponent(i); if (m.visible) { Dimension d = m.getPreferredSize(); dim.height = Math.max(dim.height, d.height); if (firstVisibleComponent) { firstVisibleComponent = false; } else { dim.width += hgap; } dim.width += d.width; } } Insets insets = target.getInsets(); dim.width += insets.left + insets.right + hgap*2; dim.height += insets.top + insets.bottom + vgap*2; return dim; } }
を参考に適当にオーバーライト
FlowLayout flowlayout = new FlowLayout(){ public Dimension preferredLayoutSize(Container target) { synchronized (target.getTreeLock()) { int hgap = getHgap(); int vgap = getVgap(); int seprWidth = width;//ウィンドウの横幅 int tempWidth = 0; int tempHeight = 0; int totalHeight = 0; int lineCount = 1; for (int i = 0, i_n = target.getComponentCount(); i < i_n; i++) { Component m = target.getComponent(i); Dimension d = m.getPreferredSize(); tempWidth += hgap; if(tempWidth + d.width > seprWidth){ //次の行へ totalHeight += tempHeight; tempHeight = 0; tempWidth = 0; lineCount++; } tempHeight = Math.max(tempHeight, d.height); tempWidth += d.width; } totalHeight += tempHeight + (lineCount+1)*vgap; return new Dimension(seprWidth, totalHeight); } } };
これでウィンドウ幅で折り返して高さを返すかんじになる
常にm.visibleであることを想定してるので必要ならその辺は適当に変更する必要あり
しっかしJavaはまだまだわからんことだらけだ