2009-06-26 9 views

答えて

1

私が使用する1つのオプションは、JLabelsのシーケンスを持つことです。アイコン付きのものとテキストのみのもの。

他のオプションは、JLabelののミニHTMLのサポートを活用することです:のJLabelのテキストとして

<html><img src='theimage.png'>The text<img src='theimage2.png'> 

を持っています。このアプローチはテキストの書式設定にも使用できますが、画像タグがそこでも動作するかどうかはわかりません。

または、カスタムレンダリングbtwを実行するためにJLabel.paint()をオーバーライドしましたか?

は、その後、私は、次のアプローチを使用します。もちろん

List<Object> textAndImage = new ArrayList<Object>() {{ 
    add("This "); 
    add(new ImageIcon("image1.png")); 
    add(" is "); 
    add(new ImageIcon("image2.png")); 
    add(" an "); 
    add(" imaged text sample "); 
}}; 
FontMetrics fm = g.getFontMetrics(); 
int x = 0; 
for (Object o : textAndImage) { 
    if (o instanceof String) { 
     g.drawString((String)o, x, fm.getHeight()); 
     x += fm.stringWidth((String)o); 
    } else 
    if (o instanceof ImageIcon) { 
     ((ImageIcon)o).paintIcon(null, g, x, 0); 
     x += ((ImageIcon)o).getIconWidth(); 
    } 
} 

を、これは本格的な解決策ではなく、どのように進んでするあなたにいくつかのヒントを与えるかもしれません。

+1

タグは動作するはずですが、SwingのHTMLサポートはバグです。注目すべき欠点の1つは、HTMLを使用するたびに、L&F指定のフォントがもう使用できなくなることです。 – Joey

+0

画像を動的に取得してを使用するオプションは機能しません。 JLabelのpaintComponentをオーバーライドしましたが、テキストとイメージのシーケンスは完全に描画されません。塗料の前にジュラベルのサイズを設定する必要がありますか? –

+0

他の投稿の情報に基づいて、BMPや他のサポートされていない画像をImageIconに読み込もうとすると、それは動作しません。 ImageIO.read()メソッドを使用してイメージを取得します。ラベルにサイズがない場合は、何も描画されません。 BorderLayout-edコンテナにCENTER領域に配置してみてください。 – akarnokd

1

1つの方法は、アイコン(および必要に応じてテキスト)をペイントするカスタムボーダーを使用することです。その後、無期限にネストできます。

/** 
* Show a leading or trailing icon in border. 
*/ 
public static class IconBorder implements Border 
{ 
    /** 
    * @param icon - icon to paint for this border 
    * @param top outer insets for this border 
    * @param left 
    * @param bottom 
    * @param right 
    */ 
    public IconBorder(Icon icon, int top, int left, int bottom, int right) 
    { 
     setIcon(icon); 
     top_ = top; 
     left_ = left; 
     bottom_ = bottom; 
     right_ = right; 
     rect_ = new Rectangle(0, 0, icon_.getIconWidth(), icon_.getIconHeight()); 
    } 

    public Insets getBorderInsets(Component c) 
    { 
     if(icon_ == null) 
      return new Insets(0, 0, 0, 0); 
     return isTrailing_ ? new Insets(top_, left_, bottom_, icon_.getIconWidth() + right_) : 
          new Insets(top_, icon_.getIconWidth() + left_, bottom_, right_); 
    } 

    public boolean isBorderOpaque() 
    { 
     return false; 
    } 

    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) 
    { 
     if(icon_ != null) 
     { 
      if(isTrailing_) 
       x = width - icon_.getIconWidth() + 4; 
      else 
       x += left_ - 1; 
      icon_.paintIcon(c, g, x, y + top_); 
      rect_.x = x; 
      rect_.y = y; 
     } 
    } 

    public void setIcon(Icon icon) 
    { 
     if(icon_ != icon) 
      icon_ = icon; 
    } 

    public Icon getIcon() 
    { 
     return icon_; 
    } 

    public void setPosition(boolean isTrailing) 
    { 
     isTrailing_ = isTrailing;   
    } 

    public Rectangle getIconRect() 
    { 
     return rect_; 
    } 

    private final int top_; 
    private final int left_; 
    private final int bottom_; 
    private final int right_; 
    private final Rectangle rect_; 
    private Icon icon_; 
    private boolean isTrailing_ = true; 
} 

私はJTextFieldの(ラ・ブラウザの検索ボックス)に検索アイコンを追加するためにこれを使用する:

は、ここではそのようなボーダーです。 getIconRectは、マウスのホバーを確認するために使用できます。たとえば、マウスが検索アイコンの上にあるときにカーソルをHAND_CURSORに変更します。

関連する問題