2016-08-03 77 views
0

任何人都可以帮我把图片加载到JLabel中? ATM不识别ImageIO。找不到变量ImageIO

private void doAnimation (int frame) 
{ 
    String str = Integer.toString(frame); 
    String selFrame = "f"+ str + ".png"; 

    BufferedImage wPic = ImageIO.read(this.getClass().getResource(selFrame)); 
    JLabel wIcon = new JLabel(new ImageIcon(wPic)); 
    add(wIcon); 
    JLabel picLabel = new JLabel(new ImageIcon(wPic)); 
    add(picLabel); 
} 

selFrame是使用定时器生成的png文件的名称。 Frame是在应用程序中使用时导入到函数中的数字。

这里是ActionListener的

if (actionEvent.getSource() == timerBackground) 
    { 
     if (backgroundNum == 42) backgroundNum = 1; 
     doAnimation(backgroundNum); 
     backgroundNum++; 
    } 

这里的计时器是在构造函数激活的定时器

timerBackground = new javax.swing.Timer (200, this); 
+0

有42个不同的框架(从一个gif->我分解成帧,并想要做一个动画) –

回答

0

你需要重写PaintComponenet方法,使周围的图像的尝试catch(IOException异常)集团= Imageio.read ... 你可以覆盖这样的方法:

@Override 
     public void paintComponent(Graphics g){ 
     super.paintComponent(g); 
     g.drawImage(image, 0, 0, this); 
     }