【问题标题】:How to put jpg in a JComboBox?如何将 jpg 放入 JComboBox?
【发布时间】:2013-09-27 01:49:15
【问题描述】:

这是我到目前为止写的一个例子:

import javax.swing.*;
import java.awt.*;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class program {

    JFrame win = new JFrame("bla bla");

    final private String[] animals = { "dog", "cat", "mouse" };

    private void Start() {
        JPanel superior = new JPanel();
        superior.setLayout(new GridLayout(3, 3));
        win.getContentPane().add(superior, BorderLayout.PAGE_START);
        final JComboBox<String> comboBox = new JComboBox<String>(animals);
        ((JLabel) comboBox.getRenderer()).setHorizontalAlignment(SwingConstants.CENTER);
        superior.add(comboBox);
        win.setSize(440, 290);
        win.setResizable(false);
        win.setLocationRelativeTo(null);
        win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        win.setVisible(true);
    }

    public static void main(String args[]) {
        program window = new program();
        window.Start();
    }
}

我为 animals 的每个项目都有一个单独的 jpg 字符串数组位于名为 jpg 的文件夹中,该文件夹位于 的同一级别(默认包)时间>。我正在使用日食。

我的想法是让 JComboBox 能够显示 jpg,同时使用带有我已经编码的某些鼠标点击事件的字符串(但不报告只是为了使其简短)。

我已经阅读了thisthisthis,但我无法真正完成工作:(

谁能解释我如何得到我想要的东西,也许修改我的代码以便我可以研究它?

【问题讨论】:

  • 请看一下,如何add images to Eclipse Project,有关使用相同的更多信息,请访问此Load ImageIcon Exception,此链接中的最后一个链接将提供相同事物的用法在链接 1 中进行了解释。还有一个 example,虽然不相关,但足以给出一个想法。希望它有所帮助:-)
  • 请参阅this example 以获取提示。

标签: java swing jcombobox imageicon listcellrenderer


【解决方案1】:

您需要为组合框提供自定义ListCellRenderer,该组合框能够显示图像(以及您需要的其他信息)

详情请见Providing a custom renderer

您可以使用ImageIO API 加载图像。您可能需要将结果包装在 ImageIcon 中以便更轻松地呈现它,但这取决于您的 API 实现

我建议使用DefaultListCellRenderer,因为它从JLabel 扩展而来,会让您的生活更轻松

非常简单的例子

我没有足够的信息来形成一个完全可运行的示例,但本质上,添加到组合框模型的值应该以某种方式包含对您要加载的图像的引用。

这样,在需要时,您可以提取图像并使用单元格渲染器显示它...

public class ImageCellRenderer extends DefaultListCellRenderer {

    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if (value instanceof ??) {
            ImageIcon icon = ...;
            setIcon(icon);
        }
        return this;
    }

}

并应用渲染器...

JComboBox cb = new JComboBox();
cb.setRenderer(new ImageCellRenderer());

更新

现在假设图像被命名为[animal].jpg(所以dog 将是dog.jpg)您应该能够构建一个简单的Map,将名称映射到动物图像...

// List of animals...
final private String[] animals = { "dog", "cat", "mouse" };

/*...*/

// Map of animal icons...
Map<String, Icon> mapImages = new HashMap<>();
// Build the icon image mapping
for (String animal : animals) {
    mapImages.put(animal, new ImageIcon(ImageIO.read(getClass().getResource("/" + animal + ".jpg))))
}

// Create a new cell renderer, passing the mappings
ImageCellRenderer renderer = new ImageCellRenderer(mapImages);

// Create a new combo box
JComboBox<String> comboBox = new JComboBox<String>(animals);
// Apply the renderer
comboBox.setRenderer(renderer);

/*...*/

public class ImageCellRenderer extends DefaultListCellRenderer {

    // Icon mappings
    private Map<String, Icon> mapImages

    public ImageCellRenderer(Map<String, Icon> mapImages) {
        // Make a new reference to the icon mappings
        this.mapImages = new HashMap<>(mapImages);
        setHorizontalAlignment(SwingConstants.CENTER);
    }

    public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
        super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
        if (value instanceof String) {
            // Look up the icon associated with the animal...
            Icon icon = mapImages.get(value.toString());
            setIcon(icon);
        }
        return this;
    }

}

【讨论】:

  • 感谢您的回复。我已经阅读了第一个链接,我正在阅读第二个和第三个。我问得太多了如果我祈祷你写下一些代码作为例子......?抱歉,如果是的话,无论如何感谢您的信息!
  • 抱歉,无法理解... if 应该使用什么参数?我应该把我的字符串数组放在哪里?如何将图标链接到字符串?
  • 哇!我们快要到了!我现在在组合框列表中得到的是左侧的 jpg 和右侧的字符串名称。有办法只显示jpg吗?如果是,我是否可以使用 setSelectedItem(animals[i]) 和 getSelectedItem(animals[i]) 方法来执行与动物字符串数组中的项目相关的一些鼠标单击操作?如果我需要发布代码,我会的,同时非常感谢!
  • 在单元格渲染中,调用super.getListCellRendererComponent后,调用setText(null)。是的,您应该能够调用 get/setSelectedItem 并获取/设置字符串值
  • 是的,它有效!只是说谢谢你的耐心,从中学到了很多东西! :)
猜你喜欢
  • 1970-01-01
  • 2012-08-19
  • 1970-01-01
  • 2012-01-19
  • 2011-06-20
  • 2013-10-15
  • 1970-01-01
  • 2014-12-14
  • 1970-01-01
相关资源
最近更新 更多