Swing/RightAlignComponentBorder のバックアップの現在との差分(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/RightAlignComponentBorder へ行く。
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:Borderの右下にJComponentを配置 #navi(../) #tags(Border, SpringLayout, JLayeredPane, JComponent) RIGHT:Posted by &author(aterai); at 2011-11-21 *Borderの右下にJComponentを配置 [#of54ef15] ``SpringLayout``を設定した``JLayeredPane``を使って、``Border``の右下に``JComponent``を配置します。 --- category: swing folder: RightAlignComponentBorder title: Borderの右下にJComponentを配置 tags: [Border, SpringLayout, JLayeredPane, JComponent] author: aterai pubdate: 2011-11-21T15:47:08+09:00 description: SpringLayoutを設定したJLayeredPaneを使って、Borderの右下にJComponentを配置します。 image: https://lh5.googleusercontent.com/-IPUQvbcJ2NM/TsjbIEMsMeI/AAAAAAAABE8/Fg16E6wkLtY/s800/RightAlignComponentBorder.png --- * 概要 [#summary] `SpringLayout`を設定した`JLayeredPane`を使って、`Border`の右下に`JComponent`を配置します。 -&jnlp; -&jar; -&zip; #download(https://lh5.googleusercontent.com/-IPUQvbcJ2NM/TsjbIEMsMeI/AAAAAAAABE8/Fg16E6wkLtY/s800/RightAlignComponentBorder.png) //#screenshot #ref(https://lh5.googleusercontent.com/-IPUQvbcJ2NM/TsjbIEMsMeI/AAAAAAAABE8/Fg16E6wkLtY/s800/RightAlignComponentBorder.png) **サンプルコード [#y64f7862] * サンプルコード [#sourcecode] #code(link){{ public JComponent makePanel(JComponent m, JComponent c) { int ir = 20; //inset.right int ch = c.getPreferredSize().height/2; int ir = 20; // inset.right int ch = c.getPreferredSize().height / 2; Border ib = BorderFactory.createEmptyBorder(0, 0, ch, 0); Border eb = BorderFactory.createEtchedBorder(); Border bo = BorderFactory.createCompoundBorder(eb, ib); m.setBorder(BorderFactory.createCompoundBorder(ib, bo)); SpringLayout layout = new SpringLayout(); JLayeredPane p = new JLayeredPane(); p.setLayout(layout); Spring x = layout.getConstraint(SpringLayout.WIDTH, p); Spring y = layout.getConstraint(SpringLayout.HEIGHT, p); Spring g = Spring.minus(Spring.constant(ir)); Spring x = layout.getConstraint(SpringLayout.WIDTH, p); Spring y = layout.getConstraint(SpringLayout.HEIGHT, p); Spring g = Spring.minus(Spring.constant(ir)); SpringLayout.Constraints constraints = layout.getConstraints(c); constraints.setConstraint(SpringLayout.EAST, Spring.sum(x, g)); constraints.setConstraint(SpringLayout.SOUTH, y); p.setLayer(c, JLayeredPane.DEFAULT_LAYER+1); p.add(c); constraints = layout.getConstraints(m); constraints.setConstraint(SpringLayout.WEST, Spring.constant(0)); constraints.setConstraint(SpringLayout.NORTH, Spring.constant(0)); constraints.setConstraint(SpringLayout.EAST, x); constraints.setConstraint(SpringLayout.SOUTH, y); p.setLayer(m, JLayeredPane.DEFAULT_LAYER); p.add(m); return p; } }} **解説 [#e226ea75] - 中央に表示するコンポーネントに、右下に配置するコンポーネントと同じ高さの``EtchedBorder``を設定 - ``SpringLayout``を設定した``JLayeredPane``の``DEFAULT_LAYER``に中央に表示するコンポーネント、``DEFAULT_LAYER+1``に右下に配置するコンポーネントを追加 - ``SpringLayout.Constraints``を設定して、中央に表示するコンポーネントは親の``JLayeredPane``のサイズとおなじになるように、右下に配置するコンポーネントは右下になるようにレイアウト -- 右下に配置するコンポーネントと親の``JLayeredPane``の右端同士は、固定で20px * 解説 [#explanation] - 中央に表示するコンポーネントに右下に配置するコンポーネントと同じ高さの`EtchedBorder`を設定 - `SpringLayout`を設定した`JLayeredPane`の`DEFAULT_LAYER`上に中央に表示するコンポーネントを追加 -- `DEFAULT_LAYER + 1`上に右下に配置するコンポーネントを追加 - `SpringLayout.Constraints`を設定して中央に表示するコンポーネントは親の`JLayeredPane`のサイズとおなじになるようにレイアウト -- 右下に配置するコンポーネントは右下になるようにレイアウト -- 右下に配置するコンポーネントと親の`JLayeredPane`の右端同士は固定で`20px` **参考リンク [#s210324a] -[[BorderにJComponentを配置>Swing/ComponentTitledBorder]] -[[SpringLayoutの使用>Swing/SpringLayout]] * 参考リンク [#reference] - [[BorderにJComponentを配置>Swing/ComponentTitledBorder]] - [[SpringLayoutの使用>Swing/SpringLayout]] **コメント [#md6b0ea4] * コメント [#comment] #comment #comment