Swing/SpringLayout のバックアップの現在との差分(No.9)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/SpringLayout へ行く。
- 1 (2004-06-02 (水) 10:00:16)
- 2 (2004-09-08 (水) 09:44:17)
- 3 (2004-10-08 (金) 06:25:28)
- 4 (2004-11-04 (木) 10:11:41)
- 5 (2005-04-28 (木) 04:33:00)
- 6 (2005-05-10 (火) 05:35:19)
- 7 (2005-06-27 (月) 06:53:37)
- 8 (2005-07-06 (水) 23:54:35)
- 9 (2005-10-16 (日) 16:35:52)
- 10 (2006-02-27 (月) 16:28:56)
- 11 (2006-06-15 (木) 19:35:23)
- 12 (2006-10-23 (月) 15:43:37)
- 13 (2007-07-13 (金) 19:50:08)
- 14 (2007-10-24 (水) 00:33:37)
- 15 (2010-09-02 (木) 02:41:13)
- 16 (2010-09-02 (木) 07:01:18)
- 17 (2013-02-20 (水) 15:26:59)
- 18 (2013-04-05 (金) 17:59:33)
- 19 (2013-04-17 (水) 01:34:15)
- 20 (2013-08-17 (土) 01:11:40)
- 21 (2014-09-17 (水) 13:18:28)
- 22 (2014-10-23 (木) 00:56:02)
- 23 (2015-03-31 (火) 21:05:41)
- 24 (2016-05-24 (火) 22:25:15)
- 25 (2016-09-02 (金) 12:25:29)
- 26 (2017-03-30 (木) 14:07:31)
- 27 (2018-03-07 (水) 19:08:35)
- 28 (2020-03-11 (水) 19:23:51)
- 29 (2021-08-20 (金) 14:02:05)
- 追加された行はこの色です。
- 削除された行はこの色です。
#navi(../) *SpringLayoutでリスト状に並べる [#nc4396b2] >編集者:[[Terai Atsuhiro>terai]]~ 作成日:2004-03-22~ 更新日:&lastmod; --- category: swing folder: SpringLayout title: SpringLayoutの使用 tags: [SpringLayout, LayoutManager] author: aterai pubdate: 2004-03-22T10:23:19+09:00 description: SpringLayoutを使用して、各ラベルのサイズとパネルからの距離が一定の比率になるような配置を指定します。 image: https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTwX9UR-I/AAAAAAAAAk8/TLNZjmIrPnw/s800/SpringLayout.png --- * 概要 [#summary] `SpringLayout`を使用して、各ラベルのサイズとパネルからの距離が一定の比率になるような配置を指定します。 #contents **概要 [#k994c8ea] 高さの異なるコンポーネントをリスト状に並べてみます。 #download(https://lh6.googleusercontent.com/_9Z4BYR88imo/TQTTwX9UR-I/AAAAAAAAAk8/TLNZjmIrPnw/s800/SpringLayout.png) http://terai.xrea.jp/swing/springlayout/screenshot.png * サンプルコード [#sourcecode] #code(link){{ private static void setScaleAndAdd( JComponent parent, SpringLayout layout, JComponent child, float sx, float sy, float sw, float sh) { Spring panelw = layout.getConstraint(SpringLayout.WIDTH, parent); Spring panelh = layout.getConstraint(SpringLayout.HEIGHT, parent); **サンプルコード [#gefca96e] public void addComp(JComponent label) { SpringLayout layout = new SpringLayout(); Component[] list = pnl.getComponents(); if(list.length==0) { layout.putConstraint( SpringLayout.WEST,label,0,SpringLayout.WEST,pnl); layout.putConstraint( SpringLayout.NORTH,label,0,SpringLayout.NORTH,pnl); }else{ JComponent cmp = null; for(int i=0;i<list.length;i++) { JComponent tmp = (JComponent)list[i]; layout.putConstraint( SpringLayout.WEST,tmp,0,SpringLayout.WEST,pnl); if(cmp==null) { layout.putConstraint( SpringLayout.NORTH,tmp,0,SpringLayout.NORTH,pnl); }else{ layout.putConstraint( SpringLayout.NORTH,tmp,0,SpringLayout.SOUTH,cmp); } cmp = tmp; } layout.putConstraint( SpringLayout.WEST,label,0,SpringLayout.WEST,pnl); layout.putConstraint( SpringLayout.NORTH,label,0,SpringLayout.SOUTH,cmp); } pnl.add(label); pnl.setLayout(layout); initComps(); } SpringLayout.Constraints c = layout.getConstraints(child); c.setX(Spring.scale(panelw, sx)); c.setY(Spring.scale(panelh, sy)); c.setWidth(Spring.scale(panelw, sw)); c.setHeight(Spring.scale(panelh, sh)); public void initComps() { Rectangle rv = scroll.getViewport().getViewRect(); Insets ins = pnl.getInsets(); int cw = (int)rv.getWidth() - ins.left - ins.right; int ch = 0; Component[] list = pnl.getComponents(); for(int i=0;i<list.length;i++) { JComponent tmp = (JComponent)list[i]; int th = tmp.getPreferredSize().height; tmp.setPreferredSize(new Dimension(cw, th)); ch = ch + th; } Dimension d = new Dimension( (int)rv.getWidth(),ch+ins.top+ins.bottom); pnl.setPreferredSize(d); pnl.revalidate(); } parent.add(child); } -[[サンプルを起動>http://terai.xrea.jp/swing/springlayout/sample.jnlp]] -[[jarファイル>http://terai.xrea.jp/swing/springlayout/sample.jar]] -[[ソース>http://terai.xrea.jp/swing/springlayout/src.zip]] // public void initLayout() { // SpringLayout layout = new SpringLayout(); // Insets i = panel.getInsets(); // int w = panel.getWidth() - i.left - i.right; // int h = panel.getHeight() - i.top - i.bottom; // // l1.setPreferredSize(new Dimension(w * 90 / 100, h * 55 / 100)); // l2.setPreferredSize(new Dimension(w * 40 / 100, h * 30 / 100)); // // layout.putConstraint(SpringLayout.WEST, l1, w * 5 / 100, // SpringLayout.WEST, panel); // layout.putConstraint(SpringLayout.NORTH, l1, h * 5 / 100, // SpringLayout.NORTH, panel); // layout.putConstraint(SpringLayout.WEST, l2, w * 50 / 100, // SpringLayout.WEST, panel); // //layout.putConstraint(SpringLayout.WEST, l2, 0, SpringLayout.WEST, l1); // layout.putConstraint(SpringLayout.SOUTH, l2, -h * 5 / 100, // SpringLayout.SOUTH, panel); // // panel.setLayout(layout); // panel.revalidate(); // } }} **解説 [#b910d821] 上記のサンプルでは、SpringLayoutを使ってパネルにコンポーネントを並べています。右クリックのポップアップメニューから、高さの異なるラベルとボタンを追加することができます。追加されたこれらのコンポーネントの幅はJListのようにViewportに合わせて変化しますが、高さはsetPreferredSize()で設定されたものを維持します。 * 解説 [#explanation] 上記のサンプルでは、`SpringLayout`を使って`2`つの`JComponent`をパネル内にレイアウトしています。 チュートリアルの[[SpringForm>http://java.sun.com/docs/books/tutorial/uiswing/layout/example-1dot4/index.html#SpringForm]]にある[[SpringUtilities>http://java.sun.com/docs/books/tutorial/uiswing/layout/example-1dot4/SpringUtilities.java]]を使うとサンプルのaddCompメソッドは、以下のように書くこともできます。 public void addComp2(JComponent cmp) { pnl.add(cmp); Component[] list = pnl.getComponents(); SpringUtilities.makeCompactGrid(pnl, list.length, 1, //rows, cols 6, 6, //initX, initY 6, 6); //xPad, yPad initComps(); } %%パネルのサイズが変更されるたびに、各ラベルのサイズとパネルからの距離が一定の割合になるように設定し直しています(ただしパネルの余白は無視)。%% //**参考リンク **コメント [#eec7c049] - `JLabel` -- 幅はパネルの`90%`、高さは`55%`になるよう設定 -- 左上座標は親パネルの左上から`x: 5%`、`y: 5%`の位置 -- %%パネルと自身の`WEST`からの距離`5%`、パネルと自身の`NORTH`からの距離`5%`%% - `JButton` -- 幅はパネルの`40%`、高さは`30%`になるよう設定 -- 左上座標は、親パネルの左上から`x: 50%`、`y: 65%`の位置 -- %%パネルと自身の`WEST`からの距離`50%`、パネルと自身の`SOUTH`からの距離`-5%`%% * 参考リンク [#reference] - [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/SpringLayout.html SpringLayout (Java Platform SE 8)] * コメント [#comment] #comment - 以前の内容は、[[BoxLayoutでリスト状に並べる>Swing/ComponentList]]に移動しました。 -- &user(aterai); &new{2006-06-15 (木) 19:40:17}; - リスナーを使わなくても次ので出来ました。 -- &new{2010-09-02 (木) 02:41:13}; #code{{ panel.setLayout(layout); Spring panelw = layout.getConstraint(SpringLayout.WIDTH, panel); layout.getConstraints(name).setWidth(Spring.scale(panelw, .9f)); layout.getConstraints(name).setX(Spring.scale(panelw, .05f)); }} - ご指摘ありがとうございます。こんなメソッドが有ったのですね。サンプルを全体的に修正してみました。 -- &user(aterai); &new{2010-09-02 (木) 07:01:18}; #comment