2016-08-03 81 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); 
     }