10
A
回答
19
是,利用在容器標籤嵌套的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);
10
嘗試CompoundIcon 編輯:Heisenbug的基於佈局的解決方案是 瑣碎的答案。
5
我只是做這個最近 - 我希望能夠將多個圖標在單行合併成一個單一的圖標。我這樣做的方式是下降到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;
}
}
3
這是非常可能的,JLabel的是搖擺的JComponent,你可以添加任何的JComponent到另一個的JComponent,這是同爲JLabel的
label.setLayout(new GridLayout(0, 2, 10, 10));
label.add(myIcon1);
label.add(myIcon2);
如果您添加fe JPanel to JLabel然後不要忘記setOpaque(false);
相關問題
- 1. JLabel的圖標未顯示
- 2. JLabel中的下標
- 3. 添加和一個JLabel刪除圖標
- 4. 正在更新JLabel圖標
- 5. 自定義JLabel圖標
- 6. 在Synth xml中設置JLabel圖標
- 7. 通過JLabel圖標添加JLabel文本。使用相同的JLabel文本
- 8. 帶JPanel和JLabel的帶圖標的JLayeredPane
- 9. 預加載圖像用作JLabel圖標
- 10. 如何讓我的JLabel圖標刷新?
- 11. 如何獲取圖像的座標/ JLabel?
- 12. 如何更改JLabel的圖標?
- 13. 用兩個圖例更改ggplot中的兩個圖例標題
- 14. 的JLabel +圖像
- 15. 檢查JLabel的圖標是否是一個特定的圖標或不
- 16. Jlabel中的圖片(含ImageIO)
- 17. 將JLabel放在另一個具有圖像的JLabel的頂部
- 18. 如何讓我的jlabel圖標看起來像桌面圖標?
- 19. 設置圖標的JLabel - 不顯示圖標
- 20. 多個JLabel上的鼠標Listern
- 21. 根據點擊交換兩個JLabel
- 22. 如何把一個JLabel的文本圖標下
- 23. JLabel與圖標對齊到左側
- 24. 爲什麼不能移動JLabel圖標?
- 25. 從組件更改JLabel圖標
- 26. 更新JLabel圖標搞背景
- 27. 檢查Jlabel是否有圖標
- 28. 加載JLabel圖標看起來變色
- 29. 兩個活動,但兩個圖標
- 30. 帶圖像的JLabel?
謝謝,那個伎倆。我還添加了'container.add(Box.createRigidArea(new Dimension(5,0)))'在兩個圖標之間添加一些空格。 –