2011-10-27 33 views
10

Tôi có một biểu tượng trong một JLabel như hình dưới đây:Hai biểu tượng trong một JLabel?

enter image description here

Có thể thêm một biểu tượng khác (ví dụ một lá cờ đại diện cho một quốc gia) giữa các biểu tượng màu sắc và các văn bản không? Ví dụ: tôi muốn thêm biểu tượng mô tả cờ Mỹ giữa biểu tượng màu đỏ và US. Cảm ơn!

Trả lời

19

Vâng, sử dụng lồng JLabel với BoxLayout trong nhãn container:

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); 
+1

Cảm ơn, điều đó đã làm được điều đó. Tôi cũng đã thêm 'container.add (Box.createRigidArea (new Dimension (5,0)))' để thêm một số khoảng trống giữa hai biểu tượng. –

10

Hãy thử CompoundIcon Chỉnh sửa: Giải pháp dựa trên bố cục của Heisenbug là câu trả lời nhỏ nhặt.

+3

hoặc lồng các vật chứa trong bố cục. hoặc thêm một cột khác trong danh sách chỉ cho mục đích này. – Randy

+0

+1 để triển khai 'Biểu tượng'. – trashgod

+0

+1: cảm ơn..không biết về CompoundIcon .. – Heisenbug

5

Tôi chỉ làm điều này thời gian gần đây - Tôi muốn để có thể kết hợp nhiều biểu tượng trong một hàng duy nhất thành một biểu tượng duy nhất. Cách tôi đã làm là đi xuống mức BufferedImage và soạn hai hình ảnh theo cách thủ công thành một hình ảnh duy nhất, sau đó sử dụng nó làm biểu tượng JLabel của tôi. Có nhiều cách khác để đạt được hiệu quả tương tự, nhưng tôi không muốn phải thay đổi phân cấp thành phần giao diện người dùng của mình.

Tôi đã tạo một lớp kết hợp nhiều hình ảnh và lưu trữ chúng. Tôi đã sử dụng nó như thế này:

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

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

Dưới đây là các nguồn:

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

này là khá tốt, JLabel là Swing JComponent và bạn có thể thêm bất kỳ JComponent cho bên kia JComponent, đó là tương tự cho JLabel

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

nếu bạn thêm fe JPanel đến JLabel sau đó đừng quên setOpaque(false);

Các vấn đề liên quan