not redisplaying an image s

蹲街弑〆低调 提交于 2019-11-28 14:25:10

Always use javax.swing.Timer never use Thread.sleep(...) in Swing atleast. Here try this code, but do replace the path to your images :

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class SlideShow extends JPanel
{
    private int i = 0;
    private Timer timer;
    private JLabel images = new JLabel();
    private Icon bg = UIManager.getIcon("OptionPane.warningIcon");
    private Icon red = UIManager.getIcon("OptionPane.errorIcon");
    private Icon blue =  UIManager.getIcon("OptionPane.informationIcon");
    private ImageIcon pictures1, pictures2, pictures3, pictures4;
    private ActionListener action = new ActionListener()
    {
        public void actionPerformed(ActionEvent ae)
        {           

            boolean go = true;

            i++;
            System.out.println(i);

            if(i == 1)
            {
                images.setIcon(bg);                                                                                                      
                System.out.println("picture 1 should be displayed here");
            }
            if(i == 2)
            {
                images.setIcon(red);   
                System.out.println("picture 2 should be displayed here");
            }
            if(i == 3)
            {
                images.setIcon(blue);   
                System.out.println("picture 3 should be displayed here");  
            }
            if(i == 4)
            {
                images.setIcon(bg);   
                System.out.println("picture 4 should be displayed here");  
            }
            if(i == 5)
            {
                go = false;
                timer.stop();
                System.exit(0);
            }
            revalidate();
            repaint();
        }
    };

    public SlideShow()
    {
        JFrame frame = new JFrame("SLIDE SHOW");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationByPlatform(true);

        frame.getContentPane().add(this);

        add(images);

        frame.setSize(300, 300);
        frame.setVisible(true); 
        timer = new Timer(2000, action);    
        timer.start();  
    }

    public static void main(String... args)
    {
        SwingUtilities.invokeLater(new Runnable()
        {
            public void run()
            {
                new SlideShow();
            }
        });
    }
}

The whole if (i == ... part can be simplified. Declare a static class member in SlideShowGUI:

private final static ImageIcon[] icons = {new ImageIcon("galileo1.jpg"),
                                          new ImageIcon("galileo2.jpg"),
                                          new ImageIcon("galileo3.jpg"),
                                          new ImageIcon("galileo4.jpg")};

and use it in the replacement of the if statements:

images.setIcon(icons[i-1]);
System.printf("Picture %s should be displayed%n", i-1);
if (i == 4) {
  i = 0;
}
Hanon

You can simplify your code as Andreas_D mentioned.

Your current design will block the main thread as you call Thread.sleep() to it, this will freeze your application.

If you would like to update the Image, you should implement the update code inside run() method.

So if you detect the user press on submit JButton, create and start the new Thread for updating UI.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!