• 追加された行はこの色です。
  • 削除された行はこの色です。
TITLE:GridBagLayoutを使ってレンガ状に配置
#navi(../)
#tags(GridBagLayout, LayoutManager)
RIGHT:Posted by &author(aterai); at 2009-03-09
*GridBagLayoutを使ってレンガ状に配置 [#h2da3fd7]
``GridBagLayout``を使ってコンポーネントをレンガ状に配置します。[http://forums.oracle.com/forums/thread.jspa?messageID=5725196 Swing - GridBagLayout to create a board]を参考にしています。
* GridBagLayoutを使ってレンガ状に配置 [#h2da3fd7]
`GridBagLayout`を使ってコンポーネントをレンガ状に配置します。[https://forums.oracle.com/thread/1357310 Swing - GridBagLayout to create a board]を参考にしています。

-&jnlp;
-&jar;
-&zip;
#download
#ref(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTIOzg1doI/AAAAAAAAASc/V_SwABvAldE/s800/BrickLayout.png)

//#screenshot
#ref(http://lh5.ggpht.com/_9Z4BYR88imo/TQTIOzg1doI/AAAAAAAAASc/V_SwABvAldE/s800/BrickLayout.png)

**サンプルコード [#s089dab7]
** サンプルコード [#s089dab7]
#code(link){{
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createTitledBorder("Brick Layout"));
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
//c.weightx = 1.0; c.weighty = 0.0;
for(int i=0;i<SIZE;i++) {
  int x = i & 1; //= (i%2==0)?0:1;
  for(int j=0;j<SIZE;j++) {
    c.gridy = i;
    c.gridx = 2*j+x;
    c.gridwidth = 2;
    panel.add(new JButton(" "),c);
  }
}
//<blockquote cite="https://forums.oracle.com/forums/thread.jspa?threadID=1355310">
//<blockquote cite="https://forums.oracle.com/thread/1357310">
//<dummy-row>
c.gridwidth = 1;
c.gridy = 10;
for(c.gridx=0; c.gridx<=2*SIZE; c.gridx++)
  panel.add(Box.createHorizontalStrut(24), c);
//</dummy-row>
//</blockquote>
}}

**解説 [#lba85e7c]
上記のサンプルでは、``GridBagLayout``を使って、``JButton``をレンガ状に配置します。互い違いに二列ずつ占めるようにボタンを配置していますが、``<dummy-row>``が無い場合、うまくレンガ状にはなりません。
** 解説 [#lba85e7c]
上記のサンプルでは、`GridBagLayout`を使って、`JButton`をレンガ状に配置します。互い違いに二列ずつ占めるようにボタンを配置していますが、`<dummy-row>`が無い場合、うまくレンガ状にはなりません。

以下、[http://forums.oracle.com/forums/thread.jspa?messageID=5725196 Swing - GridBagLayout to create a board]のDarryl.Burkeさんの投稿を引用
以下、[https://forums.oracle.com/thread/1357310 Swing - GridBagLayout to create a board]のDarryl.Burkeさんの投稿を引用

 A column (or row) in a GridBagLayout is not well defined unless there is at least one component which occupies only that column (or row). All your rows have components spanning 2 columns.

列の基準となる行は、どこでも(先頭でも最後でも)構わないようです。

**参考リンク [#u6f8dce4]
-[http://forums.oracle.com/forums/thread.jspa?messageID=5725196 Swing - GridBagLayout to create a board]
** 参考リンク [#u6f8dce4]
- [https://forums.oracle.com/thread/1357310 Swing - GridBagLayout to create a board]

**コメント [#te0e3d58]
** コメント [#te0e3d58]
#comment