Swing/InsertComponentBaseline のバックアップ(No.4)
- バックアップ一覧
- 差分 を表示
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- Swing/InsertComponentBaseline へ行く。
- 1 (2012-09-03 (月) 06:06:39)
- 2 (2012-12-07 (金) 16:50:09)
- 3 (2012-12-25 (火) 18:20:51)
- 4 (2013-01-28 (月) 02:08:18)
- 5 (2015-02-25 (水) 17:38:04)
- 6 (2016-05-27 (金) 15:36:14)
- 7 (2017-04-04 (火) 14:13:45)
- 8 (2017-04-07 (金) 13:51:51)
- 9 (2017-08-12 (土) 21:48:41)
- 10 (2018-08-10 (金) 16:18:21)
- 11 (2019-06-25 (火) 15:57:13)
- 12 (2021-03-02 (火) 14:37:13)
- 13 (2022-07-21 (木) 08:40:08)
- 14 (2025-01-03 (金) 08:57:02)
- 15 (2025-01-03 (金) 09:01:23)
- 16 (2025-01-03 (金) 09:02:38)
- 17 (2025-01-03 (金) 09:03:21)
- 18 (2025-01-03 (金) 09:04:02)
- 19 (2025-06-19 (木) 12:41:37)
- 20 (2025-06-19 (木) 12:43:47)
TITLE:JTextPaneに追加するコンポーネントのベースラインを揃える
Posted by aterai at 2012-09-03
JTextPaneに追加するコンポーネントのベースラインを揃える
`JTextPane
`に追加するコンポーネントのベースラインが他の文字列などとを揃うように設定します。
- &jnlp;
- &jar;
- &zip;
サンプルコード
JCheckBox check1 = new JCheckBox("JComponent.setAlignmentY(...)");
Dimension d = check1.getPreferredSize();
int baseline = check1.getBaseline(d.width, d.height);
check1.setAlignmentY(baseline/(float)d.height);
textPane.replaceSelection("\n\n Baseline: ");
textPane.insertComponent(check1);
View in GitHub: Java, Kotlin解説
- 上: `
Default
`- `
JTextPane#insertComponent(...)
で、
JCheckBox
`を追加 - `
JCheckBox
のデフォルトの
AlignmentY
は
0.5
`なのでテキストのベースラインと揃わない
- `
- 中: `
JComponent#setAlignmentY(...)
`- `
JComponent#getBaseline()
でベースラインを取得し、
JComponent#setAlignmentY(baseline/(float)d.height)
`でテキストベースラインの相対位置に配置
- `
- 下: `
setAlignmentY+setCursor+...
`- 「中: `
JComponent#setAlignmentY(...)
」+
Cursor+Opaque+Focusable
`を設定
- 「中: `
check2.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
check2.setOpaque(false);
check2.setFocusable(false);
参考リンク
- JTextPane#insertComponent(java.awt.Component) (Java Platform SE 6)
- 引用: コンポーネントは、`
Component.getAlignmentY
によって返された値に従って、テキストベースラインに相対的に配置されます。
Swing
コンポーネントの場合、
JComponent.setAlignmentY
メソッドを使うと、この値を簡単に設定できます。たとえば、値を
0.75
`に設定すると、コンポーネントの 75 パーセントがベースラインの上に、25 パーセントがベースラインの下になります。
- 引用: コンポーネントは、`
- java - How to appropriately adding JLabel to JEditorPane? - Stack Overflow