JLabel 中有两个图标?
我在 JLabel 中有一个图标,如下所示:
是否可以添加另一个图标(例如标志代表一个国家)在颜色图标和文本之间?例如,我想在红色图标和 US
之间添加一个描绘美国国旗的图标。谢谢!
I have an icon in a JLabel as shown below:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这很有可能,JLabel 是 Swing JComponent,您可以将任何 JComponent 添加到另一个 JComponent,这对于 JLabel 来说是一样的,
如果您将 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
if you add f.e. JPanel to the JLabel then don't forget to
setOpaque(false);
是的,在容器标签中使用嵌套的 JLabel 和 BoxLayout:
Yes, use nested JLabel with BoxLayout in the container label:
尝试 CompoundIcon
编辑:Heisenbug 基于布局的解决方案是
微不足道的答案。
Try CompoundIcon
Edit: Heisenbug's layout-based solution is the
trivial answer.
我最近刚刚这样做 - 我希望能够将单行中的多个图标组合成一个图标。我的方法是进入 BufferedImage 级别并将两个图像手动组合成单个图像,然后将其用作我的 JLabel 图标。还有其他方法可以实现相同的效果,但我不想更改我的 UI 组件层次结构。
我最终创建了一个组合多个图像并缓存它们的类。我这样使用它:
这是来源:
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 myJLabel
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:
Here's the source: