Swing/BrickLayout のバックアップの現在との差分(No.16)
- category: swing folder: BrickLayout title: GridBagLayoutを使ってレンガ状に配置 tags: [GridBagLayout, LayoutManager] author: aterai pubdate: 2009-03-09T14:08:29+09:00 description: GridBagLayoutを使ってコンポーネントをレンガ状に配置します。 image:
概要
GridBagLayout
を使ってコンポーネントをレンガ状に配置します。Swing - GridBagLayout to create a boardを参考にしています。
Screenshot
Advertisement
サンプルコード
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.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);
//}
// if (d == 0) {
// c.gridwidth = 1;
// panel.add(new JButton("c"), c);
// }
}
#spandel
//GridBagLayout to create a board
#spanend
#spandel
//https://community.oracle.com/thread/1357310
#spanend
#spandel
//<dummy-row>
#spanend
#spanadd
// GridBagLayout to create a board
#spanend
#spanadd
// https://community.oracle.com/thread/1357310
#spanend
#spanadd
// <guide-row>
#spanend
c.gridwidth = 1;
#spandel
//c.gridy = GridBagConstraints.REMAINDER;
#spanend
#spanadd
// c.gridy = GridBagConstraints.REMAINDER;
#spanend
for (c.gridx = 0; c.gridx <= WIDTH * XSIZE; c.gridx++) {
panel.add(Box.createHorizontalStrut(24), c);
}
#spandel
//</dummy-row>
#spanend
#spanadd
// </guide-row>
#spanend
View in GitHub: Java, Kotlin解説
上記のサンプルでは、GridBagLayout
を使って、JButton
をレンガ状に配置します。互い違いに二列ずつ占めるようにボタンを配置していますが、<dummy-row>
が無い場合、うまくレンガ状にはなりません。
上記のサンプルでは、GridBagLayout
を使ってJButton
をレンガ状に配置します。互い違いに2
列ずつ占めるようにボタンを配置していますが、<guide-row>
が無い場合正しくレンガ状にはなりません。
以下、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
をキーボード風に配置するサンプル
#spanend
#spandel
import java.awt.*;
#spanend
#spandel
import java.awt.event.*;
#spanend
#spandel
import javax.swing.*;
#spanend
#spandel
#spanend
#spandel
public class KeyboardTest {
#spanend
private static final String[][] keys = {
{"`", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0", "-", "=", "Backspace"},
{"Tab", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "[", "]", "\\", ""},
{"Caps", "A", "S", "D", "F", "G", "H", "J", "K", "L", ";", "'", "Enter", ""},
{"Shift", "Z", "X", "C", "V", "B", "N", "M", ",", ".", "/", "", "\u2191"},
{" ", " ", " ", "", " ", " ", "\u2190", "\u2193", "\u2192"}
};
public JComponent makeUI() {
JPanel keyboard = new JPanel(new GridBagLayout());
keyboard.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
#spandel
#spanend
GridBagConstraints c = new GridBagConstraints();
c.weightx = 1d;
c.weighty = 1d;
c.fill = GridBagConstraints.BOTH;
c.gridheight = 1;
c.gridx = 0;
#spandel
#spanend
c.gridy = 50;
for (int i = 0; i < keys[0].length * 2; i++) {
c.gridx = i;
keyboard.add(Box.createHorizontalStrut(24));
}
#spandel
#spanend
for (int row = 0; row < keys.length; row++) {
c.gridx = 0;
c.gridy = row;
for (int col = 0; col < keys[row].length; col++) {
String key = keys[row][col];
int l = key.length();
c.gridwidth = l > 10 ? 14 :
l > 4 ? 4 :
l > 1 ? 3 :
l == 1 ? 2 : 1;
if (l > 2) {
keyboard.add(new JButton(key), c);
} else if (key.trim().length() == 0) {
keyboard.add(Box.createHorizontalStrut(24), c);
} else {
keyboard.add(new KeyButton(key), c);
}
c.gridx += c.gridwidth;
}
}
return keyboard;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
@Override public void run() {
createAndShowGUI();
}
});
}
public static void createAndShowGUI() {
JFrame f = new JFrame("Keyboard");
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new KeyboardTest().makeUI());
f.setResizable(false);
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}
#spandel
}
#spanend
#spandel
#spanend
#spandel
class KeyButton extends JButton {
#spanend
public KeyButton(String str) {
super(str);
}
@Override public Dimension getPreferredSize() {
return new Dimension(48, 48);
}
#spandel
}
#spanend
#spandel