Java 在JComboBox中使用动画GIF

Java 在JComboBox中使用动画GIF,java,swing,gif,jcombobox,animated,Java,Swing,Gif,Jcombobox,Animated,我试图在JComboBox中使用动画(GIF)图标 由于DefaultListCellRenderer基于JLabel,因此将图像图标放入ComboBoxModel时直接支持它们 但是,这不适用于动画GIF 在下拉列表中,除非选择了GIF,否则它们根本不会显示(但在常规JLabel中使用GIF时,GIF也会起作用) 填充组合框的代码很简单: ImageIcon[] data = new ImageIcon[4]; data[0] = new ImageIcon("icon_one.gif");

我试图在JComboBox中使用动画(GIF)图标

由于DefaultListCellRenderer基于JLabel,因此将图像图标放入ComboBoxModel时直接支持它们

但是,这不适用于动画GIF

在下拉列表中,除非选择了GIF,否则它们根本不会显示(但在常规JLabel中使用GIF时,GIF也会起作用)

填充组合框的代码很简单:

ImageIcon[] data = new ImageIcon[4];
data[0] = new ImageIcon("icon_one.gif");
data[1] = new ImageIcon("icon_two.gif");
data[2] = new ImageIcon("icon_three.gif");
data[3] = new ImageIcon("icon_four.gif");
ComboBoxModel model = new DefaultComboBoxModel(data);
setModel(model);
icon_one.gif是静态图标,显示时没有任何问题。其他人都很活跃。(图像加载正确,因为如果我将这些图标中的任何一个直接指定给JLabel,它们都会显示得很好)

我还尝试使用我自己的基于JPanel的ListCellRenderer(受这个问题的答案启发:)

这样做效果更好,但也不理想。只有在显示下拉列表时将鼠标移到图标上,图标才会显示。所以我想这是一个重新油漆的问题,虽然我不知道在哪里

这是我的JPanel中实现ListCellRenderer接口的部分

public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
  this.image = ((ImageIcon)value).getImage();
  if (isSelected)
  {
    setBackground(list.getSelectionBackground());
    setForeground(list.getSelectionForeground());
  }
  else
  {
    setBackground(list.getBackground());
    setForeground(list.getForeground());
  }
  revalidate();
  repaint();

  return this;
}
调用revalidate()和repaint()的灵感来自于查看JLabel.setIcon()的代码

paint()方法也很简单:

public void paintComponent(Graphics g)
{
  super.paintComponent(g);
  if (image != null)
  {
    g.drawImage(image, 0, 0, this);
  }
}

有什么想法吗?我真的不需要在下拉列表中设置这些图标的动画(尽管这会很好),但我至少希望看到静态图像。

这个示例的灵感来自


有关类似的问题,请参阅。@mKorbel引用自:src/java/awt/Component.java#imageUpdate(…)@aterai一件事是API中实现了大量方法,第二件事是正确使用。。。
import java.awt.*;
import java.awt.image.*;
import java.net.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;
class MainPanel {
  public JComponent makeUI() {
    JComboBox combo = new JComboBox();
    URL url1 = getClass().getResource("static.png");
    URL url2 = getClass().getResource("animated.gif");
    combo.setModel(new DefaultComboBoxModel(new Object[] {
      new ImageIcon(url1), makeImageIcon(url2, combo, 1)
    }));
    JPanel p = new JPanel();
    p.add(combo);
    return p;
  }
  private static ImageIcon makeImageIcon(
      URL url, final JComboBox combo, final int row) {
    ImageIcon icon = new ImageIcon(url);
    icon.setImageObserver(new ImageObserver() {
      //http://www2.gol.com/users/tame/swing/examples/SwingExamples.html
      //AnimatedIconTableExample.java
      @Override public boolean imageUpdate(
          Image img, int infoflags, int x, int y, int w, int h) {
        if(combo.isShowing() && (infoflags & (FRAMEBITS|ALLBITS)) != 0) {
          if(combo.getSelectedIndex()==row) {
            combo.repaint();
          }
          BasicComboPopup p = (BasicComboPopup)
            combo.getAccessibleContext().getAccessibleChild(0);
          JList list = p.getList();
          if(list.isShowing()) {
            list.repaint(list.getCellBounds(row, row));
          }
        }
        return (infoflags & (ALLBITS|ABORT)) == 0;
      };
    });
    return icon;
  }
  public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
      @Override public void run() {
        createAndShowGUI();
      }
    });
  }
  public static void createAndShowGUI() {
    JFrame f = new JFrame();
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.getContentPane().add(new MainPanel().makeUI());
    f.setSize(320, 240);
    f.setLocationRelativeTo(null);
    f.setVisible(true);
  }
}