如何将背景图像添加到JComboBox?

时间:2014-01-15 11:17:11

标签: java swing background-image jcombobox

我问here如何正确地将背景图像添加到JComboBox,但没有看到答案,所以我认为我应该将这两个问题分开......

因此。如何正确地将背景图像添加到JComboBox文本字段和JComboBox弹出面板?

UPD: 一些代码给你)

当前代码:

// ... Renderer

public class CBoxListCellRenderer implements ListCellRenderer {

     ImagePanel panel;
     JLabel label = new JLabel();

     public CBoxListCellRenderer(Image img) {panel = new ImagePanel(img);}

     public void setImage(Image img) {panel.setImage(img);}

     @Override
     public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
          label.setText(value.toString());
          panel.add(label);
          return panel;
     }
}

// ... ImagePanel

public class ImagePanel extends JPanel {

    private BufferedImage img;

    public ImagePanel(String img) {
        setImage(img);
    }

    public void setImage(String img)
    {
        try {
            this.img = ImageIO.read(this.getClass().getResource(img));
        } catch (IOException ex) {
           ex.printStackTrace();
        }
        Dimension size = new Dimension(this.img.getWidth(), this.img.getHeight());
        setSize(size);
    }

    @Override
    public void paintComponent(Graphics g) {
        g.drawImage(img, 0, 0, getWidth(), getHeight(), 0, 0, img.getWidth(), img.getHeight(), this);
    }
}

// ... Colorizing arrow

class ColorArrowUI extends BasicComboBoxUI {

     public static ComboBoxUI createUI(JComponent c) {
         return new ColorArrowUI();
     }

     @Override protected JButton createArrowButton() {
          return new BasicArrowButton(
               BasicArrowButton.SOUTH,
               Color.cyan, Color.magenta,
               Color.yellow, Color.blue);
     }
}

// ... Creating object

combo_language = new JComboBox(new DefaultComboBoxModel(new String[] { "English", "日本語", "Русский" }));
combo_language.setBorder(null);
combo_language.setRenderer(new CBoxListCellRenderer(new ImageIcon(getClass().getResource("/Images/form.png")).getImage()));
combo_language.setUI(new ColorArrowUI());


// ... Putting JComboBox to JFrame

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

以下是自定义ListCellRenderer的示例。我返回一个已向其绘制图像的JPanel,然后添加了一个JLabel,其中包含列表对象的值。它有改进的余地,但它只是你可以做的一个例子。

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.*;

public class TestCBoxListCellRenderer {

    public TestCBoxListCellRenderer() {
        String[] list = {"Hello World 1", "Hello World 2", "Hello World 3"};
        JComboBox box = new JComboBox(list);
        box.setRenderer(new CBoxListCellRenderer());

        JOptionPane.showMessageDialog(null, box, "Check out this Renderer", JOptionPane.PLAIN_MESSAGE);
    }

    public static void main(String[] args) {
        new TestCBoxListCellRenderer();
    }

    class BackGroundPanel extends JPanel {

        BufferedImage img = null;

        public BackGroundPanel() {
            try {
                img = ImageIO.read(TestCBoxListCellRenderer.class.getResource("/res/leafcell.png"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.drawImage(img, 0, 0, getWidth(), getHeight(),
                    0, 0, img.getWidth(), img.getHeight(), this);
        }
    }

    private class CBoxListCellRenderer implements ListCellRenderer {

        final BackGroundPanel panel = new BackGroundPanel();
        JLabel label = new JLabel();

        @Override
        public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {

            label.setText(value.toString());
            panel.add(label);

            return panel;
        }
    }
}

enter image description here

您可能希望添加细线边框或在选中时执行某些操作,由您自己决定。使用getListCellRendererComponent方法中的变量根据您的喜好进行渲染