3

http://prntscr.com/1gpdqe

对不起,这是我第一次在这个论坛上的链接。

谁能帮帮我吗?

我一直在尝试在JComboBox的一行中同时显示图像和文本,在网站上搜索后,我找不到解决问题的方法。

JLabel上方的JLabel有效,但JComboBox中的JLabel无效。

4

2 回答 2

8

也许这个例子会解决问题:

getListCellRendererComponent我使用Label+Icon

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.HashMap;
import java.util.Map;

import javax.swing.DefaultListCellRenderer;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;


public class ShowConboWithIcons extends JFrame {

private static final long serialVersionUID = 1L;

private static final ImageIcon INFO_ICON  = new ImageIcon("info.png");
private static final ImageIcon NONE_ICON  = new ImageIcon("none.png");
public final String NONE_STR = "None";
private final String INFO_STR = "Info";

private JComboBox comboBox;
private JPanel    topPanel;

private String[] str_arr = null; 


public ShowConboWithIcons(String[] str_arr) {
    this.str_arr = str_arr;     
}


public void createGUI(){

    setMinimumSize(new Dimension(100,100));
    setTitle("Demo");
    setLocation(200, 200);

    topPanel = new JPanel();
    getContentPane().add(topPanel, BorderLayout.CENTER);

    Map<Object, Icon> icons = new HashMap<Object, Icon>(); 

    icons.put(NONE_STR, NONE_ICON); 
    icons.put(INFO_STR, INFO_ICON); 

    comboBox = new JComboBox();
    comboBox.setRenderer(new IconListRenderer(icons));
    comboBox.addItem("None");

    for(String val : str_arr){
        comboBox.addItem(val);
    }

    topPanel.add(comboBox);

    super.addWindowListener(new WindowAdapter() {           
        public void windowClosing(WindowEvent e) {              
            dispose();          
        }           
    }); 
}

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException {

    UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );   

    String[] str_arr = {"A", "B", "C", "D", "E"};

    ShowConboWithIcons T = new ShowConboWithIcons(str_arr);
    T.createGUI();
    T.setVisible(true);        
}


class IconListRenderer extends DefaultListCellRenderer{ 
    private static final long serialVersionUID = 1L;
    private Map<Object, Icon> icons = null; 

    public IconListRenderer(Map<Object, Icon> icons){ 
        this.icons = icons; 
    } 

    @Override
    public Component getListCellRendererComponent(JList list, Object value, int index,boolean isSelected, boolean cellHasFocus)
    { 
        JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus); 

        // Get icon to use for the list item value 
        Icon icon = icons.get(value); 

        if(!value.toString().equals(NONE_STR)){
            icon = icons.get(INFO_STR);
        }

        // Set icon to display for value 
        label.setIcon(icon); 
        return label; 
    } 
}
}

在此处输入图像描述

于 2013-07-21T17:21:04.470 回答
5

不要将 a 插入JLabelJComboBox. 改用 a ListCellRenderer。阅读提供自定义渲染器

于 2013-07-21T16:53:36.273 回答