【问题标题】:Using an animated GIF in a JComboBox在 JComboBox 中使用动画 GIF
【发布时间】:2012-03-22 06:05:41
【问题描述】:

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

由于 DefaultListCellRenderer 是基于 JLabel 的,所以 ImageIcon 在放入 ComboBoxModel 时直接支持。

但这不适用于动画 GIF。

在下拉列表中,除非它们被选中,否则它们根本不会显示(不过,在常规 JLabel 中使用时,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(受此问题答案的启发:Java animated GIF without using a JLabel)。

效果更好,但也不理想。仅当我在显示下拉菜单时将鼠标移到它们上方时才会显示这些图标。所以我想这是一个修复问题,虽然我不知道在哪里

这是我的 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);
  }
}

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

【问题讨论】:

标签: java swing gif jcombobox animated


【解决方案1】:

这个例子的灵感来自AnimatedIconTableExample.java

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);
  }
}

【讨论】:

  • @mKorbel 引用自:src/java/awt/Component.java#imageUpdate(...)
  • @aterai 一件事是 API 中实现了很多方法,第二次正确使用 ...
猜你喜欢
  • 1970-01-01
  • 2020-08-30
  • 2013-11-26
  • 2017-12-30
  • 2010-10-31
  • 1970-01-01
  • 2018-03-25
  • 2014-07-06
  • 2018-02-16
相关资源
最近更新 更多