はじめに
http://blogs.yahoo.co.jp/dk521123/36553511.htmlで、BoxLayout を行ったが、 「Box.createVerticalBox()」「Box.createHorizontalBox()」を使う方法もあるので メモる。
Box の主なメソッド
* Box.createHorizontalBox() / Box.createVerticalBox() * Box.createHorizontalGlue() / Box.createVerticalGlue() * Box.createHorizontalStrut([Width]) / Box.createVerticalStrut([Height]) : 固定幅/高さのマージン
サンプル
ボタン(2×3)を並べる(イメージ図)+------------------------+ |+------++------++------+| || 1_1 || 1_2 || 1_3 || |+------++------++------+| |+------++------++------+| || 2_2 || 2_2 || 2_3 || |+------++------++------+| +------------------------+
BoxDemo.java
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class BoxDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("BoxLayout Demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
// 垂直なBox生成
Box box = Box.createVerticalBox();
panel.add(box);
// 水平なBox生成 - 1
Box childBox1 = Box.createHorizontalBox();
box.add(Box.createVerticalGlue());
box.add(childBox1);
JButton button1_1 = new JButton("button1_1");
JButton button1_2 = new JButton("button1_2");
JButton button1_3 = new JButton("button1_3");
childBox1.add(button1_1);
childBox1.add(Box.createHorizontalGlue());
childBox1.add(button1_2);
childBox1.add(Box.createHorizontalGlue());
childBox1.add(button1_3);
// 水平なBox生成 - 2
Box childBox2 = Box.createHorizontalBox();
box.add(Box.createVerticalGlue());
box.add(childBox2);
JButton button2_1 = new JButton("button2_1");
JButton button2_2 = new JButton("button2_2");
JButton button2_3 = new JButton("button2_3");
childBox2.add(button2_1);
childBox1.add(Box.createHorizontalGlue());
childBox2.add(button2_2);
childBox1.add(Box.createHorizontalGlue());
childBox2.add(button2_3);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
}
参考文献
Box.createHorizontalGlue() / Box.createVerticalGlue()http://www.javadrive.jp/tutorial/boxlayout/index7.html
Box.createHorizontalStrut([Width]) / Box.createVerticalStrut([Height])
https://sites.google.com/site/shin1ogawa/java/swing/fixed-margin