2013-04-11 24 views
0

有人可以幫助我。這是我的代碼。如何循環添加到JLable的圖像

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.BorderFactory; 
import javax.swing.ImageIcon; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.Timer; 
import javax.swing.border.LineBorder; 
import javax.swing.border.TitledBorder; 

public class picturesPanel implements ActionListener 
{ 

public static JPanel mainPanel; 
public static JPanel panel1; 
public static JLabel images; 
boolean go = false; 

private int i = 0;  
private ImageIcon myImage1 = new ImageIcon ("D:\\LCTPShare\\Picture1.gif"); 
private ImageIcon myImage2 = new ImageIcon ("D:\\LCTPShare\\Picture6.gif"); 
private ImageIcon myImage3 = new ImageIcon ("D:\\LCTPShare\\Picture3.gif"); 
private ImageIcon myImage4 = new ImageIcon ("D:\\LCTPShare\\Picture4.gif"); 
private ImageIcon myImage5 = new ImageIcon ("D:\\LCTPShare\\Picture5.gif"); 
private ImageIcon myImage6 = new ImageIcon ("D:\\LCTPShare\\Picture7.gif"); 
private ImageIcon myImage7 = new ImageIcon ("D:\\LCTPShare\\Picture8.gif"); 
private ImageIcon[] myImages = new ImageIcon[7];  

public void pictures() 
{  
    myImages[0]=myImage1; 
    myImages[1]=myImage2; 
    myImages[2]=myImage3; 
    myImages[3]=myImage4; 
    myImages[4]=myImage5; 
    myImages[5]=myImage6; 
    myImages[6]=myImage7; 

    LineBorder borderA = new LineBorder(Color.RED); 
    TitledBorder titledBorderA = BorderFactory.createTitledBorder(borderA, ""); 

    mainPanel = new JPanel(); 
    mainPanel.setBackground(Color.CYAN); 
    mainPanel.setInputVerifier(null);  
    mainPanel.setBorder(titledBorderA); 
    FINAL_LCTP_WORKBENCE.buttonsPanel.add(mainPanel, 0);  

    panel1 = new JPanel(); 
    panel1.setBackground(Color.LIGHT_GRAY); 
    panel1.setPreferredSize(new Dimension(260, 115));   
    mainPanel.add(panel1); 

    images = new JLabel(); 
    images.setIcon(myImage1);  
    panel1.add(images, BorderLayout.CENTER);   
    javax.swing.Timer timer = new javax.swing.Timer(5000, this); 
    timer.start(); 
} 


public void actionPerformed(ActionEvent e) 
{ 
    i++;   

    if(i == 1) 
    { 
     images.setIcon(myImages[0]);                          

    } 
    if(i == 2) 
    { 
     images.setIcon(myImages[1]); 

    } 
    if(i == 3) 
    { 
     images.setIcon(myImages[2]); 

    } 
    if(i == 4) 
    { 
     images.setIcon(myImages[3]); 

    } 
    if(i == 5) 
    { 
     images.setIcon(myImages[4]); 

    }   
    if(i == 6) 
    { 
     images.setIcon(myImages[5]); 

    } 
    if(i == 7) 
    { 
     images.setIcon(myImages[6]); 

    }  


    panel1.revalidate(); 
    panel1.repaint(); 

} 

}

我能顯示我想要的圖片1至6我如何在我的JPanel循環顯示的圖像?我的目標是在圖像1到6顯示後,它將再次顯示圖像1到6,並且過程一直持續到我關閉程序,或者直到我點擊一個指示停止的按鈕。我是新來的,所以請原諒我的方法,謝謝。

回答

0

通過

images.setIcon(myImages[i]); 
i++; 
if (i >= myImages.length) { 
    i = 0; 
} 
+0

感謝替換actionPerformed()你的代碼的小費先生。 – 2013-04-12 01:02:58