使用图形的 jlabel 中文本之间的图标

发布于 2024-07-26 01:50:09 字数 65 浏览 3 评论 0原文

如何使用图形在 JLabel 中绘制多个图标(即文本之间的图标)? 请协助我完成这项工作

How to paint multiple icons (ie., say icons between text) in JLabel using graphics? Please do assist me in this effort

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

以可爱出名 2024-08-02 01:50:09

我会使用的一个选项是拥有一系列 JLabel。 带图标的和仅带文本的。

另一种选择是利用 JLabel 的迷你 HTML 支持:

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

作为 JLabel 的文本。 这种方法适用于文本格式,但我不确定图像标签是否也适用。

或者您确实重写了 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();
    }
}

当然,这不是一个完全成熟的解决方案,但可能会给您一些如何继续的提示。

One option I would use is to have a sequence of JLabels. Ones with icon and ones with text only.

The other option would be to leverage the mini HTML support of the JLabel: Have

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

as the text of the JLabel. This approach works for text formatting but I'm not sure if the image tags work there too.

Or you did override the JLabel.paint() to do custom rendering btw?

Then I would use the following approach:

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();
    }
}

Of course this is not a fully fledged solution but might give you some hints how to proceed.

请帮我爱他 2024-08-02 01:50:09

一种方法是使用自定义边框来绘制图标(如果需要,还可以绘制文本),然后您可以无限期地嵌套。

这是一个这样的边框:

/**
 * 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

One way is to use a custom border that paints an icon (and text if you want), then you can nest indefinitely.

Here's one such border:

/**
 * 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;
}

I use this to add a search icon to a JTextField (a la browser search box). getIconRect can be used to check for mouse hover. For example I change cursor to HAND_CURSOR when mouse is over the search icon.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文