Quantcast
Channel: プログラム の個人的なメモ
Viewing all articles
Browse latest Browse all 860

【Java】【Swing】 レイアウト [4] ~ BoxLayout 編 - [2] ~

$
0
0

はじめに

http://blogs.yahoo.co.jp/dk521123/36553511.html
で、BoxLayout を行ったが、
「Box.createVerticalBox()」「Box.createHorizontalBox()」を使う方法もあるので
メモる。

サンプル

ボタン(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("BorderLayout 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.createHorizontalGlue()
http://www.javadrive.jp/tutorial/boxlayout/index7.html

関連記事

【Java】【Swing】 レイアウト [1] ~ 基本編 ~

http://blogs.yahoo.co.jp/dk521123/36548731.html

【Java】【Swing】 レイアウト [2] ~ GroupLayout編 ~

http://blogs.yahoo.co.jp/dk521123/36548707.html

【Java】【Swing】 レイアウト [3] ~ GridBagLayout 編 ~

http://blogs.yahoo.co.jp/dk521123/36548823.html

【Java】【Swing】 レイアウト [4] ~ BoxLayout 編 - [1] ~

http://blogs.yahoo.co.jp/dk521123/36553511.html

Viewing all articles
Browse latest Browse all 860

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>