Swing/MenuItemAcceleratorFont のバックアップ差分(No.4)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/MenuItemAcceleratorFont へ行く。
- 1 (2023-11-13 (月) 06:20:17)
- 2 (2023-11-13 (月) 11:52:47)
- 3 (2023-11-17 (金) 23:06:27)
- 4 (2023-11-18 (土) 20:09:35)
- 5 (2023-11-24 (金) 16:00:39)
- 追加された行はこの色です。
- 削除された行はこの色です。
--- category: swing folder: MenuItemAcceleratorFont title: JMenuItemのAccelerator表示を変更する tags: [JMenuItem, UIManager, Font, JCheckBoxMenuItem, JRadioButtonMenuItem] author: aterai pubdate: 2023-11-13T06:14:06+09:00 description: JMenuItemに設定されたAcceleratorの文字サイズや色を変更します。 image: https://drive.google.com/uc?id=1yw38bWwIYd05dmP11IRoVcc-YF740hoh --- * 概要 [#summary] `JMenuItem`に設定された`Accelerator`の文字サイズや色を変更します。 #download(https://drive.google.com/uc?id=1yw38bWwIYd05dmP11IRoVcc-YF740hoh) * サンプルコード [#sourcecode] #code(link){{ Color color1; Color color2; Font font; List<String> list = Arrays.asList( "MenuItem", "CheckBoxMenuItem", "RadioButtonMenuItem"); for (String prefix : list) { String key1 = prefix + ".acceleratorForeground"; String key2 = prefix + ".acceleratorSelectionForeground"; String key3 = prefix + ".acceleratorFont"; if (selected) { color1 = AFC; color2 = Color.WHITE; font = UIManager.getFont(key3).deriveFont(10f); font = UIManager.getFont(key3); if (font != null) { font = font.deriveFont(10f); } } else { UIDefaults def = UIManager.getLookAndFeelDefaults(); color1 = def.getColor(key1); color2 = def.getColor(key2); font = def.getFont(key3); } UIManager.put(key1, color1); UIManager.put(key2, color2); UIManager.put(key3, font); } SwingUtilities.updateComponentTreeUI(popup); }} * 解説 [#explanation] - `UIManager.put("MenuItem.acceleratorForeground", color)` -- `JMenuItem#setAccelerator(KeyStroke)`で設定した`accelerator`の文字色を変更 -- `MetalLookAndFeel`, `WindowsLookAndFeel`などで有効、`NimbusLookAndFeel`などでは無効 -- `JCheckBoxMenuItem`, `JRadioButtonMenuItem`には影響しないため、別途`CheckBoxMenuItem.acceleratorForeground`などで設定する必要がある -- `BasicLookAndFeel`で`Menu.acceleratorForeground`, `Menu.acceleratorSelectionForeground`, `Menu.acceleratorSelectionFont`が設定されているが、`JMenu`に`accelerator`を設定しても実行時に`java.lang.Error: setAccelerator() is not defined for JMenu. Use setMnemonic() instead.`とエラーになるので無意味 --- [https://docs.oracle.com/javase/jp/8/docs/api/javax/swing/JMenu.html#setAccelerator-javax.swing.KeyStroke- JMenu#setAccelerator(KeyStroke) (Java Platform SE 8)] - `UIManager.put("MenuItem.acceleratorSelectionForeground", color)` -- `accelerator`の選択文字色を変更 - `UIManager.put("MenuItem.acceleratorSelectionFont", font)` -- `accelerator`の`Font`を変更 -- このサンプルでは`Font`サイズを`Font#deriveFont(10f)`で縮小している * 参考リンク [#reference] - [[JMenuItemのAccelerator表示を右揃えにする>Swing/MenuItemAcceleratorAlignment]] * コメント [#comment] #comment #comment