树节点中的动画gif图像

时间:2013-02-20 11:28:43

标签: java swing jtree animated-gif treenode

我正在尝试将动画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);
    }
}

1 个答案:

答案 0 :(得分:1)