#navi(../)
*JButtonなどの高さを変更せずに幅を指定 [#d4f459ec]
>編集者:[[Terai Atsuhiro>terai]]~
作成日:2004-11-29~
更新日:&lastmod;

#contents
**概要 [#r2cfb19e]
高さはデフォルトのまま幅だけを指定して、JButton、JComboBox、JTextFieldなどのサイズを変更します。

http://terai.xrea.jp/swing/buttonwidth/screenshot.png

**サンプルコード [#xbca7694]
 Dimension dim = button1.getPreferredSize();
 button1.setPreferredSize(new Dimension(100, dim.height));
 button2.setPreferredSize(new Dimension(100, dim.height));
 Box box1 = Box.createHorizontalBox();
 box1.add(Box.createHorizontalGlue());
 box1.add(button1);
 box1.add(Box.createHorizontalStrut(5));
 box1.add(button2);
 box1.add(Box.createHorizontalStrut(5));
 box1.add(Box.createRigidArea(new Dimension(0, dim.height+10)));

-[[サンプルを起動>http://terai.xrea.jp/swing/buttonwidth/sample.jnlp]]
-[[jarファイル>http://terai.xrea.jp/swing/buttonwidth/sample.jar]]
-[[ソース>http://terai.xrea.jp/swing/buttonwidth/src.zip]]

**解説 [#v8878c59]
上記のサンプルでは、pack()する前なのでgetSize()ではなく、getPreferredSize()でボタンのデフォルトサイズから高さを取得し、BoxLayoutを使用してこれを水平方向に並べています。

サンプルの下段のようにJButtonの幅を一定にそろえて水平に並べたい場合や、GridBugLayoutでウエイトを指定するのが面倒といった場合に使用します。

//**参考リンク
**コメント [#c408da48]
#comment