2

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

1 回答 1

1
  1. 动画图标树示例

  2. 动画 Gif 图标 (any_gif_animated_image)

  3. 在 JTree 节点中产生疯狂的动画

在此处输入图像描述

在此处输入图像描述

于 2013-02-20T11:59:10.163 回答