Swing/TitledSeparator のバックアップ差分(No.8)
- バックアップ一覧
- 現在との差分 を表示
- 現在との差分 - Visual を表示
- ソース を表示
- バックアップ を表示
- Swing/TitledSeparator へ行く。
- 1 (2012-08-27 (月) 18:52:36)
- 2 (2012-09-07 (金) 09:50:54)
- 3 (2012-09-13 (木) 12:36:11)
- 4 (2012-09-13 (木) 14:04:43)
- 5 (2012-12-07 (金) 18:05:11)
- 6 (2013-08-17 (土) 00:53:39)
- 7 (2014-06-04 (水) 22:01:40)
- 8 (2014-10-29 (水) 01:41:59)
- 9 (2014-11-23 (日) 17:03:53)
- 10 (2016-01-06 (水) 21:32:56)
- 11 (2016-10-06 (木) 00:12:15)
- 12 (2017-11-10 (金) 14:12:14)
- 13 (2018-02-24 (土) 19:51:30)
- 14 (2019-07-04 (木) 19:22:06)
- 15 (2021-03-17 (水) 01:57:34)
- 16 (2024-02-10 (土) 17:48:42)
- 追加された行はこの色です。
- 削除された行はこの色です。
TITLE:TitledBorderとMatteBorderを使用してTitledSeparatorを作成する #navi(../) #tags(JSeparator, TitledBorder, MatteBorder, JLabel, Icon, LinearGradientPaint) RIGHT:Posted by &author(aterai); at 2012-08-27 * TitledBorderとMatteBorderを使用してTitledSeparatorを作成する [#ub2f11b2] --- title: TitledBorderとMatteBorderを使用してTitledSeparatorを作成する tags: [JSeparator, TitledBorder, MatteBorder, JLabel, Icon, LinearGradientPaint] author: aterai pubdate: 2012-08-27T18:52:36+09:00 description: TitledBorderとMatteBorderを使用してTitle付きのSeparatorを作成します。 --- * 概要 [#ub2f11b2] `TitledBorder`と`MatteBorder`を使用して`Title`付きの`Separator`を作成します。 #download #ref(https://lh3.googleusercontent.com/-sRtVayYL37Q/UDs_iiXRk7I/AAAAAAAABRk/71qZoe9vM60/s800/TitledSeparator.png) #download(https://lh3.googleusercontent.com/-sRtVayYL37Q/UDs_iiXRk7I/AAAAAAAABRk/71qZoe9vM60/s800/TitledSeparator.png) ** サンプルコード [#a13df73e] * サンプルコード [#a13df73e] #code(link){{ class TitledSeparator extends JLabel{ private Color color; private final String title; private final Color target; private final int height; private final int titlePosition; public TitledSeparator(String title, int height, int titlePosition) { this(title, null, height, titlePosition); } public TitledSeparator(String _title, Color _target, int _height, int _titlePosition) { super(); this.title = _title; this.target = _target; this.height = _height; this.titlePosition = _titlePosition; Icon icon = new Icon() { private int width = -1; private Paint painter1, painter2; @Override public void paintIcon(Component c, Graphics g, int x, int y) { int w = c.getWidth(); if(w!=width || painter1==null || painter2==null || color==null) { width = w; Point2D start = new Point2D.Float(0f, 0f); Point2D end = new Point2D.Float((float)width, 0f); float[] dist = {0.0f, 1.0f}; color = getBackground(); color = color==null ? UIManager.getColor("Panel.background") : color; Color tc = target==null ? color : target; painter1 = new LinearGradientPaint(start, end, dist, new Color[] {tc.darker(), color}); painter2 = new LinearGradientPaint(start, end, dist, new Color[] {tc.brighter(), color}); } int h = getIconHeight()/2; Graphics2D g2 = (Graphics2D)g.create(); g2.setPaint(painter1); g2.fillRect(x, y, width, getIconHeight()); g2.setPaint(painter2); g2.fillRect(x, y+h, width, getIconHeight()-h); g2.dispose(); } @Override public int getIconWidth() { return 200; } //dummy width @Override public int getIconHeight() { return height; } }; this.setBorder(BorderFactory.createTitledBorder( BorderFactory.createMatteBorder(height, 0, 0, 0, icon), title, TitledBorder.DEFAULT_JUSTIFICATION, titlePosition)); //System.out.println(getInsets()); } @Override public Dimension getMaximumSize() { Dimension d = super.getPreferredSize(); d.width = Short.MAX_VALUE; return d; } @Override public void updateUI() { super.updateUI(); color = null; } } }} ** 解説 [#o6cd3ee7] * 解説 [#o6cd3ee7] `TitledBorder`と、左下右の`Insets`が`0`(上余白のみ設定)でタイルアイコンでグラデーションを行う`MatteBorder`を組み合わせ、これを空の`JLabel`に設定することで、`TitledSeparator`を作成しています。 - 上 -- タイトルの垂直位置をデフォルトの`TitledBorder.DEFAULT_POSITION`にして、`Separator`上に重なるように表示 -- `Java 1.6.0`では、タイトルの上に`Separator`が表示される場合がある?(`1.7.0`では正常) - 中 -- タイトルの垂直位置が上(`TitledBorder.ABOVE_TOP`)で、`Separator`の上に表示 - 下 -- `JSeparator`を使用 ---- 注: 縦のセパレーターには未対応 注: 縦のセパレータには未対応 ** 参考リンク [#oc439ee0] * 参考リンク [#oc439ee0] - [[TitledBorderのタイトル位置>Swing/TitledBorder]] - [[Separatorのグラデーション>Swing/Gradient]] ** コメント [#i31c8183] * コメント [#i31c8183] #comment #comment