在java中可以利用Timer类实现图片循环显示。Timer构造函数如下:
public Timer(int delay,ActionListener listener)
delay,是延迟的毫秒数,listener是监听器实例。实现过delay毫秒,触发一个动作事件。调用start启动定时器。
图片循环显示,利用标签显示图片,过delay毫秒触发一个动作事件,更新标签的图片。
代码如下:
class TimePanel extends JPanel
{
JLabel lab = new JLabel();
Timer t = new Timer(1000,new ActionL());
ImageIcon[] image = new ImageIcon[20];
int index = 0;
public TimePanel()
{
setLayout(new GridLayout(1,1));
add(lab);
for(int i=0;i<image.length;i++)
image[i] = new ImageIcon("img/"+(i+1)+".jpg");
lab.setIcon(image[0]);
t.start();
}
class ActionL implements ActionListener