我正在尝试将动画 gif 图像添加到树节点,但显然 JTree 是唯一无法呈现 gif 动画的组件。你能告诉我如何解决这个问题吗?
import java.awt.BorderLayout;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
public class Test extends JFrame {
public static void main(String[] args) {
ImageIcon leafIcon = new ImageIcon(any_gif_animated_image);
DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer();
renderer.setClosedIcon(leafIcon);
renderer.setOpenIcon(leafIcon);
renderer.setLeafIcon(leafIcon);
JTree tree = new JTree();
tree.setCellRenderer(renderer);
JLabel label = new JLabel(leafIcon);
label.setText("loading");
JPanel panel = new JPanel(new BorderLayout());
panel.add(label, BorderLayout.NORTH);
panel.add(tree, BorderLayout.CENTER);
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setSize(900, 600);
frame.setVisible(true);
}
}