JLabel 中有两个图标?

发布于 2024-12-12 10:13:15 字数 188 浏览 0 评论 0原文

我在 JLabel 中有一个图标,如下所示:

在此处输入图像描述

是否可以添加另一个图标(例如标志代表一个国家)在颜色图标和文本之间?例如,我想在红色图标和 US 之间添加一个描绘美国国旗的图标。谢谢!

I have an icon in a JLabel as shown below:

enter image description here

Is it possible to add another icon (e.g. a flag representing a country) between the color icon and the text? For example, I want to add an icon depicting the U.S. flag between the red icon and US. Thanks!

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

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

发布评论

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

评论(4

岛徒 2024-12-19 10:13:16

这很有可能,JLabel 是 Swing JComponent,您可以将任何 JComponent 添加到另一个 JComponent,这对于 JLabel 来说是一样的,

label.setLayout(new GridLayout(0, 2, 10, 10));
label.add(myIcon1);
label.add(myIcon2);

如果您将 fe JPanel 添加到 JLabel 然后不要忘记 setOpaque(false);

this is pretty possible, JLabel is Swing JComponent and you can add any JComponent to the another JComponent, that's same for JLabel

label.setLayout(new GridLayout(0, 2, 10, 10));
label.add(myIcon1);
label.add(myIcon2);

if you add f.e. JPanel to the JLabel then don't forget to setOpaque(false);

甜是你 2024-12-19 10:13:15

是的,在容器标签中使用嵌套的 JLabel 和 BoxLayout:

JLabel container = new JLabel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JLabel icon1Label = new JLabel();
JLabel icon2Label = new JLabel();
icon1Label.setIcon(icon1);
icon2Label.setIcon(icon2);
container.add(icon1Label);
container.add(icon2Label);

Yes, use nested JLabel with BoxLayout in the container label:

JLabel container = new JLabel();
container.setLayout(new BoxLayout(container, BoxLayout.X_AXIS));
JLabel icon1Label = new JLabel();
JLabel icon2Label = new JLabel();
icon1Label.setIcon(icon1);
icon2Label.setIcon(icon2);
container.add(icon1Label);
container.add(icon2Label);
记忆消瘦 2024-12-19 10:13:15

尝试 CompoundIcon
编辑:Heisenbug 基于布局的解决方案是
微不足道的答案。

Try CompoundIcon
Edit: Heisenbug's layout-based solution is the
trivial answer.

在风中等你 2024-12-19 10:13:15

我最近刚刚这样做 - 我希望能够将单行中的多个图标组合成一个图标。我的方法是进入 BufferedImage 级别并将两个图像手动组合成单个图像,然后将其用作我的 JLabel 图标。还有其他方法可以实现相同的效果,但我不想更改我的 UI 组件层次结构。

我最终创建了一个组合多个图像并缓存它们的类。我这样使用它:

ImageIcon icon1 = ...;
ImageIcon icon2 = ...;

ImageIcon labelIcon = new CachedCompositeIcon( icon1, icon2 ).getIcon();
jLabel.setIcon( labelIcon );

这是来源:

/** This is a convenience class to handle creating a single composite icon from several icons, and caching the
 *  created icons to eliminate duplicate work. This class is basically used as a key into a map, allowing us to
 *  define both a hashCode and equals in a single place.
 */
public class CachedCompositeIcon
{
    private static final byte ICON_PADDING = 2;
    private static final HashMap<CachedCompositeIcon, ImageIcon> CACHED_ICONS =
            new HashMap<CachedCompositeIcon, ImageIcon>( 4 );

    private final ImageIcon[] m_icons;

    public CachedCompositeIcon(final ImageIcon... icons) {
        m_icons = icons;
    }

    public ImageIcon getIcon() {
        if ( !CACHED_ICONS.containsKey( this ) ) {
            CACHED_ICONS.put( this, lcl_combineIcons() );
        }

        return CACHED_ICONS.get( this );
    }

    /** Generates an icon that is a composition of several icons by appending each icon together with some
     *  padding between them.
     *
     * @return An icon that is the concatenation of all the icons this was constructed with.
     */
    private ImageIcon lcl_combineIcons() {
        // First determine how big our composite icon will be; we need to know how wide & tall to make it.
        int totalWidth = (m_icons.length - 1) * ICON_PADDING; // Take into account the padding between icons
        int minHeight  = 0;
        for ( int i = 0; i < m_icons.length; ++i ) {
            totalWidth += m_icons[i].getIconWidth();
            if ( m_icons[i].getIconHeight() > minHeight ) {
                minHeight = m_icons[i].getIconHeight();
            }
        }

        // Create an image big enough and acquire the image canvas to draw on
        final BufferedImage compositeImage = new BufferedImage( totalWidth, minHeight, BufferedImage.TYPE_INT_ARGB );
        final Graphics      graphics       = compositeImage.createGraphics();

        // Iterate over the icons, painting each icon and adding some padding space between them
        int x = 0;
        for ( int i = 0; i < m_icons.length; ++i ) {
            final ImageIcon icon = m_icons[ i ];
            graphics.drawImage( icon.getImage(), x, 0, null );
            x += icon.getIconWidth() + ICON_PADDING;
        }

        return new ImageIcon( compositeImage );
    }

    /** Generates a hash that takes into account the number of icons this composition includes and the hash &
     *  order of those icons.
     *
     * @return A hash code.
     */
    @Override
    public int hashCode() {
        int weakHash = m_icons.length;
        for ( int i = 0; i < m_icons.length; ++i ) {
            weakHash += m_icons[i].hashCode() * (i + 1);
        }
        return weakHash;
    }

    /** Two instances are equal if and only if they include the same icons and they're in the same order.
     *
     * @param obj An object to check for equality with this.
     *
     * @return true if the two objects are equal, false otherwise.
     */
    @Override
    public boolean equals(final Object obj) {
        if ( !(obj instanceof CachedCompositeIcon) ) {
            return false;
        }

        final CachedCompositeIcon other = (CachedCompositeIcon) obj;
        if ( m_icons.length != other.m_icons.length ) {
            return false;
        }

        for ( int i = 0; i < m_icons.length; ++i ) {
            if ( m_icons[i].hashCode() != other.m_icons[i].hashCode() ) {
                return false;
            }
        }

        return true;
    }
}

I just did this recently - I wanted to be able to combine multiple icons in a single row into a single icon. The way I did it was to go down to the BufferedImage level and compose the two images manually into a single image, and then use that as my JLabel icon. There are other ways of achieving the same effect, but I didn't want to have to change my UI component hierarchy.

I ended up creating a class that combines multiple images and caches them. I used it like this:

ImageIcon icon1 = ...;
ImageIcon icon2 = ...;

ImageIcon labelIcon = new CachedCompositeIcon( icon1, icon2 ).getIcon();
jLabel.setIcon( labelIcon );

Here's the source:

/** This is a convenience class to handle creating a single composite icon from several icons, and caching the
 *  created icons to eliminate duplicate work. This class is basically used as a key into a map, allowing us to
 *  define both a hashCode and equals in a single place.
 */
public class CachedCompositeIcon
{
    private static final byte ICON_PADDING = 2;
    private static final HashMap<CachedCompositeIcon, ImageIcon> CACHED_ICONS =
            new HashMap<CachedCompositeIcon, ImageIcon>( 4 );

    private final ImageIcon[] m_icons;

    public CachedCompositeIcon(final ImageIcon... icons) {
        m_icons = icons;
    }

    public ImageIcon getIcon() {
        if ( !CACHED_ICONS.containsKey( this ) ) {
            CACHED_ICONS.put( this, lcl_combineIcons() );
        }

        return CACHED_ICONS.get( this );
    }

    /** Generates an icon that is a composition of several icons by appending each icon together with some
     *  padding between them.
     *
     * @return An icon that is the concatenation of all the icons this was constructed with.
     */
    private ImageIcon lcl_combineIcons() {
        // First determine how big our composite icon will be; we need to know how wide & tall to make it.
        int totalWidth = (m_icons.length - 1) * ICON_PADDING; // Take into account the padding between icons
        int minHeight  = 0;
        for ( int i = 0; i < m_icons.length; ++i ) {
            totalWidth += m_icons[i].getIconWidth();
            if ( m_icons[i].getIconHeight() > minHeight ) {
                minHeight = m_icons[i].getIconHeight();
            }
        }

        // Create an image big enough and acquire the image canvas to draw on
        final BufferedImage compositeImage = new BufferedImage( totalWidth, minHeight, BufferedImage.TYPE_INT_ARGB );
        final Graphics      graphics       = compositeImage.createGraphics();

        // Iterate over the icons, painting each icon and adding some padding space between them
        int x = 0;
        for ( int i = 0; i < m_icons.length; ++i ) {
            final ImageIcon icon = m_icons[ i ];
            graphics.drawImage( icon.getImage(), x, 0, null );
            x += icon.getIconWidth() + ICON_PADDING;
        }

        return new ImageIcon( compositeImage );
    }

    /** Generates a hash that takes into account the number of icons this composition includes and the hash &
     *  order of those icons.
     *
     * @return A hash code.
     */
    @Override
    public int hashCode() {
        int weakHash = m_icons.length;
        for ( int i = 0; i < m_icons.length; ++i ) {
            weakHash += m_icons[i].hashCode() * (i + 1);
        }
        return weakHash;
    }

    /** Two instances are equal if and only if they include the same icons and they're in the same order.
     *
     * @param obj An object to check for equality with this.
     *
     * @return true if the two objects are equal, false otherwise.
     */
    @Override
    public boolean equals(final Object obj) {
        if ( !(obj instanceof CachedCompositeIcon) ) {
            return false;
        }

        final CachedCompositeIcon other = (CachedCompositeIcon) obj;
        if ( m_icons.length != other.m_icons.length ) {
            return false;
        }

        for ( int i = 0; i < m_icons.length; ++i ) {
            if ( m_icons[i].hashCode() != other.m_icons[i].hashCode() ) {
                return false;
            }
        }

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