简体   繁体   English

刷新或更新JLabel

[英]Refreshing or updating of JLabel

I want to develop a program that presents a slideshow of images using JLabel s. 我想开发一个使用JLabel呈现图像幻灯片的程序。 Whenever I display a single image it works fine . 每当我显示单个图像时,它都可以正常工作。 My problem is that when I load image paths in an array they aren't displayed on to the JLabel : refreshing or updating of JLabel doesn't work. 我的问题是,当我将图像路径加载到数组中时,它们不会显示在JLabel :刷新或更新JLabel不起作用。

Here is my code that displays the function where i find something wrong 这是我的代码,显示我发现错误的功能

private ActionListener action = new ActionListener(){
    public void actionPerformed(ActionEvent ae){
        if (count == imgNames.size()) {
            System.exit(0);
        }
        JLabel temp = new JLabel();
        temp.setIcon(new ImageIcon(imgNames.get(count)));
        picLabel = temp ;
        add(picLabel);
        System.out.println("Count "+ count);
        count++;
        revalidate();
        repaint();
    }
};

Where the timer is set up as follows: timer设置如下:

timer = new Timer(2000, action);
timer.start();

The ans to the above problem has been solved. 以上问题的答案已解决。 This is very simple. 这很简单。 Use ImageIcon[] that is an array instead of getting strings(FILE NAMES) and then giving it to the setIcon method , because this doesn't refresh or update image on jLabel.. so its better to use this way. 使用作为数组的ImageIcon []而不是获取字符串(文件名),然后将其提供给setIcon方法,因为这不会刷新或更新jLabel上的图像。因此,最好使用这种方式。 I don't know the cause of this answer . 我不知道这个答案的原因。 If anyone knows please post it so that others and I will be helpful to the topic. 如果有人知道,请张贴它,以便其他人和我对这个话题有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM