Мне нужно изменить значок jLabel каждые 2 секунды. Для этого я использую таймер и таймер, но он показывает только первое изображение. Вот код:Периодически меняйте значок JLabel
ImageIcon[] icons = {new ImageIcon(this.getClass().getResource("orange.jpg")), new
ImageIcon(this.getClass().getResource("cosmote.jpg")), new
ImageIcon(this.getClass().getResource("vodafone.jpg"))};
java.util.Timer timer = new java.util.Timer();
int indexIcon;
А потом в конструкторе JFrame:
initComponents();
open(fisierAgenda);
TimerTask task = new TimerTask() {
public void run() {
indexIcon=(indexIcon++)%3;
jLabel.setIcon(icons[indexIcon]);
jLabel.setText(""+indexIcon);
}
};
timer.schedule(task, 0, 2000);
Любая помощь будет оценена.
Внедрите ** Swing ** 'Timer' для повторения задач. Подробнее см. [Параллелизм в Swing] (http://docs.oracle.com/javase/tutorial/uiswing/concurrency/). См. Также [рабочий пример] (http://stackoverflow.com/a/13512826/418556). –