0

有人能帮助我吗。这是我的代码。

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,并且该过程将继续,直到我关闭我的程序,或者直到我单击一个指示停止的按钮。我是新手,所以请原谅我的方法,谢谢。

4

1 回答 1

0

Replace the code in actionPerformed() by

images.setIcon(myImages[i]);
i++;
if (i >= myImages.length) {
    i = 0;
}
于 2013-04-11T10:26:47.183 回答