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 :(得分:0)
将actionPerformed()
中的代码替换为
images.setIcon(myImages[i]);
i++;
if (i >= myImages.length) {
i = 0;
}