希望现在还不算太晚.
我设法通过这种方式在我的JPanel中获得动画gif:
private JPanel loadingPanel() {
JPanel panel = new JPanel();
BoxLayout layoutMgr = new BoxLayout(panel, BoxLayout.PAGE_AXIS);
panel.setLayout(layoutMgr);
ClassLoader cldr = this.getClass().getClassLoader();
java.net.URL imageURL = cldr.getResource("img/spinner.gif");
ImageIcon imageIcon = new ImageIcon(imageURL);
JLabel iconLabel = new JLabel();
iconLabel.setIcon(imageIcon);
imageIcon.setImageObserver(iconLabel);
JLabel label = new JLabel("Loading...");
panel.add(iconLabel);
panel.add(label);
return panel;
}
Run Code Online (Sandbox Code Playgroud)
这种方法的一些要点:
1.图像文件在jar内;
2. ImageIO.read()返回一个BufferedImage,它不会更新ImageObserver;
3.找到捆绑在jar文件中的图像的另一种方法是询问Java类加载器,即加载程序的代码,以获取文件.它知道事物的位置.
所以通过这样做,我能够在我的JPanel中获得我的动画gif,它就像一个魅力.
归档时间: |
|
查看次数: |
25858 次 |
最近记录: |