---
category: swing
folder: BrickLayout
title: GridBagLayoutを使ってレンガ状に配置
tags: [GridBagLayout, LayoutManager]
author: aterai
pubdate: 2009-03-09T14:08:29+09:00
description: GridBagLayoutを使ってコンポーネントをレンガ状に配置します。
image: https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTIOzg1doI/AAAAAAAAASc/V_SwABvAldE/s800/BrickLayout.png
---
* Summary [#summary]
`GridBagLayout`を使ってコンポーネントをレンガ状に配置します。[https://community.oracle.com/thread/1357310 Swing - GridBagLayout to create a board]を参考にしています。
#download(https://lh5.googleusercontent.com/_9Z4BYR88imo/TQTIOzg1doI/AAAAAAAAASc/V_SwABvAldE/s800/BrickLayout.png)
* Source Code Examples [#sourcecode]
#code(link){{
JPanel panel = new JPanel(new GridBagLayout());
panel.setBorder(BorderFactory.createTitledBorder("Brick Layout"));
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
c.gridy = GridBagConstraints.RELATIVE;
for (int y = 0; y < YSIZE; y++) {
// // c.gridy = GridBagConstraints.RELATIVE; // c.gridy = y;
// int d = y & 0b1; // = y % 2 == 0 ? 0 : 1; // start x offset
// if (d == 1) {
// c.gridwidth = 1;
// c.gridx = 0;
// panel.add(new JButton("a"), c);
// }
c.gridx = y & 0b1; // start x offset
c.gridwidth = WIDTH;
for (int x = 0; x < XSIZE; x++) {
panel.add(new JButton(" "), c);
c.gridx += WIDTH;
}
// if (d == 0) {
// c.gridwidth = 1;
// panel.add(new JButton("c"), c);
// }
}
// GridBagLayout to create a board
// https://community.oracle.com/thread/1357310
// <guide-row>
c.gridwidth = 1;
// c.gridy = GridBagConstraints.REMAINDER;
for (c.gridx = 0; c.gridx <= WIDTH * XSIZE; c.gridx++) {
panel.add(Box.createHorizontalStrut(24), c);
}
// </guide-row>
}}
* Description [#explanation]
* Description [#description]
上記のサンプルでは、`GridBagLayout`を使って`JButton`をレンガ状に配置します。互い違いに`2`列ずつ占めるようにボタンを配置していますが、`<guide-row>`が無い場合正しくレンガ状にはなりません。
以下、[https://community.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.
列の基準となるガイド行は、どこでも(先頭でも最後でも)構わないようです。
----
- 同様に指定した幅、かつ高さ`0`のセルで構成されたガイド行を作成して`JButton`をキーボード風に配置するサンプル
-- [[GridBagLayoutを使ってJButtonをキーボード状に配置する>Swing/KeyboardLayout]]に移動
* Reference [#reference]
- [https://community.oracle.com/thread/1357310 Swing - GridBagLayout to create a board]
- [[GridBagLayoutを使ってJButtonをキーボード状に配置する>Swing/KeyboardLayout]]
* Comment [#comment]
#comment
#comment